Exemplo n.º 1
0
        static void Main(string[] args)
        {
            BinaryTree tree = new BinaryTree();

            tree.root            = new Node(1);
            tree.root.Left       = new Node(2);
            tree.root.Right      = new Node(3);
            tree.root.Left.Left  = new Node(4);
            tree.root.Left.Right = new Node(5);

            Console.WriteLine("Preorder traversal of binary tree is ");
            var preorderSeq = new List <int>();

            tree.PrintPreorder(preorderSeq);

            Console.WriteLine("\nInorder traversal of binary tree is ");
            var inorderSeq = new List <int>();

            tree.PrintInorder(inorderSeq);

            Console.WriteLine("\nPostorder traversal of binary tree is ");
            var postorderSeq = new List <int>();

            tree.PrintPostorder(postorderSeq);

            Console.WriteLine();
        }
Exemplo n.º 2
0
        // various Tree Traversals demo
        // by Sam Klok (Serge Klokov)
        //
        // c# implementation of operations with binary tree
        // and with ..
        //
        // find lowest common ancestor of
        // n1 and n2 using one traversal of binary tree
        // It also handles cases even when n1 and n2 are not there in Tree

        //
        //      1
        //   2    3
        // 4  5  6  7
        //

        static void Main(string[] args)
        {
            // populate tree
            var bTreee = new BinaryTree();

            PopulateBinaryTreeWithValues(bTreee);

            bTreee.Print(bTreee.root);


            Console.WriteLine("Max depth of a tree: " + bTreee.maxDepth(bTreee.root));
            Console.WriteLine();

            Console.WriteLine("Lowest Common Ancestor - LCA");
            Node lca = bTreee.findLCA(6, 7);

            if (lca != null)
            {
                Console.WriteLine("LCA(6,7) = " + lca.item);
                Console.WriteLine("Max depth of node {0} = {1}", lca.item, bTreee.maxDepth(lca));
            }
            else
            {
                Console.WriteLine("LCA(4,5) not present");
            }
            Console.WriteLine();

            lca = bTreee.findLCA(4, 5);
            if (lca != null)
            {
                Console.WriteLine("LCA(4,5) = " + lca.item);
                Console.WriteLine("Max depth of node {0} = {1}", lca.item, bTreee.maxDepth(lca));
            }
            else
            {
                Console.WriteLine("LCA(4,5) not present");
            }
            Console.WriteLine();

            lca = bTreee.findLCA(4, 10);
            if (lca != null)
            {
                Console.WriteLine("LCA(4,10) = " + lca.item);
            }
            else
            {
                Console.WriteLine("LCA(4,10) not present");
            }
            Console.WriteLine();

            Console.WriteLine("Print inorder");
            bTreee.PrintInorder(bTreee.root);
            Console.WriteLine(System.Environment.NewLine);

            Console.WriteLine("Print postorder");
            bTreee.PrintPostorder(bTreee.root);
            Console.WriteLine(System.Environment.NewLine);

            var dll = new DoubleLinkedList();

            PopulateDoubleLinkedListWValues(dll);
            Console.Write("Simple Doubly Linked List: ");
            dll.Print(dll.root);
            Console.WriteLine();

            Console.WriteLine("Print Binary Tree converted (Inorder) to Double Linked List");
            var dll2 = new DoubleLinkedList();

            bTreee.ConvertBTreeToDllInorder(bTreee.root, dll2);
            dll2.Print(dll2.root);

            Console.WriteLine("Press any key..");
            Console.ReadKey();
        }