Пример #1
0
        static void Main(string[] args)
        {
            // Create the root node for the tree
            MyNode rootNode = new MyNode {
                Code = "abc", Description = "abc node"
            };
            // Instantiate a new tree with the given root node.  string is the index key type, "Code" is the index property name
            var tree = new IndexedTree <MyNode, string>("Code", rootNode);

            // Add a child to the root node
            tree.Root.AddChild(new MyNode {
                Code = "def", Description = "def node"
            });
            MyNode newNode = new MyNode {
                Code = "foo", Description = "foo node"
            };

            // Add a child to the first child of root
            tree.Root.Children[0].AddChild(newNode);
            // Add a child to the previously added node
            newNode.AddChild(new MyNode {
                Code = "bar", Description = "bar node"
            });
            // Show the full tree
            Console.WriteLine("Root node tree:");
            PrintNodeTree(tree.Root, 0);
            Console.WriteLine();
            // Find the second level node
            MyNode foundNode = tree.FindNode("def");

            if (foundNode != null)
            {
                // Show the tree starting from the found node
                Console.WriteLine("Found node tree:");
                PrintNodeTree(foundNode, 0);
            }
            // Remove the last child
            foundNode = tree.FindNode("foo");
            TreeNodeBase nodeToRemove = foundNode.Children[0];

            foundNode.RemoveChild(nodeToRemove);
            // Add a child to this removed node.  The tree index is not updated.
            nodeToRemove.AddChild(new MyNode {
                Code = "blah", Description = "blah node"
            });
            Console.ReadLine();
        }
Пример #2
0
        static void Main(string[] args)
        {
            MyNode rootNode = new MyNode {
                Code = "abc", Description = "abc node"
            };
            var tree = new IndexedTree <MyNode, string>("Code", rootNode);

            tree.Root.AddChild(new MyNode {
                Code = "def", Description = "def node"
            });
            MyNode foundNode = tree.FindNode("abc");

            if (foundNode != null)
            {
                Console.WriteLine("Found node: " + foundNode.Description);
                Console.WriteLine("with children:");
                foreach (MyNode child in foundNode.Children)
                {
                    Console.WriteLine("  " + child.Description);
                }
            }
            foundNode = tree.FindNode("def");
            Console.ReadLine();
        }