コード例 #1
0
        static void Main(string[] args)
        {
            TreeNode rootNodeA = new TreeNode("A");
            Tree tree = new Tree(rootNode: rootNodeA);
            tree.BuildSampleTree();

            Console.WriteLine("============= START Full TREE==============");
            // Print Tree
            tree.PrintTreeNode(rootNodeA, TreeTravesalOrder.POST_ORDER, string.Empty);
            Console.WriteLine("=============END ==============");

            Console.WriteLine("============= START First Child Node Of Root==============");
            // Print Tree
            tree.PrintTreeNode(rootNodeA.Childrens[0], TreeTravesalOrder.PRE_ORDER, string.Empty);
            Console.WriteLine("=============END ==============");

            List<TreeNode> decendentNodes = tree.FindDescendents(rootNodeA);
            Console.WriteLine("============= START Decendent Nodes Of [ {0} ]==============", rootNodeA.Data);
            //As first element will be always parent input node so while prinign skipping that node.
            for (int i = 1; i < decendentNodes.Count; i++)
            {
                Console.WriteLine("Node - " + decendentNodes[i].Data);
            }

            Console.WriteLine("============= END Decendent Nodes Of [ {0} ]==============", rootNodeA.Data);

            TreeNode parentOfDecencentsNode = rootNodeA.Childrens[0];
            decendentNodes = tree.FindDescendents(parentOfDecencentsNode);
            Console.WriteLine("============= START Decendent Nodes Of [ {0} ]==============", parentOfDecencentsNode.Data);

            //As first element will be always parent input node so while prinign skipping that node.
            for (int i = 1; i < decendentNodes.Count; i++)
            {
                Console.WriteLine("Node - " + decendentNodes[i].Data);
            }

            Console.WriteLine("============= END Decendent Nodes Of [ {0} ]==============", parentOfDecencentsNode.Data);

            Console.WriteLine("============= START PAth ==============");
            HashSet<TreeNode> path = tree.FindPathBetweenTwoNodes(rootNodeA.Childrens[0], rootNodeA.Childrens[0].Childrens[0].Childrens[2]);
            foreach (TreeNode pathNode in path)
            {
                Console.Write(pathNode.Data + " => ");
            }

            Console.WriteLine("\n============= End Path==============");

            Console.WriteLine("============= START Leaf Nodes ==============");
            List<TreeNode> leafNodes = tree.FindAllLeafElementOfANode(rootNodeA);
            foreach (TreeNode pathNode in leafNodes)
            {
                Console.Write(pathNode.Data + " => ");
            }

            Console.WriteLine("\n============= End Leaf Nodes ==============");

            Console.WriteLine("============= START Internal Nodes ==============");
            List<TreeNode> internalNodes = tree.FindAllInternalNodes(rootNodeA);
            foreach (TreeNode node in internalNodes)
            {
                Console.Write(node.Data + " => ");
            }

            Console.WriteLine("\n============= End Internal Nodes ==============");
        }