public void IsLeaf()
        {
            var A = new MockBinaryTreeNode <int, string>(50, "A");
            var B = new MockBinaryTreeNode <int, string>(20, "B");
            var C = new MockBinaryTreeNode <int, string>(10, "C");
            var D = new MockBinaryTreeNode <int, string>(40, "D");
            var E = new MockBinaryTreeNode <int, string>(30, "E");

            A.Parent     = null;
            A.LeftChild  = B;
            A.RightChild = null;

            B.Parent     = A;
            B.LeftChild  = C;
            B.RightChild = D;

            C.Parent     = B;
            C.LeftChild  = null;
            C.RightChild = null;

            D.Parent     = B;
            D.LeftChild  = E;
            D.RightChild = null;

            E.Parent     = D;
            E.LeftChild  = null;
            E.RightChild = null;

            Assert.IsFalse(A.IsLeaf());
            Assert.IsFalse(B.IsLeaf());
            Assert.IsTrue(C.IsLeaf());
            Assert.IsFalse(D.IsLeaf());
            Assert.IsTrue(E.IsLeaf());
        }
        public void GetUncle()
        {
            var A = new MockBinaryTreeNode <int, string>(8, "A");
            var B = new MockBinaryTreeNode <int, string>(2, "B");
            var C = new MockBinaryTreeNode <int, string>(10, "C");
            var D = new MockBinaryTreeNode <int, string>(9, "D");

            A.Parent     = null;
            A.LeftChild  = B;
            A.RightChild = C;

            B.Parent     = A;
            B.LeftChild  = null;
            B.RightChild = null;

            C.Parent     = A;
            C.LeftChild  = D;
            C.RightChild = null;

            D.Parent     = C;
            D.LeftChild  = null;
            D.RightChild = null;

            Assert.IsTrue(A.GetUncle() == null);
            Assert.IsTrue(B.GetUncle() == null);
            Assert.IsTrue(C.GetUncle() == null);
            Assert.IsTrue(D.GetUncle().Equals(B));
        }
        public void FormsTriangle()
        {
            var A = new MockBinaryTreeNode <int, string>(50, "A");
            var B = new MockBinaryTreeNode <int, string>(30, "B");
            var C = new MockBinaryTreeNode <int, string>(20, "C");
            var D = new MockBinaryTreeNode <int, string>(40, "D");
            var E = new MockBinaryTreeNode <int, string>(35, "E");
            var F = new MockBinaryTreeNode <int, string>(45, "F");
            var G = new MockBinaryTreeNode <int, string>(47, "G");
            var H = new MockBinaryTreeNode <int, string>(25, "h");

            A.Parent     = null;
            A.LeftChild  = B;
            A.RightChild = null;

            B.Parent     = A;
            B.LeftChild  = C;
            B.RightChild = D;

            C.Parent     = B;
            C.LeftChild  = null;
            C.RightChild = H;

            D.Parent     = B;
            D.LeftChild  = E;
            D.RightChild = F;

            E.Parent     = D;
            E.LeftChild  = null;
            E.RightChild = null;

            F.Parent     = D;
            F.LeftChild  = null;
            F.RightChild = G;

            G.Parent     = F;
            G.LeftChild  = null;
            G.RightChild = null;

            H.Parent     = C;
            H.LeftChild  = null;
            H.RightChild = null;

            Assert.IsFalse(A.FormsTriangle());
            Assert.IsFalse(B.FormsTriangle());
            Assert.IsFalse(C.FormsTriangle());
            Assert.IsTrue(D.FormsTriangle());
            Assert.IsTrue(E.FormsTriangle());
            Assert.IsFalse(F.FormsTriangle());
            Assert.IsFalse(G.FormsTriangle());
            Assert.IsTrue(H.FormsTriangle());
        }
        public void IsComplete()
        {
            var node1 = new MockBinaryTreeNode <int, string>(10, "str1")
            {
                LeftChild  = null,
                RightChild = null
            };

            Assert.IsFalse(node1.IsComplete());
            node1.LeftChild = new MockBinaryTreeNode <int, string>(5, "str2");

            Assert.IsFalse(node1.IsComplete());

            node1.RightChild = new MockBinaryTreeNode <int, string>(15, "str3");
            Assert.IsTrue(node1.IsComplete());
        }
        public void GetGrandParent()
        {
            var A = new MockBinaryTreeNode <int, string>(50, "A");
            var B = new MockBinaryTreeNode <int, string>(30, "B");
            var C = new MockBinaryTreeNode <int, string>(20, "C");
            var D = new MockBinaryTreeNode <int, string>(40, "D");
            var E = new MockBinaryTreeNode <int, string>(35, "E");
            var F = new MockBinaryTreeNode <int, string>(45, "F");
            var G = new MockBinaryTreeNode <int, string>(47, "G");

            A.Parent     = null;
            A.LeftChild  = B;
            A.RightChild = null;

            B.Parent     = A;
            B.LeftChild  = C;
            B.RightChild = D;

            C.Parent     = B;
            C.LeftChild  = null;
            C.RightChild = null;

            D.Parent     = B;
            D.LeftChild  = E;
            D.RightChild = F;

            E.Parent     = D;
            E.LeftChild  = null;
            E.RightChild = null;

            F.Parent     = D;
            F.LeftChild  = null;
            F.RightChild = G;

            G.Parent     = F;
            G.LeftChild  = null;
            G.RightChild = null;

            Assert.IsTrue(A.GetGrandParent() == null);
            Assert.IsTrue(B.GetGrandParent() == null);
            Assert.IsTrue(C.GetGrandParent().Equals(A));
            Assert.IsTrue(D.GetGrandParent().Equals(A));
            Assert.IsTrue(E.GetGrandParent().Equals(B));
            Assert.IsTrue(F.GetGrandParent().Equals(B));
            Assert.IsTrue(G.GetGrandParent().Equals(D));
        }
