Esempio n. 1
0
        static void TestBinaryTree()
        {
            var tree = new BinaryTree();

            tree.Insert(7);
            tree.Insert(4);
            tree.Insert(9);
            tree.Insert(1);
            tree.Insert(6);
            tree.Insert(8);
            tree.Insert(10);

            var tree2 = new BinaryTree();

            tree2.Insert(7);
            tree2.Insert(4);
            tree2.Insert(9);
            tree2.Insert(1);
            tree2.Insert(6);
            tree2.Insert(8);
            tree2.Insert(10);

            //Console.WriteLine(tree.Find(6));
            //Console.WriteLine(tree.Find(3));

            //tree.TraversePreOrder();
            //tree.TraverseInOrder();
            //tree.TraversePostOrder();

            Console.WriteLine("Tree Height: {0}", tree.Height());
            Console.WriteLine("Tree Min Value: {0}", tree.Min());

            Console.WriteLine("Binary Search Tree Min Value: {0}", tree.MinFromBST());
            Console.WriteLine("Binary Search Tree Max Value: {0}", tree.MaxFromBST());

            Console.WriteLine("Are Equals: {0}", tree.Equals(tree2));

            //tree.SwapRoot();
            Console.WriteLine("Is Binary Search Tree: {0}", tree.IsBinarySearchTree());

            //var nodes = tree.GetNodesAtDistance(2);
            //foreach (var node in nodes)
            //    Console.WriteLine(node);

            tree.TraverseBreadthFirst();
            tree.TraverseLevelOrder();

            Console.WriteLine("Size: {0}", tree.Size());

            Console.WriteLine("Leaves: {0}", tree.CountLeaves());

            Console.WriteLine("Max: {0}", tree.Max());

            Console.WriteLine("Contains 6: {0}", tree.Contains(6));
            Console.WriteLine("Contains 11: {0}", tree.Contains(11));

            Console.WriteLine("Are Sibling: {0}", tree.AreSibling(4, 9));
            Console.WriteLine("Are Sibling: {0}", tree.AreSibling(1, 10));

            var ancestors = tree.GetAncestors(4);

            foreach (var ancestor in ancestors)
            {
                Console.WriteLine("Ancestor of 4: {0}", ancestor);
            }


            var ancestors1 = tree.GetAncestors(1);

            foreach (var ancestor in ancestors1)
            {
                Console.WriteLine("Ancestor of 1: {0}", ancestor);
            }

            var ancestors2 = tree.GetAncestors(10);

            foreach (var ancestor in ancestors2)
            {
                Console.WriteLine("Ancestor of 10: {0}", ancestor);
            }

            Console.WriteLine("Is Balanced: {0}", tree.IsBalanced());
        }