private void PrepareTree1(ITree <IBinaryTreeNode <int, int>, int, int> tree)
        {
            //Tree with just a root
            //
            //     (1)
            BinaryTreeNode <int, int> root = new BinaryTreeNode <int, int>(null, 1, 1);

            tree.Root = root;

            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);
        }
        private void TestTree1(ITree <IBinaryTreeNode <int, int>, int, int> tree)
        {
            this.PrepareTree1(tree);

            //Remove Root
            tree.Remove(1);
            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);

            //Should lead to empty tree
            Assert.AreEqual(0, tree.Nodes.Count());
            Assert.IsNull(tree.Root);
        }
Exemplo n.º 3
0
        public void BinaryTreeUnbalancedDelete3()
        {
            UnbalancedBinaryTree <int, int> tree = this.GetTreeForDelete();

            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);
            tree.Remove(2);
            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);

            this.TestOrderStructs <int>(new List <int> {
                1, 3
            }, tree.Keys.ToList());
        }
        private void TestTree3b(ITree <IBinaryTreeNode <int, int>, int, int> tree)
        {
            this.PrepareTree3(tree);

            //Remove Right Child
            tree.Remove(2);
            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);

            //Should just leave Root
            Assert.AreEqual(1, tree.Nodes.Count());
            Assert.AreEqual(1, tree.Root.Value);
            Assert.IsNull(tree.Root.RightChild);
        }
        private void TestTree2a(ITree <IBinaryTreeNode <int, int>, int, int> tree)
        {
            this.PrepareTree2(tree);

            //Remove Root
            tree.Remove(2);
            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);

            //Should lead to Left Child as Root
            Assert.AreEqual(1, tree.Nodes.Count());
            Assert.AreEqual(1, tree.Root.Value);
            Assert.IsNull(tree.Root.LeftChild);
        }
