Exemplo n.º 1
0
 private static BinaryTreeNode <KeyValuePair <TKey, TValue> > RemoveMininumKey(BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out KeyValuePair <TKey, TValue> min)
 {
     if (t.LeftChild == null)
     {
         min = t.Data;
         return(t.RightChild);
     }
     else
     {
         BinaryTreeNode <KeyValuePair <TKey, TValue> > left = RemoveMininumKey(t.LeftChild, out min);
         return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, left, t.RightChild));
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// retruns the result of removing a binary tree(as a binary tree)
        /// </summary>
        /// <param name="key">key that is passed in</param>
        /// <param name="t">tree that is passed in</param>
        /// <param name="removed">tree that is going to be removed</param>
        /// <returns></returns>
        private static BinaryTreeNode <KeyValuePair <TKey, TValue> > Remove(TKey key, BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out bool removed)
        {
            if (t == null)
            {
                removed = false;
                return(t);
            }
            else if (t.Data.Key.CompareTo(key) < 0)//trees data is less than the key
            {
                BinaryTreeNode <KeyValuePair <TKey, TValue> > thing = Remove(key, t.RightChild, out removed);
                BinaryTreeNode <KeyValuePair <TKey, TValue> > exit  = new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, t.LeftChild, thing);
                return(exit);
            }
            else if (t.Data.Key.CompareTo(key) > 0)
            {
                BinaryTreeNode <KeyValuePair <TKey, TValue> > leftThing = Remove(key, t.LeftChild, out removed);
                BinaryTreeNode <KeyValuePair <TKey, TValue> > exit      = new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, leftThing, t.RightChild);
                return(exit);
            }
            else
            {
                removed = true;

                if (t.RightChild == null) //i fliped this case with the last one
                {
                    return(t.LeftChild);  //retrun that child
                }
                else if (t.LeftChild == null)
                {
                    return(t.RightChild);//retrun that child
                }
                else
                {
                    KeyValuePair <TKey, TValue> tempKey = new KeyValuePair <TKey, TValue>();
                    BinaryTreeNode <KeyValuePair <TKey, TValue> > updateRight = RemoveMininumKey(t.RightChild, out tempKey);

                    //buld a new tree min(data) as head original tree and right child
                    BinaryTreeNode <KeyValuePair <TKey, TValue> > exit = new BinaryTreeNode <KeyValuePair <TKey, TValue> >(tempKey, t.LeftChild, updateRight);
                    return(exit);//should I create a tree with the parameters found above?
                }
            }
        }
        /// <summary>
        /// Removes key given from dictionary using binary tree
        /// </summary>
        /// <param name="key">The Key</param>
        /// <param name="t">The tree</param>
        /// <param name="removed">true if remove occurred</param>
        /// <returns></returns>
        private static BinaryTreeNode <KeyValuePair <TKey, TValue> > Remove(TKey key, BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out bool removed)
        {
            //checks that a tree exists
            if (t == null)
            {
                removed = false;
                return(null);
            }

            int comp = key.CompareTo(t.Data.Key);

            // if given key is equal to root's key
            if (comp == 0)
            {
                removed = true;
                //if either child is empty, return the other child
                if (t.LeftChild == null)
                {
                    return(t.RightChild);
                }
                else if (t.RightChild == null)
                {
                    return(t.LeftChild);
                }
                else
                {
                    //build tree
                    KeyValuePair <TKey, TValue> rmin;
                    BinaryTreeNode <KeyValuePair <TKey, TValue> > rremoved = RemoveMininumKey(t.RightChild, out rmin);

                    return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(rmin, t.LeftChild, rremoved));
                }
            }
            else if (comp < 0)
            {
                return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, Remove(key, t.LeftChild, out removed), t.RightChild));
                //return tree whose left child is the result of removing the given key from it
            }
            else
            {
                ///Otherwise, return a tree whose right child is the result of removing the given key from it.
                return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, t.LeftChild, Remove(key, t.RightChild, out removed)));
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <param name="tree"></param>
        /// <param name="removed"></param>
        /// <returns></returns>
        private static BinaryTreeNode <KeyValuePair <TKey, TValue> > RemoveNode(TKey key, BinaryTreeNode <KeyValuePair <TKey, TValue> > tree, out bool removed)
        {
            if (tree == null)
            {
                removed = false;
                return(tree);
            }
            int  compare = tree.Data.Key.CompareTo(key);
            bool recursiveRemoved;

            if (compare < 0)
            {
                BinaryTreeNode <KeyValuePair <TKey, TValue> > rightUpdate = RemoveNode(key, tree.RightChild, out recursiveRemoved);
                removed = recursiveRemoved;
                return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(tree.Data, tree.LeftChild, rightUpdate));
            }
            else if (compare > 0)
            {
                BinaryTreeNode <KeyValuePair <TKey, TValue> > leftUpdate = RemoveNode(key, tree.LeftChild, out recursiveRemoved);
                removed = recursiveRemoved;
                return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(tree.Data, leftUpdate, tree.RightChild));
            }
            else
            {
                removed = true;
                if (tree.RightChild == null && tree.LeftChild == null)
                {
                    return(null);
                }
                else if (tree.RightChild == null)
                {
                    return(tree.LeftChild);
                }
                else if (tree.LeftChild == null)
                {
                    return(tree.RightChild);
                }
                else
                {
                    KeyValuePair <TKey, TValue> min;
                    BinaryTreeNode <KeyValuePair <TKey, TValue> > updatedRight = FindMinimumKey(tree.RightChild, out min);
                    return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(min, tree.LeftChild, updatedRight));
                }
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// returns the minimum tree from a tree that is passed in.
 /// </summary>
 /// <param name="t">tree that is passed in </param>
 /// <param name="min">out parameter to be set(tree)</param>
 /// <returns></returns>
 private static BinaryTreeNode <KeyValuePair <TKey, TValue> > RemoveMininumKey(BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out KeyValuePair <TKey, TValue> min)
 {
     if (t.LeftChild != null)                                                                          //if the tree has a left
     {
         BinaryTreeNode <KeyValuePair <TKey, TValue> > lefty = RemoveMininumKey(t.LeftChild, out min); //left child
         BinaryTreeNode <KeyValuePair <TKey, TValue> > exit  = new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, lefty, t.RightChild);
         return(exit);
     }
     else//we are the min
     {
         min = t.Data;
         return(t.RightChild);
     }
 }
 /// <summary>
 /// Removes the node at a given key from the tree.
 /// </summary>
 /// <param name="key"></param>
 /// <param name="t"></param>
 /// <param name="removed"></param>
 /// <returns></returns>
 private static BinaryTreeNode <KeyValuePair <TKey, TValue> > Remove(TKey key, BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out bool removed)
 {
     if (t == null)
     {
         removed = false;
         return(null);
     }
     else
     {
         int comp = key.CompareTo(t.Data.Key);
         if (comp == 0)
         {
             removed = true;
             if (t.LeftChild == null)
             {
                 return(t.RightChild);
             }
             else if (t.RightChild == null)
             {
                 return(t.LeftChild);
             }
             else if (t.LeftChild != null && t.RightChild != null)
             {
                 KeyValuePair <TKey, TValue> min;
                 BinaryTreeNode <KeyValuePair <TKey, TValue> > r = RemoveMinimumKey(t, out min);
                 return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(min, t.LeftChild, r));
             }
             else
             {
                 return(t);
             }
         }
         else if (comp < 0)
         {
             return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, Remove(key, t.LeftChild, out removed), t.RightChild));
         }
         else
         {
             return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, t.LeftChild, Remove(key, t.RightChild, out removed)));
         }
     }
 }
 /// <summary>
 /// Moves the smallest node for given tree and returns the result
 /// </summary>
 /// <param name="t">BST to remove minimum from</param>
 /// <param name="min">out parameter holding K-V pair of minimum value in tree</param>
 /// <returns></returns>
 private static BinaryTreeNode <KeyValuePair <TKey, TValue> > RemoveMinimumKey(BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out KeyValuePair <TKey, TValue> min)
 {
     if (t == null)
     {
         throw new ArgumentException();
     }
     if (t.LeftChild == null)
     {
         min = t.Data;
         return(t.RightChild);
     }
     else
     {
         return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, RemoveMinimumKey(t.LeftChild, out min), t.RightChild));
     }
 }