Beispiel #6
0
        public void Initialize()
        {
            var A = new MockBinaryTreeNode <int, string>(40, "str3");
            var B = new MockBinaryTreeNode <int, string>(20, "str1");
            var C = new MockBinaryTreeNode <int, string>(70, "str6");
            var D = new MockBinaryTreeNode <int, string>(50, "str4");
            var E = new MockBinaryTreeNode <int, string>(80, "str7");
            var F = new MockBinaryTreeNode <int, string>(30, "str2");
            var G = new MockBinaryTreeNode <int, string>(60, "str5");

            A.Parent     = null;
            A.LeftChild  = B;
            A.RightChild = C;

            B.Parent     = A;
            B.LeftChild  = null;
            B.RightChild = F;

            C.Parent     = A;
            C.LeftChild  = D;
            C.RightChild = E;

            D.Parent     = C;
            D.LeftChild  = null;
            D.RightChild = G;

            E.Parent     = C;
            E.LeftChild  = null;
            E.RightChild = null;

            F.Parent     = B;
            F.LeftChild  = null;
            F.RightChild = null;

            G.Parent     = D;
            G.LeftChild  = null;
            G.RightChild = null;

            _tree = new MockBinarySearchTreeBase <int, string>();
            _root = A;
        }
        public void IsRightChild_Success()
        {
            var A = new MockBinaryTreeNode <int, string>(5, "A");
            var B = new MockBinaryTreeNode <int, string>(10, "B");
            var C = new MockBinaryTreeNode <int, string>(20, "C");

            A.Parent     = null;
            A.LeftChild  = null;
            A.RightChild = B;

            B.Parent     = A;
            B.LeftChild  = null;
            B.RightChild = C;

            C.Parent     = B;
            C.LeftChild  = null;
            C.RightChild = null;

            Assert.IsTrue(B.IsRightChild());
            Assert.IsTrue(C.IsRightChild());
        }
        public void Initialize()
        {
            var B = new MockBinaryTreeNode <int, string>(30, "B");
            var A = new MockBinaryTreeNode <int, string>(47, "A");
            var C = new MockBinaryTreeNode <int, string>(50, "C");
            var D = new MockBinaryTreeNode <int, string>(20, "D");
            var E = new MockBinaryTreeNode <int, string>(40, "E");
            var F = new MockBinaryTreeNode <int, string>(35, "F");
            var G = new MockBinaryTreeNode <int, string>(45, "G");

            A.Parent     = null;
            A.LeftChild  = B;
            A.RightChild = C;

            B.Parent     = A;
            B.LeftChild  = D;
            B.RightChild = E;

            C.Parent     = A;
            C.LeftChild  = null;
            C.RightChild = null;

            D.Parent     = B;
            D.LeftChild  = null;
            D.RightChild = null;

            E.Parent     = B;
            E.LeftChild  = F;
            E.RightChild = G;

            F.Parent     = E;
            F.LeftChild  = null;
            F.RightChild = null;

            G.Parent     = E;
            G.LeftChild  = null;
            G.RightChild = null;

            _root = A;
        }
        public void IsRightChild_Failure()
        {
            var A = new MockBinaryTreeNode <int, string>(10, "A");
            var B = new MockBinaryTreeNode <int, string>(5, "B");
            var C = new MockBinaryTreeNode <int, string>(2, "C");

            A.Parent     = null;
            A.LeftChild  = B;
            A.RightChild = null;

            B.Parent     = A;
            B.LeftChild  = C;
            B.RightChild = null;

            C.Parent     = B;
            C.LeftChild  = null;
            C.RightChild = null;

            Assert.IsFalse(A.IsRightChild());
            Assert.IsFalse(B.IsRightChild());
            Assert.IsFalse(C.IsRightChild());
        }
