Пример #1
0
 /// <summary>
 /// Removes the smallest element from the given binary search tree, which must not be null.
 /// </summary>
 /// <param name="t">The binary search tree from which to remove the minimum.</param>
 /// <param name="min">The data removed.</param>
 /// <returns>The resulting binary search tree.</returns>
 private BinaryTreeNode <NameInformation> RemoveMin(BinaryTreeNode <NameInformation> t, out NameInformation min)
 {
     if (t.LeftChild == null)
     {
         min = t.Data;
         return(t.RightChild);
     }
     else
     {
         return(BinaryTreeNode <NameInformation> .GetAvlTree(t.Data, RemoveMin(t.LeftChild, out min), t.RightChild));
     }
 }
Пример #2
0
 RemoveMininumKey(BinaryTreeNode <KeyValuePair <TKey, TValue> > t, out KeyValuePair <TKey, TValue> min)
 {
     if (t.LeftChild == null)
     {
         min = t.Data;
         return(t.RightChild);
     }
     else
     {
         return(BinaryTreeNode <KeyValuePair <TKey, TValue> > .GetAvlTree(t.Data, RemoveMininumKey(t.LeftChild, out min),
                                                                          t.RightChild));
     }
 }
Пример #3
0
        /// <summary>
        /// Builds the result of adding the given NameInformation to the given binary search tree.
        /// </summary>
        /// <param name="info">The NameInformation to add.</param>
        /// <param name="t">The binary search tree.</param>
        /// <returns>The resulting binary search tree.</returns>
        private BinaryTreeNode <NameInformation> Add(NameInformation info, BinaryTreeNode <NameInformation> t)
        {
            if (t == null)
            {
                return(BinaryTreeNode <NameInformation> .GetAvlTree(info, null, null));
            }
            int comp = info.Name.CompareTo(t.Data.Name);

            if (comp == 0)
            {
                throw new ArgumentException();
            }
            else if (comp < 0)
            {
                return(BinaryTreeNode <NameInformation> .GetAvlTree(t.Data, Add(info, t.LeftChild), t.RightChild));
            }
            else
            {
                return(BinaryTreeNode <NameInformation> .GetAvlTree(t.Data, t.LeftChild, Add(info, t.RightChild)));
            }
        }
Пример #4
0
 /// <summary>
 /// Builds the result of removing the node with the given name from the given binary search tree.
 /// </summary>
 /// <param name="name">The name to remove.</param>
 /// <param name="t">The binary search tree.</param>
 /// <param name="removed">Indicates whether the name was found.</param>
 /// <returns>The resulting tree.</returns>
 private BinaryTreeNode <NameInformation> Remove(string name, BinaryTreeNode <NameInformation> t, out bool removed)
 {
     if (t == null)
     {
         removed = false;
         return(t);
     }
     else
     {
         int comp = name.CompareTo(t.Data.Name);
         {
             if (comp == 0)
             {
                 removed = true;
                 if (t.LeftChild == null)
                 {
                     return(t.RightChild);
                 }
                 else if (t.RightChild == null)
                 {
                     return(t.LeftChild);
                 }
                 else
                 {
                     NameInformation min;
                     BinaryTreeNode <NameInformation> right = RemoveMininumName(t.RightChild, out min);
                     return(BinaryTreeNode <NameInformation> .GetAvlTree(min, t.LeftChild, right));
                 }
             }
             else if (comp < 0)
             {
                 return(BinaryTreeNode <NameInformation> .GetAvlTree(t.Data, Remove(name, t.LeftChild, out removed), t.RightChild));
             }
             else
             {
                 return(BinaryTreeNode <NameInformation> .GetAvlTree(t.Data, t.LeftChild, Remove(name, t.RightChild, out removed)));
             }
         }
     }
 }