Example #1
0
        static void Tree()
        {
            #region tree Construction
            Tree <int> tree = new Tree <int>(4);

            Node <int> node2 = new Node <int>(8);
            Node <int> node3 = new Node <int>(15);
            Node <int> node4 = new Node <int>(16);
            Node <int> node5 = new Node <int>(23);
            Node <int> node6 = new Node <int>(42);
            Node <int> node7 = new Node <int>(108);

            tree.Root.LeftChild  = node2;
            tree.Root.RightChild = node3;
            node2.LeftChild      = node4;
            node2.RightChild     = node5;
            node3.LeftChild      = node6;
            node3.RightChild     = node7;

            #endregion

            List <int> preOrder = tree.PreOrder(tree.Root);
            Console.WriteLine("======== PRE-ORDER =======");
            Console.WriteLine(string.Join(',', preOrder));
            Console.WriteLine();



            List <int> postOrder = tree.PostOrder(tree.Root);
            Console.WriteLine("======== POST-ORDER =======");
            Console.WriteLine(string.Join(',', postOrder));
            Console.WriteLine();


            List <int> inOrder = tree.InOrder(tree.Root);
            Console.WriteLine("======== IN-ORDER =======");

            Console.WriteLine(string.Join(',', inOrder));
            Console.WriteLine();


            List <int> breadth = tree.BreadthFirst(tree.Root);
            Console.WriteLine("======== BREADTH-FIRST =======");

            Console.WriteLine(string.Join(',', breadth));
        }
        public static void Main(string[] args)
        {
            Node currentNode = null;

            Tree.Insert(ref currentNode, 10);
            Tree.Insert(ref currentNode, 20);
            Tree.Insert(ref currentNode, 15);
            Tree.Insert(ref currentNode, 0);
            Tree.Insert(ref currentNode, 5);

            Console.WriteLine("contains tests:");

            Console.WriteLine($"{Tree.Contains(currentNode, 10) == true} , 10 exists");
            Console.WriteLine($"{Tree.Contains(currentNode, 4) == false} , 4 does not exist");
            Console.WriteLine($"{Tree.Contains(currentNode, 20) == true} , 20 exists");
            Console.WriteLine($"{Tree.Contains(currentNode, 0) == true} , 0 exists");
            Console.WriteLine($"{Tree.Contains(currentNode, 100) == false} , 100 does not exist");

            Console.WriteLine("find min tests:");

            Console.WriteLine($"{Tree.FindMin(currentNode) == 0} , 0 is the minumim");

            Console.WriteLine("find max tests:");

            Console.WriteLine($"{Tree.FindMin(currentNode) == 20} , 20 is the maximum");

            Console.WriteLine("level-order traversal - can be used for printing the tree:");
            Tree.LevelOrder(currentNode);


            Console.WriteLine();
            Console.WriteLine("pre-order traversal");
            Tree.PreOrder(currentNode);


            Console.WriteLine();
            Console.WriteLine("in-order traversal - can be used for sorting the tree:");
            Tree.InOrder(currentNode);


            Console.WriteLine();
            Console.WriteLine("post-order traversal");
            Tree.PostOrder(currentNode);
        }