public static void Main(string[] args)
        {
            var parentChildArray = new ParentChildNode[]
            {
                new ParentChildNode('a', 1, null),
                new ParentChildNode('b', 2, 1),
                new ParentChildNode('c', 3, 1),
                new ParentChildNode('d', 4, 2),
                new ParentChildNode('e', 5, 3),
                new ParentChildNode('f', 6, 3),
                new ParentChildNode('g', 7, 2),
                new ParentChildNode('i', 8, 4),
                new ParentChildNode('j', 9, 8)
            };

            Tree graph = new Tree(parentChildArray);

            var hierarchicallyStructuredRecords = graph
                                                  .DepthFirstSearch()
                                                  .BuildHierarchicallyStructuredForm();

            Console.WriteLine("Name,\tlft,\trgt,");

            foreach (var record in hierarchicallyStructuredRecords)
            {
                Console.WriteLine($"{record.Name},\t{record.Lft},\t{record.Rgt},");
            }

            Console.ReadLine();
        }
Exemple #2
0
        private Tree Traverse(ParentChildNode node)
        {
            var inProgressNodes = new Stack <ParentChildNode>();

            inProgressNodes.Push(node);
            _visited[node.Id] = true;

            var left = visitCounter++;

            while (inProgressNodes.Count != 0)
            {
                var children = GetChildrenFor(node);

                if (children.Length == 0)
                {
                    _structuredNested[node.Id] = new NestedNode(node.Name, left, visitCounter++);
                }

                foreach (var child in children)
                {
                    Traverse(child);

                    if (!AllChildrenVisited(node))
                    {
                        continue;
                    }

                    _structuredNested[node.Id] = new NestedNode(node.Name, left, visitCounter++);
                }

                inProgressNodes.Pop();
            }

            return(this);
        }
Exemple #3
0
        private ParentChildNode[] GetChildrenFor(ParentChildNode parentNode)
        {
            var children = _parentChildNodes.Where(childNode => childNode.ParentId == parentNode.Id).ToArray();

            return(children);
        }
Exemple #4
0
        private bool AllChildrenVisited(ParentChildNode parentNode)
        {
            var children = GetChildrenFor(parentNode);

            return(children.All(c => _visited[c.Id]));
        }