Exemplo n.º 8
0
 /// <summary>
 /// Removes the given key from the provided tree noded and returns whether or not the pair was removed.
 /// </summary>
 /// <param name="key">The provided key to be removed</param>
 /// <param name="t">The given tree node</param>
 /// <param name="removed">True if the key was removed.</param>
 /// <returns></returns>
 private static BinaryTreeNode <KeyValuePair <TKey, TValue> > Remove(TKey key, BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out bool removed)
 {
     if (t == null)
     {
         removed = false;
         BinaryTreeNode <KeyValuePair <TKey, TValue> > temp = null;
         return(temp);
     }
     else
     {
         float compare = key.CompareTo(t.Data.Key);
         if (compare == 0)
         {
             removed = true;
             if (t.LeftChild == null)
             {
                 return(t.RightChild);
             }
             else if (t.RightChild == null)
             {
                 return(t.LeftChild);
             }
             else
             {
                 BinaryTreeNode <KeyValuePair <TKey, TValue> > newRightChild = RemoveMininumKey(t.RightChild, out KeyValuePair <TKey, TValue> min);
                 BinaryTreeNode <KeyValuePair <TKey, TValue> > newNode       = new BinaryTreeNode <KeyValuePair <TKey, TValue> >(min, t.LeftChild, newRightChild);
                 return(newNode);
             }
         }
         else if (compare < 0)
         {
             BinaryTreeNode <KeyValuePair <TKey, TValue> > newLeftChild = Remove(key, t.LeftChild, out removed);
             BinaryTreeNode <KeyValuePair <TKey, TValue> > newNode      = new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, newLeftChild, t.RightChild);
             return(newNode);
         }
         else
         {
             BinaryTreeNode <KeyValuePair <TKey, TValue> > newRightChild = Remove(key, t.RightChild, out removed);
             BinaryTreeNode <KeyValuePair <TKey, TValue> > newNode       = new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, t.LeftChild, newRightChild);
             return(newNode);
         }
     }
 }
