public void TestContains()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     Assert.IsFalse(tree.Contains('c'));
     tree.Add('c');
     Assert.IsTrue(tree.Contains('c'));
     Assert.IsFalse(tree.Contains('b'));
 }
 public void TestAdd()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     for(int i = 0; i < 50; i++)  {
         tree.Add(i);
     }
     for(int i = 0; i < 50; i++)  {
         Assert.IsTrue(tree.Contains(i));
     }
 }
 public void TestClear()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     //count is 0 after clear
     for (int i = 0; i < 10; i++)
     {
         tree.Add(i);
     }
     tree.Clear();
     Assert.IsTrue(tree.Count == 0);
 }
        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);
            }
        }
        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();
        }
 public void TestCopyTo()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     int[] array = new int[0];
     tree.CopyTo(array, 0);
     Assert.IsTrue(array.Length == 0);
     //basic copy
     for (int i = 0; i < 10; i++)
     {
         tree.Add(i);
     }
     array = new int[10];
     int startIndex = 0;
     tree.CopyTo(array, startIndex);
     foreach (int curr in tree)
     {
         Assert.IsTrue(curr == array[startIndex]);
         startIndex++;
     }
     //copy at later position in middle
     array = new int[25];
     startIndex = 10;
     tree.CopyTo(array, startIndex);
     foreach (int curr in tree)
     {
         Assert.IsTrue(curr == array[startIndex]);
         startIndex++;
     }
     //copy to final positions of array.
     array = new int[25];
     startIndex = 15;
     tree.CopyTo(array, startIndex);
     foreach (int curr in tree)
     {
         Assert.IsTrue(curr == array[startIndex]);
         startIndex++;
     }
 }
Exemple #8
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);
        }
Exemple #9
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();
            }
        }
Exemple #10
0
        static void Main(string[] args)
        {
            // create the tree
            Node root            = new Node(1);
            Node leftChild       = new Node(2);
            Node rightChild      = new Node(3);
            Node LeftChildLeft   = new Node(4);
            Node LeftChildRight  = new Node(5);
            Node RightChildLeft  = new Node(6);
            Node RightChildRight = new Node(7);

            root.Left        = leftChild;
            root.Right       = rightChild;
            leftChild.Left   = LeftChildLeft;
            leftChild.Right  = LeftChildRight;
            rightChild.Left  = RightChildLeft;
            rightChild.Right = RightChildRight;

            BinaryTree bt = new BinaryTree(root);

            // Prints preorder
            WriteLine("======== Pre-order ========");
            bt.PreOrder();
            WriteLine("======== In-order ========");
            bt.InOrder();
            WriteLine("======== Post-order ========");
            bt.PostOrder();

            WriteLine("Adding bst with values 3, 1, 2 in that order.");
            // Creates a new bst
            BinarySearchTree bst = new BinarySearchTree(new Node(3));

            bst.Add(new Node(1));
            bst.Add(new Node(2));

            WriteLine("Searching bst for 5, should not be found");
            Node result = bst.Search(5);

            if (result == null)
            {
                WriteLine("5 was not found");
            }

            WriteLine("done");

            root       = new Node(1);
            leftChild  = new Node(2);
            rightChild = new Node(3);

            root.Left  = leftChild;
            root.Right = rightChild;

            bt = new BinaryTree(root);

            bool resultEnd = true;

            List <Node> listBack = bt.PostOrder();

            int[] expected = { 2, 3, 1 };
            for (int i = 0; i < expected.Length; i++)
            {
                WriteLine((int)(listBack[i].Value));
                if (expected[i] != (int)listBack[i].Value)
                {
                    resultEnd = false;
                }
            }

            WriteLine(resultEnd);
        }
