Exemplo n.º 1
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);
     }
 }
Exemplo n.º 2
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);
     }
 }
Exemplo n.º 3
0
 public static BstNodeBase <TKey> SearchPreviousNode <TKey>(this BstNodeBase <TKey> node)
 {
     if (node == null)
     {
         return(null);
     }
     return(SearchMaxNode(node.Left) ?? SearchPreviousAncestor(node));
 }
Exemplo n.º 4
0
 public static BstNodeBase <TKey> SearchMaxNode <TKey>(this BstNodeBase <TKey> node)
 {
     if (node == null)
     {
         return(null);
     }
     return(SearchMaxNode(node.Right) ?? node);
 }
Exemplo n.º 5
0
 public static BstNodeBase <TKey> SearchNextNode <TKey>(this BstNodeBase <TKey> node)
 {
     if (node == null)
     {
         return(null);
     }
     return(SearchMinNode(node.Right) ?? SearchNextAncestor(node));
 }
Exemplo n.º 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);
 }
Exemplo n.º 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);
    }
Exemplo n.º 8
0
 public static BstNodeBase <TKey> SearchMaxNode <TKey>(this BstNodeBase <TKey> node, Func <TKey, bool> predicate)
 {
     if (node == null)
     {
         return(null);
     }
     if (predicate(node.Key))
     {
         return(SearchMaxNode(node.Right, predicate) ?? node);
     }
     else
     {
         return(SearchMaxNode(node.Left, predicate));
     }
 }
Exemplo n.º 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));
     }
 }
Exemplo n.º 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));
     }
 }
Exemplo n.º 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));
        }
    }