Exemplo n.º 6
0
        public void BinaryTreeUnbalancedDelete2()
        {
            UnbalancedBinaryTree <int, int> tree = this.GetTreeForDelete();

            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);
            tree.Remove(3);
            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);
            Assert.IsNull(tree.Root.RightChild, "Right Child should now be null");

            this.TestOrderStructs <int>(new List <int> {
                1, 2
            }, tree.Keys.ToList());
        }
        private void TestTree8b(ITree <IBinaryTreeNode <int, int>, int, int> tree)
        {
            this.PrepareTree8(tree);

            //Remove Right Child
            tree.Remove(3);
            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);

            //Should move left child of right child to right child
            Assert.AreEqual(2, tree.Nodes.Count());
            Assert.AreEqual(1, tree.Root.Value);
            Assert.IsNotNull(tree.Root.RightChild);
            Assert.AreEqual(2, tree.Root.RightChild.Value);
        }
        private void TestTree7a(ITree <IBinaryTreeNode <int, int>, int, int> tree)
        {
            this.PrepareTree7(tree);

            //Remove Root
            tree.Remove(1);
            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);

            //Should move Right Child to Root
            Assert.AreEqual(2, tree.Nodes.Count());
            Assert.AreEqual(2, tree.Root.Value);
            Assert.IsNotNull(tree.Root.RightChild);
            Assert.AreEqual(3, tree.Root.RightChild.Value);
        }
        private void TestTree5b(ITree <IBinaryTreeNode <int, int>, int, int> tree)
        {
            this.PrepareTree5(tree);

            //Remove Left Inner Child
            tree.Remove(2);
            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);

            //Should leave Leaf as Left Child of Root
            Assert.AreEqual(2, tree.Nodes.Count());
            Assert.AreEqual(3, tree.Root.Value);
            Assert.IsNotNull(tree.Root.LeftChild);
            Assert.AreEqual(1, tree.Root.LeftChild.Value);
        }
        private void TestTree8c(ITree <IBinaryTreeNode <int, int>, int, int> tree)
        {
            this.PrepareTree8(tree);

            //Remove Left Child
            tree.Remove(2);
            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);

            //Should leave Root and Right Child as-is
            Assert.AreEqual(2, tree.Nodes.Count());
            Assert.AreEqual(1, tree.Root.Value);
            Assert.IsNotNull(tree.Root.RightChild);
            Assert.AreEqual(3, tree.Root.RightChild.Value);
        }
        private void TestTree10c(ITree <IBinaryTreeNode <int, int>, int, int> tree)
        {
            this.PrepareTree10(tree);

            //Remove Right Left of left subtree should leave root and immediate children as=is
            tree.Remove(2);
            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);

            //Should leave Root and immediate children as-is
            Assert.AreEqual(3, tree.Nodes.Count());
            Assert.AreEqual(3, tree.Root.Value);
            Assert.IsNotNull(tree.Root.LeftChild);
            Assert.AreEqual(1, tree.Root.LeftChild.Value);
            Assert.IsNotNull(tree.Root.RightChild);
            Assert.AreEqual(4, tree.Root.RightChild.Value);
        }
        private void TestTree10b(ITree <IBinaryTreeNode <int, int>, int, int> tree)
        {
            this.PrepareTree10(tree);

            //Remove Left Inner Child
            tree.Remove(1);
            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);

            //Should leave Root and Right Child as-is, Right Leaf of left subtree should move up
            Assert.AreEqual(3, tree.Nodes.Count());
            Assert.AreEqual(3, tree.Root.Value);
            Assert.IsNotNull(tree.Root.LeftChild);
            Assert.AreEqual(2, tree.Root.LeftChild.Value);
            Assert.IsNotNull(tree.Root.RightChild);
            Assert.AreEqual(4, tree.Root.RightChild.Value);
        }
        private void PrepareTree3(ITree <IBinaryTreeNode <int, int>, int, int> tree)
        {
            //Tree with a root and a left child
            //
            //     (2)
            //    /
            //  (1)
            BinaryTreeNode <int, int> root       = new BinaryTreeNode <int, int>(null, 1, 1);
            BinaryTreeNode <int, int> rightChild = new BinaryTreeNode <int, int>(null, 2, 2);

            root.RightChild = rightChild;

            tree.Root = root;

            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);
        }
        private void TestTree10a(ITree <IBinaryTreeNode <int, int>, int, int> tree)
        {
            this.PrepareTree10(tree);

            //Remove Root
            tree.Remove(3);
            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);

            //Should lead to Rightmost Child of Left subtree to Root with Right Child as-is
            Assert.AreEqual(3, tree.Nodes.Count());
            Assert.AreEqual(2, tree.Root.Value);
            Assert.IsNotNull(tree.Root.LeftChild);
            Assert.AreEqual(1, tree.Root.LeftChild.Value);
            Assert.IsNotNull(tree.Root.RightChild);
            Assert.AreEqual(4, tree.Root.RightChild.Value);
        }
        private void PrepareTree4(ITree <IBinaryTreeNode <int, int>, int, int> tree)
        {
            //Tree with a root and two children
            //
            //     (2)
            //    /   \
            //  (1)    (3)
            BinaryTreeNode <int, int> root       = new BinaryTreeNode <int, int>(null, 2, 2);
            BinaryTreeNode <int, int> leftChild  = new BinaryTreeNode <int, int>(null, 1, 1);
            BinaryTreeNode <int, int> rightChild = new BinaryTreeNode <int, int>(null, 3, 3);

            root.LeftChild  = leftChild;
            root.RightChild = rightChild;

            tree.Root = root;

            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);
        }
        private void PrepareTree8(ITree <IBinaryTreeNode <int, int>, int, int> tree)
        {
            //Tree with a root and a right child which has a left child
            //
            //     (1)
            //       \ 
            //       (3)
            //       /
            //     (2)
            BinaryTreeNode <int, int> root       = new BinaryTreeNode <int, int>(null, 1, 1);
            BinaryTreeNode <int, int> rightChild = new BinaryTreeNode <int, int>(null, 3, 3);
            BinaryTreeNode <int, int> leftChild  = new BinaryTreeNode <int, int>(null, 2, 2);

            rightChild.LeftChild = leftChild;
            root.RightChild      = rightChild;

            tree.Root = root;

            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);
        }
        private void PrepareTree5(ITree <IBinaryTreeNode <int, int>, int, int> tree)
        {
            //Tree with a root and two left children
            //
            //     (3)
            //    /
            //  (2)
            //  /
            //(1)
            BinaryTreeNode <int, int> root           = new BinaryTreeNode <int, int>(null, 3, 3);
            BinaryTreeNode <int, int> leftInnerChild = new BinaryTreeNode <int, int>(null, 2, 2);
            BinaryTreeNode <int, int> leftLeafChild  = new BinaryTreeNode <int, int>(null, 1, 1);

            leftInnerChild.LeftChild = leftLeafChild;
            root.LeftChild           = leftInnerChild;

            tree.Root = root;

            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);
        }
        private void PrepareTree10(ITree <IBinaryTreeNode <int, int>, int, int> tree)
        {
            //Tree with a root and two children, left child has a right child
            //
            //     (3)
            //    /   \
            //  (1)    (4)
            //    \
            //    (2)
            BinaryTreeNode <int, int> root           = new BinaryTreeNode <int, int>(null, 3, 3);
            BinaryTreeNode <int, int> leftChild      = new BinaryTreeNode <int, int>(null, 1, 1);
            BinaryTreeNode <int, int> rightLeafChild = new BinaryTreeNode <int, int>(null, 2, 2);
            BinaryTreeNode <int, int> rightChild     = new BinaryTreeNode <int, int>(null, 4, 4);

            leftChild.RightChild = rightLeafChild;
            root.LeftChild       = leftChild;
            root.RightChild      = rightChild;

            tree.Root = root;

            BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree);
        }