public void TestBSTRemove()
        {
            var tree = new BinarySearchTree<int>() { 40, 11, 62, 43, 34, 16, 10, 63 };

            Assert.IsTrue(tree.Contains(43), "\nChecking if 43 is there.");
            Assert.IsTrue(tree.Remove(43), "\nRemoving 43.");
            Assert.IsFalse(tree.Contains(43), "\nChecking if 43 is still there.");
        }
Esempio n. 2
0
 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'));
 }
Esempio n. 3
0
 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));
     }
 }
        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);
            }
        }
        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 TestBSTContain()
        {
            var tree = new BinarySearchTree<int>() { 40, 11, 62, 43, 34, 16, 10, 63 };

            Assert.IsTrue(tree.Contains(62), "\nFinding 43 in the tree.");
        }
        public void TestBSTRemoveAt()
        {
            var tree = new BinarySearchTree<int>() { 40, 11, 62, 43, 34, 16, 10, 63 };

            Assert.AreEqual(34, tree[3], "Checking that 34 is on the 3. place in the array.");
            tree.RemoveAt(3);
            Assert.IsFalse(tree.Contains(34), "Checking if 34 is in the tree.");
        }
Esempio n. 8
0
 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));
 }