Beispiel #10
0
        public void InsertBST_WithoutBalancing()
        {
            var keyVals = new Dictionary <int, string>
            {
                [40] = "str3",
                [20] = "str1",
                [70] = "str6",
                [50] = "str4",
                [80] = "str7",
                [30] = "str2",
                [60] = "str5",
            };

            var tree = new MockBinarySearchTreeBase <int, string>();
            MockBinaryTreeNode <int, string> root = null;

            root = tree.Insert_BST(root, new MockBinaryTreeNode <int, string>(40, "str3"));
            root = tree.Insert_BST(root, new MockBinaryTreeNode <int, string>(20, "str1"));
            root = tree.Insert_BST(root, new MockBinaryTreeNode <int, string>(70, "str6"));
            root = tree.Insert_BST(root, new MockBinaryTreeNode <int, string>(50, "str4"));
            root = tree.Insert_BST(root, new MockBinaryTreeNode <int, string>(80, "str7"));
            root = tree.Insert_BST(root, new MockBinaryTreeNode <int, string>(30, "str2"));
            root = tree.Insert_BST(root, new MockBinaryTreeNode <int, string>(60, "str5"));

            Assert.IsTrue(HasBinarySearchTreeOrderProperty <MockBinaryTreeNode <int, string>, int, string>(root));

            var nodes = new List <MockBinaryTreeNode <int, string> >();

            _tree.InOrderTraversal(root, nodes);
            Assert.AreEqual(7, nodes.Count);
            for (int i = 0; i < nodes.Count - 1; i++)
            {
                Assert.IsTrue(nodes[i].Key < nodes[i + 1].Key);
            }

            Assert.AreEqual(40, root.Key);
            Assert.AreEqual("str3", root.Value, ignoreCase: false);
        }
 /// <summary>
 /// Searches for the given key in the tree.
 /// </summary>
 /// <param name="root">Current root of the tree, or the node at which search operation should be started. </param>
 /// <param name="key">The key to be searched. </param>
 /// <returns>Returns the tree node that contains key. </returns>
 public override MockBinaryTreeNode <TKey, TValue> Search(MockBinaryTreeNode <TKey, TValue> root, TKey key)
 {
     return(Search_BST(root, key));
 }
 /// <summary>
 /// Inserts a new node in the tree
 /// </summary>
 /// <param name="root">Current root of the tree, or the node at which insert operation should be started.</param>
 /// <param name="newNode">New node to be inserted in the tree. </param>
 /// <returns>New root of the tree (might or might not change during operation).</returns>
 public override MockBinaryTreeNode <TKey, TValue> Insert(MockBinaryTreeNode <TKey, TValue> root, MockBinaryTreeNode <TKey, TValue> newNode)
 {
     return(Insert_BST(root, newNode));
 }
 /// <summary>
 /// Finds the minimum key in the (sub)tree rooted at <paramref name="root"/> node.
 /// </summary>
 /// <param name="root">The node at which (sub)tree is rooted. </param>
 /// <returns>The node containing the minimum key. </returns>
 public override MockBinaryTreeNode <TKey, TValue> FindMin(MockBinaryTreeNode <TKey, TValue> root)
 {
     return(FindMin_BST(root));
 }
 /// <summary>
 /// Deletes a node with the given key from th tree.
 /// </summary>
 /// <param name="root">Current root of the tree, or the node at which delete operation should be started. </param>
 /// <param name="key">The key of the node to be deleted. </param>
 /// <returns>New root of the tree (might or might not change during the operation).</returns>
 public override MockBinaryTreeNode <TKey, TValue> Delete(MockBinaryTreeNode <TKey, TValue> root, TKey key)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Updates the tree node of the specified key with the new given value.
 /// </summary>
 /// <param name="root">Current root of the tree, or the node at which update operation should be started.</param>
 /// <param name="key">The key of the node whose value should be updated.</param>
 /// <param name="value">The new value. </param>
 /// <returns>true in case of success and false otherwise.</returns>
 public override bool Update(MockBinaryTreeNode <TKey, TValue> root, TKey key, TValue value)
 {
     return(Update_BST(root, key, value));
 }
