static void Main(string[] args)
        {
            BinaryTree binaryTree = new BinaryTree(new Node(1));

            binaryTree.Add(new Node(2), binaryTree.Root);
            binaryTree.Add(new Node(3), binaryTree.Root);
            binaryTree.Add(new Node(4), binaryTree.Root);
            binaryTree.Add(new Node(5), binaryTree.Root);

            BinarySearchTree BST = new BinarySearchTree(new Node(50));

            BST.Add(new Node(25), BST.Root);
            BST.Add(new Node(10), BST.Root);
            BST.Add(new Node(75), BST.Root);
            BST.Add(new Node(60), BST.Root);
            BST.Add(new Node(72), BST.Root);
            BST.Add(new Node(80), BST.Root);

            Console.WriteLine("BreadthFirst()");
            //1-2-3-4-5
            binaryTree.BreadthFirst(binaryTree.Root);
            Console.WriteLine("------");
            //50-25-75-10-60-80-72
            BST.BreadthFirst(BST.Root);
            Console.ReadLine();
            Console.Clear();

            Console.WriteLine("PreOrder()");
            //1-2-4-5-3
            binaryTree.PreOrder(binaryTree.Root);
            Console.WriteLine("------");
            //50-25-10-75-60-72-80
            BST.PreOrder(BST.Root);
            Console.ReadLine();
            Console.Clear();

            Console.WriteLine("InOrder()");
            //4-2-5-1-3
            binaryTree.InOrder(binaryTree.Root);
            Console.WriteLine("------");
            //10-25-50-60-72-75-80
            BST.InOrder(BST.Root);
            Console.ReadLine();
            Console.Clear();

            Console.WriteLine("PostOrder()");
            //4-5-2-3-1
            binaryTree.PostOrder(binaryTree.Root);
            Console.WriteLine("------");
            //10-25-72-60-80-75-50
            BST.PostOrder(BST.Root);
        }
        static void Main(string[] args)
        {
            BinaryTree bt = new BinaryTree();

            Node rootNode = new Node(1);

            bt.Add(rootNode, new Node(2));
            bt.Add(rootNode, new Node(3));
            bt.Add(rootNode, new Node(4));
            bt.Add(rootNode, new Node(5));

            Console.WriteLine("PreOrder: 1, 2, 4, 5, 3");
            bt.PreOrder(rootNode);

            Console.WriteLine("InOrder: 4, 2, 5, 1, 3");
            bt.InOrder(rootNode);

            Console.WriteLine("PostOrder: 4, 5, 2, 3, 1");
            bt.PostOrder(rootNode);

            Console.WriteLine("Breadth First: 1, 2, 3, 4, 5");
            bt.BreadthFirst(rootNode);

            Console.WriteLine("Press ENTER for BST Methods");
            Console.ReadLine();
            Console.Clear();

            BinarySearchTree bst     = new BinarySearchTree();
            Node             newRoot = new Node(10);

            bst.Add(newRoot, new Node(5));
            bst.Add(newRoot, new Node(15));
            bst.Add(newRoot, new Node(3));
            bst.Add(newRoot, new Node(17));
            bst.Add(newRoot, new Node(7));
            bst.Add(newRoot, new Node(13));

            Console.WriteLine("PreOrder: 10, 5, 3, 7, 15, 13, 17");
            bst.PreOrder(newRoot);

            Console.WriteLine("InOrder: 3, 5, 7, 10, 13, 15, 17");
            bst.InOrder(newRoot);

            Console.WriteLine("PostOrder: 3, 7, 5, 13, 17, 15, 10");
            bst.PostOrder(newRoot);

            Console.WriteLine("BreadthFirst: 10, 5, 15, 3, 7, 13, 17");
            bst.BreadthFirst(newRoot);
            Console.ReadLine();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            //declare and define the tree root for both
            Node treeRoot = new Node(20);

            //create new Binary Search Tree
            BinarySearchTree binarySearch = new BinarySearchTree();

            //create new binary tree
            BinaryTree biTree = new BinaryTree();

            //add to binary search tree
            Console.WriteLine("adding some nodes to binary search tree");
            binarySearch.Add(treeRoot, new Node(17));
            binarySearch.Add(treeRoot, new Node(15));
            binarySearch.Add(treeRoot, new Node(10));
            binarySearch.Add(treeRoot, new Node(9));
            binarySearch.Add(treeRoot, new Node(5));
            binarySearch.Add(treeRoot, new Node(3));

            //add to binary tree
            Console.WriteLine("adding some nodes to binary tree");
            biTree.Add(treeRoot, new Node(3));
            biTree.Add(treeRoot, new Node(5));
            biTree.Add(treeRoot, new Node(9));
            biTree.Add(treeRoot, new Node(10));
            biTree.Add(treeRoot, new Node(15));
            biTree.Add(treeRoot, new Node(17));

            // expected output 20, 3, 5, 9, 10, 15, 17
            binarySearch.PreOrder(treeRoot);
            biTree.PreOrder(treeRoot);


            // expected output
            biTree.InOrder(treeRoot);

            // expected output
            biTree.PostOrder(treeRoot);

            // expected output
            biTree.BreadthFirst(treeRoot);
        }
        static void Main(string[] args)
        {
            BinarySearchTree testTree = new BinarySearchTree();

            testTree.Add(100);
            testTree.Add(50);
            testTree.Add(200);
            testTree.Add(25);
            testTree.Add(75);
            testTree.Add(150);
            testTree.Add(300);
            testTree.Add(60);
            testTree.Add(90);
            testTree.Add(342);
            Console.WriteLine(testTree.Contains(90));
            Console.WriteLine(testTree.Contains(1));
            IList <int> results = testTree.PreOrder(testTree.Root);

            foreach (int item in results)
            {
                Console.WriteLine(item);
            }
        }