Exemple #11
0
        public static void Main(string[] args)
        {
            // Declare placeholder variables - used later by various methods
            string menuSelection = "", newNodeInput = "", nodeSearchValue = "";

            // Instantiate BinaryTree and BinarySearchTree objects, add initial values
            BinaryTree binTree = new BinaryTree(new Node(25));

            binTree.Add(binTree.Root, new Node(10));
            binTree.Add(binTree.Root, new Node(5));
            binTree.Add(binTree.Root, new Node(7));
            binTree.Add(binTree.Root, new Node(1));
            binTree.Add(binTree.Root, new Node(40));
            binTree.Add(binTree.Root, new Node(50));
            binTree.Add(binTree.Root, new Node(30));

            BinarySearchTree binSearchTree = new BinarySearchTree(new Node(25));

            binSearchTree.Add(binSearchTree.Root, new Node(10));
            binSearchTree.Add(binSearchTree.Root, new Node(5));
            binSearchTree.Add(binSearchTree.Root, new Node(7));
            binSearchTree.Add(binSearchTree.Root, new Node(1));
            binSearchTree.Add(binSearchTree.Root, new Node(40));
            binSearchTree.Add(binSearchTree.Root, new Node(50));
            binSearchTree.Add(binSearchTree.Root, new Node(30));

            // Loop until the user enters the "8" key to exit the application
            do
            {
                // Prompt user to select an option from the menu
                PrintMainMenu();
                menuSelection = Console.ReadLine();
                Console.Clear();

                switch (menuSelection)
                {
                case "1":     // Adds a Node to the Binary Tree
                    Console.WriteLine("What value you like add to the Binary Tree?");
                    newNodeInput = Console.ReadLine();
                    Console.Clear();

                    if (int.TryParse(newNodeInput, out int binTree_Add))
                    {
                        binTree.Add(binTree.Root, new Node(binTree_Add));
                        Console.WriteLine("Success!");
                    }
                    else
                    {
                        Console.WriteLine("Sorry, unable to add that value to the Binary Tree.");
                    }

                    PromptToReturnToMainMenu();
                    break;

                case "2":     // Adds a Node to the Binary Search Tree
                    Console.WriteLine("What value you like add to the Binary Search Tree?");
                    newNodeInput = Console.ReadLine();
                    Console.Clear();

                    if (int.TryParse(newNodeInput, out int binSearchTree_Add))
                    {
                        binSearchTree.Add(binSearchTree.Root, new Node(binSearchTree_Add));
                        Console.WriteLine("Success!");
                    }
                    else
                    {
                        Console.WriteLine("Sorry, unable to add that value to the Binary Search Tree.");
                    }

                    PromptToReturnToMainMenu();
                    break;

                case "3":     // Searches for a value in the Binary Tree
                    Console.WriteLine("What value you like search for in the Binary Tree?");
                    nodeSearchValue = Console.ReadLine();
                    Console.Clear();

                    if (int.TryParse(nodeSearchValue, out int binTree_Search))
                    {
                        Node foundBinTreeNode = binTree.Search(binTree.Root, binTree_Search);

                        if (foundBinTreeNode != null)
                        {
                            Console.WriteLine("Found!");
                        }
                        else
                        {
                            Console.WriteLine("That value does not exist within the Binary Tree");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Sorry, please input an integer to search for.");
                    }

                    PromptToReturnToMainMenu();
                    break;

                case "4":     // Searches for a value in the Binary Search Tree
                    Console.WriteLine("What value you like search for in the Binary Search Tree?");
                    nodeSearchValue = Console.ReadLine();
                    Console.Clear();

                    if (int.TryParse(nodeSearchValue, out int binSearchTree_Search))
                    {
                        Node foundBinSearchTreeNode = binSearchTree.Search(binSearchTree.Root, binSearchTree_Search);

                        if (foundBinSearchTreeNode != null)
                        {
                            Console.WriteLine("Found!");
                        }
                        else
                        {
                            Console.WriteLine("That value does not exist within the Binary Search Tree");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Sorry, please input an integer to search for.");
                    }

                    PromptToReturnToMainMenu();
                    break;

                case "5":     // Prints the values in the Binary Tree in "Preorder" sequence
                    binTree.PreOrder(binTree.Root);

                    PromptToReturnToMainMenu();
                    break;

                case "6":     // Prints the values in the Binary Tree in "Postorder" sequence
                    binTree.PostOrder(binTree.Root);

                    PromptToReturnToMainMenu();
                    break;

                case "7":     // Prints the values in the Binary Tree in "Inorder" sequence
                    binTree.InOrder(binTree.Root);

                    PromptToReturnToMainMenu();
                    break;

                case "8":     // Prints the values in the Binary Tree in "Breadth First" sequence
                    binTree.BreadthFirst(binTree.Root);

                    PromptToReturnToMainMenu();
                    break;

                case "9":     // Exits the Program
                    Environment.Exit(0);
                    break;

                default:     // Handles cases where user doesn't enter a valid menu option
                    Console.WriteLine("That did not match one of the menu options. Try again.\n");
                    break;
                }
            } while (menuSelection != "9");
        }
Exemple #12
0
        static void Main(string[] args)
        {
            // var node = new Node(33);
            // node.Left = new Node(16).Left = new Node(13).Right = new Node(15);
            // node.Left.Right.Left = new Node(17);
            // node.Left.Right.Right = new Node(25);
            // node.Left.Right.Right.Left = new Node(19);
            // node.Left.Right.Right.Right = new Node(21);

            // node.Right = new Node(50).Right = new Node(58).Right = new Node(66);
            // node.Right.Left = new Node(34);
            // node.Right.Right.Left = new Node(51).Right = new Node(55);

            var trees = new BinarySearchTree();

            trees.Insert(33);
            trees.Insert(16);
            trees.Insert(13);
            trees.Insert(18);
            trees.Insert(15);
            trees.Insert(17);
            trees.Insert(25);
            trees.Insert(19);
            trees.Insert(21);
            trees.Insert(50);
            trees.Insert(34);
            trees.Insert(58);
            trees.Insert(51);
            trees.Insert(55);
            trees.Insert(66);

            trees.Delete(18);

            var trees1 = new BinarySearchTree <int>();

            trees1.Add(33);
            trees1.Add(16);
            trees1.Add(13);
            trees1.Add(18);
            trees1.Add(15);
            trees1.Add(17);
            trees1.Add(25);
            trees1.Add(19);
            trees1.Add(21);
            trees1.Add(50);
            trees1.Add(34);
            trees1.Add(58);
            trees1.Add(51);
            trees1.Add(55);
            trees1.Add(66);

            trees1.Remove(13);
            Console.WriteLine("Hello World!");
        }
        public static void TreeCarlos()
        {
            Node nodeOne   = new Node(1);
            Node nodeTwo   = new Node(2);
            Node nodeThree = new Node(3);
            Node nodeFour  = new Node(4);
            Node nodeFive  = new Node(5);
            Node nodeSix   = new Node(6);
            Node nodeSeven = new Node(7);
            Node nodeEight = new Node(8);
            Node nodeNine  = new Node(9);
            Node nodeTen   = new Node(10);

            BinaryTree binaryTree = new BinaryTree(nodeOne);

            binaryTree.Root.LeftChild             = nodeTwo;
            binaryTree.Root.RightChild            = nodeThree;
            binaryTree.Root.LeftChild.LeftChild   = nodeFour;
            binaryTree.Root.LeftChild.RightChild  = nodeFive;
            binaryTree.Root.RightChild.LeftChild  = nodeSix;
            binaryTree.Root.RightChild.RightChild = nodeSeven;

            Console.WriteLine(" ");
            Console.WriteLine("==========PreOrder=========");
            List <int> preOrder = binaryTree.PreOrder(binaryTree.Root);

            foreach (var item in preOrder)
            {
                Console.Write(item);
            }

            binaryTree.values.Clear();
            Console.WriteLine(" ");
            Console.WriteLine("==========InOrder=========");
            List <int> inOrder = binaryTree.InOrder(binaryTree.Root);

            foreach (var item in inOrder)
            {
                Console.Write(item);
            }

            binaryTree.values.Clear();
            Console.WriteLine(" ");
            Console.WriteLine("==========PostOrder=========");
            List <int> postOrder = binaryTree.PostOrder(binaryTree.Root);

            foreach (var item in postOrder)
            {
                Console.Write(item);
            }

            binaryTree.values.Clear();
            Console.WriteLine(" ");
            Console.WriteLine("==========Add Binary Search Tree=========");
            Console.WriteLine("Added 2, 3, 4, 6, 7, 8, 10");
            BinarySearchTree binarySearchTree = new BinarySearchTree();

            binarySearchTree.Add(nodeSix, 3);
            binarySearchTree.Add(nodeSix, 2);
            binarySearchTree.Add(nodeSix, 4);
            binarySearchTree.Add(nodeSix, 8);
            binarySearchTree.Add(nodeSix, 7);
            binarySearchTree.Add(nodeSix, 10);
            Console.WriteLine("==========Contains Binary Search Tree=========");
            Console.WriteLine($"Contains 7: {binarySearchTree.Contains(nodeSix, 4)}");
            Console.WriteLine($"Contains 52: {binarySearchTree.Contains(nodeSix, 52)}");
        }
 public void TestCopyToSmallArray()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     for (int i = 0; i < 10; i++)
     {
         tree.Add(i);
     }
     int[] array = new int[5];
     //try copying to array that is too small
     try
     {
         tree.CopyTo(array, 0);
         Assert.Fail("Should fail, array too small");
     }
     catch (Exception e)
     {
         Assert.IsTrue(e is ArgumentException);
     }
     //try copying too close to the end of the array.
     array = new int[25];
     try
     {
         tree.CopyTo(array, 20);
         Assert.Fail("Should fail, Not enough room to copy");
     }
     catch (Exception e)
     {
         Assert.IsTrue(e is ArgumentException);
     }
 }
        public static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;

            BinarySearchTree <int> tree = new BinarySearchTree <int>();

            tree.Root = new BinaryTreeNode <int>()
            {
                Data = 100
            };
            tree.Root.Left = new BinaryTreeNode <int>()
            {
                Data = 50, Parent = tree.Root
            };
            tree.Root.Right = new BinaryTreeNode <int>()
            {
                Data = 150, Parent = tree.Root
            };
            tree.Count = 3;
            VisualizeTree(tree, "The BST with three nodes (50, 100, 150):");

            tree.Add(75);
            tree.Add(125);
            VisualizeTree(tree, "The BST after adding two nodes (75, 125):");

            tree.Add(25);
            tree.Add(175);
            tree.Add(90);
            tree.Add(110);
            tree.Add(135);
            VisualizeTree(tree, "The BST after adding five nodes (25, 175, 90, 110, 135):");

            tree.Remove(25);
            VisualizeTree(tree, "The BST after removing the node 25:");

            tree.Remove(50);
            VisualizeTree(tree, "The BST after removing the node 50:");

            tree.Remove(100);
            VisualizeTree(tree, "The BST after removing the node 100:");

            Console.Write("Pre-order traversal:\t");
            Console.Write(string.Join(", ", tree.Traverse(TraversalEnum.PREORDER).Select(n => n.Data)));

            Console.Write("\nIn-order traversal:\t");
            Console.Write(string.Join(", ", tree.Traverse(TraversalEnum.INORDER).Select(n => n.Data)));

            Console.Write("\nPost-order traversal:\t");
            Console.Write(string.Join(", ", tree.Traverse(TraversalEnum.POSTORDER).Select(n => n.Data)));

            tree.Remove(90);
            VisualizeTree(tree, "---");

            tree.Remove(75);
            VisualizeTree(tree, "---");

            tree.Remove(125);
            VisualizeTree(tree, "---");

            tree.Remove(110);
            VisualizeTree(tree, "---");

            tree.Remove(175);
            VisualizeTree(tree, "---");

            tree.Remove(135);
            VisualizeTree(tree, "---");

            tree.Remove(150);
            VisualizeTree(tree, "---");
            Console.ReadLine();
        }
 public void TestBSTAdd()
 {
     var tree = new BinarySearchTree<int>() { 40, 11, 62, 43, 34, 16, 10, 63 };
     tree.Add(3);
     Assert.AreEqual(9, tree.Count, "\nCounting the elements");
 }
 public void TestCount()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     Assert.IsTrue(tree.Count == 0);
     tree.Add(10);
     Assert.IsTrue(tree.Count == 1);
     for (int i = 100; i < 200; i++)
     {
         tree.Add(i);
     }
     Assert.IsTrue(tree.Count == 101);
     tree.Remove(10);
     Assert.IsTrue(tree.Count == 100);
     for (int i = 100; i < 200; i++)
     {
         tree.Remove(i);
     }
     Assert.IsTrue(tree.Count == 0);
     //count isnt decremented if unsuccessfully try to remove an item.
     tree.Remove(10);
     Assert.IsTrue(tree.Count == 0);
 }
 public void TestCopyToInvalidIndex()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     for (int i = 0; i < 10; i++)
     {
         tree.Add(i);
     }
     int[] array = new int[10];
     //Try to set start index outside of max size of array.
     try
     {
         tree.CopyTo(array, 11);
         Assert.Fail("Should fail, index is larger than size of array.");
     }
     catch (Exception e)
     {
         Assert.IsTrue(e is ArgumentOutOfRangeException);
     }
     //try to copy to start index below 0
     try
     {
         tree.CopyTo(array, -2);
         Assert.Fail("Should fail, index is below 0");
     }
     catch (Exception e)
     {
         Assert.IsTrue(e is ArgumentOutOfRangeException);
     }
 }
 public void TestIterator()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     //empty tree should give no iterations.
     foreach (int curr in tree)
     {
         Assert.Fail("Should be empty.");
     }
     for(int i = 0; i < 100; i++)  {
         tree.Add(i);
     }
     int element = 0;
     foreach(int curr in tree) {
         Assert.AreEqual(element, curr);
         element++;
     }
     //remove root element, an item with 1 child in the middle and a leaf with no children at bottom. Recheck everything iterates in order.
     tree.Remove(0);
     tree.Remove(50);
     tree.Remove(100);
     element = 1;
     foreach (int curr in tree)
     {
         Assert.AreEqual(element, curr);
         if (element == 49)
             element += 2;
         else
             element++;
     }
     //re add 50 so that 51 now has child on left of 50 and child right of 52.
     tree.Add(50);
     element = 1;
     foreach (int curr in tree)
     {
         Assert.AreEqual(element, curr);
         element++;
     }
     //remove node with 2 children, check iterates correctly still.
     tree.Remove(51);
     element = 1;
     foreach (int curr in tree)
     {
         Assert.AreEqual(element, curr);
         if (element == 50)
             element += 2;
         else
             element++;
     }
 }
        public void testMassAddRemove()
        {
            BinarySearchTree<int> tree = new BinarySearchTree<int>();
            List<int> list = new List<int>();
            Random r = new Random();
            int numberToAdd = r.Next(1000000);
            for (int i = 0; i < 1000; i++)
            {
                while (list.Contains(numberToAdd))
                {
                    numberToAdd = r.Next(1000000);
                }
                list.Add(numberToAdd);
                tree.Add(numberToAdd);
            }
            Assert.IsTrue(list.Count == tree.Count && list.Count == 1000);
            list.Sort();
            Assert.IsTrue(Enumerable.SequenceEqual(list, tree));
            int curr;
            for (int i = 0; i < list.Count; i++)
            {
                curr = list.ElementAt(0);
                Assert.IsTrue(list.Remove(curr));
                Assert.IsTrue(tree.Remove(curr));
            }
            Assert.IsTrue(Enumerable.SequenceEqual(list, tree));

            numberToAdd = r.Next(1000000);
            for (int i = 0; i < 1000; i++)
            {
                while (list.Contains(numberToAdd))
                {
                    numberToAdd = r.Next(1000000);
                }
                list.Add(numberToAdd);
                tree.Add(numberToAdd);
                if (i % 2 == 0)
                {
                    curr = list.ElementAt(r.Next(list.Count));
                    Assert.IsTrue(list.Remove(curr));
                    Assert.IsTrue(tree.Remove(curr));
                }
            }
            list.Sort();
            Assert.IsTrue(Enumerable.SequenceEqual(list, tree));
        }
 public void TestRemove()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     //fails to remove item from empty list.
     Assert.IsFalse(tree.Remove(10));
     tree.Add(10);
     tree.Add(12);
     Assert.IsTrue(tree.Remove(10));
     Assert.IsFalse(tree.Contains(10));
     Assert.IsFalse(tree.Remove(10));
 }
 public void TestAddDuplicate()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     tree.Add(10);
     tree.Add(10);
 }