Exemplo n.º 9
0
 private static BinaryTreeNode <KeyValuePair <TKey, TValue> > RemoveMininumKey(BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out KeyValuePair <TKey, TValue> min)
 {
     if (t.LeftChild != null)
     {
         BinaryTreeNode <KeyValuePair <TKey, TValue> > newLeft     = RemoveMininumKey(t.LeftChild, out min);
         BinaryTreeNode <KeyValuePair <TKey, TValue> > updatedTree = new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, newLeft, t.RightChild);
         return(updatedTree);
     }
     else
     {
         min = t.Data;
         return(t.RightChild);
     }
 }
Exemplo n.º 10
0
        private static BinaryTreeNode <KeyValuePair <TKey, TValue> > Remove(TKey key, BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out bool removed)
        {
            if (t == null)
            {
                removed = false;
                return(t);
            }
            else
            {
                int comp = key.CompareTo(t.Data.Key);
                if (comp == 0)
                {
                    removed = true;

                    //remove it
                    if (t.LeftChild == null)
                    {
                        return(t.RightChild);
                    }
                    else if (t.RightChild == null)
                    {
                        return(t.LeftChild);
                    }
                    else
                    {
                        KeyValuePair <TKey, TValue> small;
                        BinaryTreeNode <KeyValuePair <TKey, TValue> > moveUp = RemoveMininumKey(t.RightChild, out small);
                        return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(small, t.LeftChild, moveUp));
                    }
                }
                else if (comp < 0)
                {
                    return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, Remove(key, t.LeftChild, out removed), t.RightChild));
                }
                else
                {
                    return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, t.LeftChild, Remove(key, t.RightChild, out removed)));
                }
            }
        }
        /// <summary>
        /// Removes the Node within the binary tree.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="t"></param>
        /// <param name="removed"></param>
        /// <returns></returns>
        private static BinaryTreeNode <KeyValuePair <TKey, TValue> > Remove(TKey key, BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out bool removed)
        {
            if (t == null)
            {
                removed = false;
                return(null);
            }
            int compare = key.CompareTo(t.Data.Key);

            if (compare == 0)
            {
                if (t.RightChild == null)
                {
                    removed = true;
                    return(t.LeftChild);
                }
                else if (t.LeftChild == null)
                {
                    removed = true;
                    return(t.RightChild);
                }
                else
                {
                    removed = true;
                    KeyValuePair <TKey, TValue> min;
                    BinaryTreeNode <KeyValuePair <TKey, TValue> > right = RemoveMininumKey(t.RightChild, out min);
                    return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(min, t.LeftChild, right));
                }
            }
            else if (compare < 0)
            {
                BinaryTreeNode <KeyValuePair <TKey, TValue> > remove = Remove(key, t.LeftChild, out removed);

                return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, remove, t.RightChild));
            }
            else
            {
                BinaryTreeNode <KeyValuePair <TKey, TValue> > remove = Remove(key, t.RightChild, out removed);

                return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, t.LeftChild, remove));
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// removes the node
        /// </summary>
        /// <param name="key"></param>
        /// <param name="t"></param>
        /// <param name="removed"></param>
        /// <returns>the result of removing the node containing the given key from the given binary search tree.</returns>
        private static BinaryTreeNode <KeyValuePair <TKey, TValue> > Remove(TKey key, BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out bool removed)
        {
            if (t == null)
            {
                removed = false;
                return(null);
            }

            if (key.CompareTo(t.Data.Key) == 0)
            {
                removed = true;
                if (t.LeftChild == null)
                {
                    return(t.RightChild);
                }
                else if (t.RightChild == null)
                {
                    return(t.LeftChild);
                }
                else
                {
                    KeyValuePair <TKey, TValue> minKey;
                    BinaryTreeNode <KeyValuePair <TKey, TValue> > Right      = RemoveMininumKey(t.RightChild, out minKey);
                    BinaryTreeNode <KeyValuePair <TKey, TValue> > binaryTree = new BinaryTreeNode <KeyValuePair <TKey, TValue> >(minKey, t.LeftChild, Right);
                    return(binaryTree);
                }
            }
            if (key.CompareTo(t.Data.Key) < 0)
            {
                return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, Remove(key, t.LeftChild, out removed), t.RightChild));
            }
            else
            {
                return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, t.LeftChild, Remove(key, t.RightChild, out removed)));
            }
        }
        private static BinaryTreeNode <KeyValuePair <TKey, TValue> > Remove(TKey key, BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out bool removed)
        {
            if (key == null)
            {
                throw new ArgumentNullException();
            }
            if (t == null)
            {
                removed = false;
                return(t);
            }

            BinaryTreeNode <KeyValuePair <TKey, TValue> > returnTree = null;
            int compareKey = key.CompareTo(t.Data.Key);


            if (compareKey > 0)
            {
                returnTree = new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, t.LeftChild, Remove(key, t.RightChild, out removed));
                return(returnTree);
            }
            else if (compareKey < 0)
            {
                returnTree = new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, Remove(key, t.LeftChild, out removed), t.RightChild);
                return(returnTree);
            }
            else
            {
                KeyValuePair <TKey, TValue> min;
                removed = true;
                if (t.RightChild != null)
                {
                    BinaryTreeNode <KeyValuePair <TKey, TValue> > tempNode = RemoveMininumKey(t.RightChild, out min);
                    returnTree = new BinaryTreeNode <KeyValuePair <TKey, TValue> >(min, t.LeftChild, tempNode);
                }
                else if (t.LeftChild != null)
                {
                    BinaryTreeNode <KeyValuePair <TKey, TValue> > tempNode = RemoveMininumKey(t.LeftChild, out min);
                    returnTree = new BinaryTreeNode <KeyValuePair <TKey, TValue> >(min, tempNode, t.RightChild);
                }
                else
                {
                    returnTree = null;
                }

                return(returnTree);
            }
        }
        private static BinaryTreeNode <KeyValuePair <TKey, TValue> > RemoveMininumKey(BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out KeyValuePair <TKey, TValue> min)
        {
            BinaryTreeNode <KeyValuePair <TKey, TValue> > tempTree = null;

            if (t.LeftChild != null)
            {
                tempTree = new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, RemoveMininumKey(t.LeftChild, out min), t.RightChild);
                return(tempTree);
            }
            else if (t.RightChild != null)
            {
                min      = t.Data;
                tempTree = new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, null, t.RightChild);
                return(tempTree);
            }
            else
            {
                min = t.Data;
                return(tempTree = null);
            }
        }