Ejemplo n.º 5
0
        public static void BinarySearchTree()
        {
            Console.Clear();
            Console.WriteLine("This is a Binary Search Tree");
            Node             n1 = new Node(15);
            Node             n2 = new Node(20);
            Node             n3 = new Node(10);
            Node             n4 = new Node(70);
            Node             n5 = new Node(30);
            Node             n6 = new Node(40);
            Node             n7 = new Node(50);
            Node             n8 = new Node(5);
            BinarySearchTree binarySearchTree = new BinarySearchTree(n1);

            binarySearchTree.Add(n1, n2);
            binarySearchTree.Add(n1, n3);
            binarySearchTree.Add(n1, n4);
            binarySearchTree.Add(n1, n5);
            binarySearchTree.Add(n1, n6);
            binarySearchTree.Add(n1, n7);
            binarySearchTree.Add(n1, n8);

            Console.Clear();
            Console.WriteLine("This is BreadthFirst:");
            Console.WriteLine("The order should be:");
            Console.WriteLine("15 -> 10 -> 20 -> 5 -> 70 -> 30 -> 40 -> 50");
            Console.WriteLine("The traversal starts now:");
            // 15 -> 10 -> 20 -> 5 -> 70 -> 30 -> 40 -> 50 ->
            binarySearchTree.BreadthFirst(n1);
            Console.ReadLine();

            Console.Clear();
            Console.WriteLine("This is PreOrder:");
            Console.WriteLine("The order should be:");
            Console.WriteLine("15 -> 10 -> 5 -> 20 -> 70 -> 30 -> 40 -> 50");
            Console.WriteLine("The traversal starts now:");
            // 15 -> 10 -> 5 -> 20 -> 70 -> 30 -> 40 -> 50
            binarySearchTree.PreOrder(n1);
            Console.ReadLine();

            Console.Clear();
            Console.WriteLine("This is InOrder");
            Console.WriteLine("The order should be:");
            Console.WriteLine("5 -> 10 -> 15 -> 20 -> 30 -> 40 -> 50 -> 70");
            Console.WriteLine("The traversal starts now:");
            // 5 -> 10 -> 15 -> 20 -> 30 -> 40 -> 50 -> 70
            binarySearchTree.InOrder(n1);
            Console.ReadLine();

            Console.Clear();
            Console.WriteLine("This is PostOrder:");
            Console.WriteLine("The order should be:");
            Console.WriteLine("5 -> 10 -> 50 -> 40 -> 30 -> 70 -> 20 -> 15");
            Console.WriteLine("The traversal starts now:");
            // 5 -> 10 -> 50 -> 40 -> 30 -> 70 -> 20 -> 15
            binarySearchTree.PostOrder(n1);
            Console.ReadLine();

            Console.Clear();
            Console.WriteLine("Here is a search for a value in the tree");
            try
            {
                Node node = binarySearchTree.Search(n1, 20);
                Console.WriteLine(node.Value);
                Console.ReadLine();
                Console.WriteLine("Here is a search for a value not in the tree");
                Node node2 = binarySearchTree.Search(n1, 25);
                Console.WriteLine(node2.Value);
            }
            catch (NullReferenceException)
            {
                Console.WriteLine("A null reference exception was thrown");
                Console.ReadLine();
            }
        }