Beispiel #16
0
        public void RotateLeft()
        {
            var A = new MockBinaryTreeNode <int, string>(50, "A");
            var B = new MockBinaryTreeNode <int, string>(30, "B");
            var C = new MockBinaryTreeNode <int, string>(20, "C");
            var D = new MockBinaryTreeNode <int, string>(40, "D");
            var E = new MockBinaryTreeNode <int, string>(35, "E");
            var F = new MockBinaryTreeNode <int, string>(45, "F");
            var G = new MockBinaryTreeNode <int, string>(47, "G");

            A.Parent     = null;
            A.LeftChild  = B;
            A.RightChild = null;

            B.Parent     = A;
            B.LeftChild  = C;
            B.RightChild = D;

            C.Parent     = B;
            C.LeftChild  = null;
            C.RightChild = null;

            D.Parent     = B;
            D.LeftChild  = E;
            D.RightChild = F;

            E.Parent     = D;
            E.LeftChild  = null;
            E.RightChild = null;

            F.Parent     = D;
            F.LeftChild  = null;
            F.RightChild = G;

            G.Parent     = F;
            G.LeftChild  = null;
            G.RightChild = null;

            Assert.IsTrue(HasBinarySearchTreeOrderProperty <MockBinaryTreeNode <int, string>, int, string>(A));
            var tree = new MockBinarySearchTreeBase <int, string>();

            tree.RotateLeft(B);
            Assert.IsTrue(HasBinarySearchTreeOrderProperty <MockBinaryTreeNode <int, string>, int, string>(A));

            Assert.IsTrue(A.Parent == null);
            Assert.IsTrue(A.LeftChild.Equals(D));
            Assert.IsTrue(A.RightChild == null);
            Assert.IsTrue(B.Parent.Equals(D));
            Assert.IsTrue(B.LeftChild.Equals(C));
            Assert.IsTrue(B.RightChild.Equals(E));
            Assert.IsTrue(C.Parent.Equals(B));
            Assert.IsTrue(C.LeftChild == null);
            Assert.IsTrue(C.RightChild == null);
            Assert.IsTrue(D.Parent.Equals(A));
            Assert.IsTrue(D.LeftChild.Equals(B));
            Assert.IsTrue(D.RightChild.Equals(F));
            Assert.IsTrue(E.Parent.Equals(B));
            Assert.IsTrue(E.LeftChild == null);
            Assert.IsTrue(E.RightChild == null);
            Assert.IsTrue(F.Parent.Equals(D));
            Assert.IsTrue(F.LeftChild == null);
            Assert.IsTrue(F.RightChild.Equals(G));
            Assert.IsTrue(G.Parent.Equals(F));
            Assert.IsTrue(G.LeftChild == null);
            Assert.IsTrue(G.RightChild == null);
        }