Exemplo n.º 15
0
 /// <summary>
 /// Returns the result of removing the node containing the given key from the given binary search tree
 /// </summary>
 /// <param name="key">The key you want removed</param>
 /// <param name="t">The tree you are wanting k to be removed from</param>
 /// <param name="removed">Output of whether or not it was removed</param>
 /// <returns>The tree with the key removed</returns>
 private static BinaryTreeNode <KeyValuePair <TKey, TValue> > Remove(TKey key, BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out bool removed)
 {
     //The tree is empty
     if (t == null)
     {
         removed = false;
         return(null);
     }
     else
     {
         int result = key.CompareTo(t.Data.Key);
         //The key we are looking for is at the root
         if (result == 0)
         {
             removed = true;
             //The left child is empty
             if (t.LeftChild == null)
             {
                 return(t.RightChild);
             }
             //The right child is empty
             else if (t.RightChild == null)
             {
                 return(t.LeftChild);
             }
             //Both children are non empty
             else
             {
                 KeyValuePair <TKey, TValue> min;
                 BinaryTreeNode <KeyValuePair <TKey, TValue> > right = RemoveMininumKey(t.RightChild, out min);
                 return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(min, t.LeftChild, right));
             }
         }
         //The key we are looking for is less than the key at the root
         else if (result < 0)
         {
             return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, Remove(key, t.LeftChild, out removed), t.RightChild));
         }
         //The key we are looking for is greater than the key at the root
         else
         {
             return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, t.LeftChild, Remove(key, t.RightChild, out removed)));
         }
     }
 }
 /// <summary>
 ///  Remove the given key and its associated value from the dictionary using the above method.
 /// </summary>
 /// <param name="k"></param>
 /// <returns> Whether the key was found </returns>
 public bool Remove(TKey k)
 {
     CheckKey(k);
     _elements = Remove(k, _elements, out bool removed);
     return(removed);
 }
