Пример #1
0
 public static IEnumerable <TKey> GetKeys <TKey>(this BstNodeBase <TKey> node)
 {
     for (var n = SearchMinNode(node); n != null; n = SearchNextNode(n))
     {
         yield return(n.Key);
     }
 }
Пример #2
0
 public static IEnumerable <TKey> GetKeys <TKey>(this BstNodeBase <TKey> node, Func <TKey, bool> predicateForMin, Func <TKey, bool> predicateForMax)
 {
     for (var n = SearchMinNode(node, predicateForMin); n != null && predicateForMax(n.Key); n = SearchNextNode(n))
     {
         yield return(n.Key);
     }
 }
Пример #3
0
 public static BstNodeBase <TKey> SearchMaxNode <TKey>(this BstNodeBase <TKey> node)
 {
     if (node == null)
     {
         return(null);
     }
     return(SearchMaxNode(node.Right) ?? node);
 }
Пример #4
0
 public static BstNodeBase <TKey> SearchNextNode <TKey>(this BstNodeBase <TKey> node)
 {
     if (node == null)
     {
         return(null);
     }
     return(SearchMinNode(node.Right) ?? SearchNextAncestor(node));
 }
Пример #5
0
 public static BstNodeBase <TKey> SearchPreviousNode <TKey>(this BstNodeBase <TKey> node)
 {
     if (node == null)
     {
         return(null);
     }
     return(SearchMaxNode(node.Left) ?? SearchPreviousAncestor(node));
 }
Пример #6
0
 public static void WalkByPreorder <TKey>(this BstNodeBase <TKey> node, Action <TKey> action)
 {
     if (node == null)
     {
         return;
     }
     action(node.Key);
     WalkByPreorder(node.Left, action);
     WalkByPreorder(node.Right, action);
 }
Пример #7
0
        public static BstNodeBase <TKey> RotateToLeft <TKey>(this BstNodeBase <TKey> node)
        {
            if (node == null)
            {
                throw new ArgumentNullException();
            }
            var p = node.Right;

            node.Right = p.Left;
            p.Left     = node;
            return(p);
        }
Пример #8
0
 public static BstNodeBase <TKey> SearchMinNode <TKey>(this BstNodeBase <TKey> node, Func <TKey, bool> predicate)
 {
     if (node == null)
     {
         return(null);
     }
     if (predicate(node.Key))
     {
         return(SearchMinNode(node.Left, predicate) ?? node);
     }
     else
     {
         return(SearchMinNode(node.Right, predicate));
     }
 }
Пример #9
0
 static BstNodeBase <TKey> SearchNextAncestor <TKey>(this BstNodeBase <TKey> node)
 {
     if (node?.Parent == null)
     {
         return(null);
     }
     if (node.Parent.Left == node)
     {
         return(node.Parent);
     }
     else
     {
         return(SearchNextAncestor(node.Parent));
     }
 }
Пример #10
0
 static BstNodeBase <TKey> SearchPreviousAncestor <TKey>(this BstNodeBase <TKey> node)
 {
     if (node?.Parent == null)
     {
         return(null);
     }
     if (node.Parent.Right == node)
     {
         return(node.Parent);
     }
     else
     {
         return(SearchPreviousAncestor(node.Parent));
     }
 }
Пример #11
0
        public static BstNodeBase <TKey> SearchNode <TKey>(this BstNodeBase <TKey> node, TKey key, Comparison <TKey> comparison)
        {
            if (node == null)
            {
                return(null);
            }
            var d = comparison(key, node.Key);

            if (d == 0)
            {
                return(node);
            }
            if (d < 0)
            {
                return(SearchNode(node.Left, key, comparison));
            }
            else
            {
                return(SearchNode(node.Right, key, comparison));
            }
        }
Пример #12
0
 public static BstNodeBase <KeyValuePair <TKey, TValue> > SearchMaxNode <TKey, TValue>(this BstNodeBase <KeyValuePair <TKey, TValue> > node, Func <TKey, bool> predicate)
 {
     if (node == null)
     {
         return(null);
     }
     if (predicate(node.Key.Key))
     {
         return(SearchMaxNode(node.Right, predicate) ?? node);
     }
     else
     {
         return(SearchMaxNode(node.Left, predicate));
     }
 }