Exemplo n.º 17
0
        /// <summary>
        /// Builds the result of performing a double rotate left on the binary tree
        /// described by the given root, left child, and right child.
        /// </summary>
        /// <param name="root">The data stored in the root of the original tree.</param>
        /// <param name="left">The left child of the root of the original tree.</param>
        /// <param name="right">The right child of the root of the original tree.</param>
        /// <returns>The result of performing a single rotate right on the tree described
        /// by the parameters.</returns>
        private static BinaryTreeNode <T> DoubleRotateLeft(T root, BinaryTreeNode <T> left, BinaryTreeNode <T> right)
        {
            BinaryTreeNode <T> newLeft  = new BinaryTreeNode <T>(root, left, right.LeftChild.LeftChild);
            BinaryTreeNode <T> newRight = new BinaryTreeNode <T>(right.Data, right.LeftChild.RightChild, right.RightChild);

            return(new BinaryTreeNode <T>(right.LeftChild.Data, newLeft, newRight));
        }
        /// <summary>
        /// goes through the tree finds the minimum value and removes it
        /// </summary>
        /// <param name="t"></param>
        /// <param name="min"></param>
        /// <returns>updated tree</returns>
        private static BinaryTreeNode <KeyValuePair <TKey, TValue> > RemoveMininumKey(BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out KeyValuePair <TKey, TValue> min)
        {
            if (t.LeftChild != null)
            {
                min = default(KeyValuePair <TKey, TValue>);
                BinaryTreeNode <KeyValuePair <TKey, TValue> > updated = RemoveMininumKey(t.LeftChild, out min);
                return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, updated, t.RightChild));
            }

            else
            {
                min = t.Data;
                return(t.RightChild);
            }
        }
 /// <summary>
 /// Adds the given key with the given associated value.
 /// If the given key is already in the dictionary, throws an
 /// InvalidOperationException.
 /// </summary>
 /// <param name="k">The key.</param>
 /// <param name="v">The value.</param>
 public void Add(TKey k, TValue v)
 {
     CheckKey(k);
     _elements = Add(_elements, k, v);
 }
        /// <summary>
        /// looks to find the key that is to be removed, and removes it from the tree, updates tree
        /// </summary>
        /// <param name="key"></param>
        /// <param name="t"></param>
        /// <param name="removed"></param>
        /// <returns>updated tree if valid</returns>
        private static BinaryTreeNode <KeyValuePair <TKey, TValue> > Remove(TKey key, BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out bool removed)
        {
            if (t == null)
            {
                removed = false;
                return(null);
            }

            else if (t.Data.Key.CompareTo(key) < 0)
            {
                BinaryTreeNode <KeyValuePair <TKey, TValue> > rightUpdate = Remove(key, t.RightChild, out removed);
                return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, t.LeftChild, rightUpdate));
            }

            else if (t.Data.Key.CompareTo(key) > 0)
            {
                BinaryTreeNode <KeyValuePair <TKey, TValue> > leftUpdate = Remove(key, t.LeftChild, out removed);
                return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, leftUpdate, t.RightChild));
            }

            else
            {
                removed = true;
                if (t.LeftChild == null && t.RightChild == null)
                {
                    return(null);
                }
                else if (t.LeftChild != null && t.RightChild == null)
                {
                    return(t.LeftChild);
                }
                else if (t.RightChild != null && t.LeftChild == null)
                {
                    return(t.RightChild);
                }
                else
                {
                    KeyValuePair <TKey, TValue> min;
                    BinaryTreeNode <KeyValuePair <TKey, TValue> > updatedRight = RemoveMininumKey(t.RightChild, out min);

                    return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(min, t.LeftChild, updatedRight));
                }
            }
        }
        /// <summary>
        /// Removes node containing given key from BST
        /// </summary>
        /// <param name="key">key being searched for</param>
        /// <param name="t">BST being searched</param>
        /// <param name="removed">Indicates if key was found</param>
        /// <returns></returns>
        private static BinaryTreeNode <KeyValuePair <TKey, TValue> > Remove(TKey key, BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out bool removed)
        {
            if (t == null)
            {
                removed = false;
                return(null);
            }
            int comparison = key.CompareTo(t.Data.Key);

            if (comparison == 0)
            {
                removed = true;
                if (t.LeftChild == null)
                {
                    return(t.RightChild);
                }
                else if (t.RightChild == null)
                {
                    return(t.LeftChild);
                }
                else
                {
                    KeyValuePair <TKey, TValue> minPair = new KeyValuePair <TKey, TValue>();
                    BinaryTreeNode <KeyValuePair <TKey, TValue> > result = RemoveMinimumKey(t.RightChild, out minPair);
                    return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(minPair, t.LeftChild, result));
                }
            }
            if (comparison < 0) //key is less than current root
            {
                return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, Remove(key, t.LeftChild, out removed), t.RightChild));
            }
            if (comparison > 0) //key greater than current root
            {
                return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, t.LeftChild, Remove(key, t.RightChild, out removed)));
            }
            else
            {
                removed = false;
                return(null);
            }
        }
        /// <summary>
        /// Returns the results of removing the node containing the given key from the given binary search tree.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="t">The binary search tree.</param>
        /// <param name="removed">Boolean value will return true if it is removed</param>
        /// <returns>Returns the new binary tree with the node removed.</returns>
        private static BinaryTreeNode <KeyValuePair <TKey, TValue> > Remove(TKey key, BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out bool removed)
        {
            if (t == null)
            {
                removed = false;
                return(t);
            }
            int c = key.CompareTo(t.Data.Key);

            if (c == 0)
            {
                if (t.LeftChild == null)
                {
                    removed = true;
                    return(t.RightChild);
                }
                if (t.RightChild == null)
                {
                    removed = true;
                    return(t.LeftChild);
                }
                removed = true;
                BinaryTreeNode <KeyValuePair <TKey, TValue> > newRightChild = RemoveMininumKey(t.RightChild, out KeyValuePair <TKey, TValue> min);
                return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(min, t.LeftChild, newRightChild));
            }
            else if (c < 0)
            {
                return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, Remove(key, t.LeftChild, out removed), t.RightChild));
            }
            else
            {
                removed = false;
                return(new BinaryTreeNode <KeyValuePair <TKey, TValue> >(t.Data, t.LeftChild, Remove(key, t.RightChild, out removed)));
            }
        }