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); } }
public static IEnumerable <TKey> GetKeys <TKey>(this BstNodeBase <TKey> node) { for (var n = SearchMinNode(node); n != null; n = SearchNextNode(n)) { yield return(n.Key); } }
public static BstNodeBase <TKey> SearchPreviousNode <TKey>(this BstNodeBase <TKey> node) { if (node == null) { return(null); } return(SearchMaxNode(node.Left) ?? SearchPreviousAncestor(node)); }
public static BstNodeBase <TKey> SearchMaxNode <TKey>(this BstNodeBase <TKey> node) { if (node == null) { return(null); } return(SearchMaxNode(node.Right) ?? node); }
public static BstNodeBase <TKey> SearchNextNode <TKey>(this BstNodeBase <TKey> node) { if (node == null) { return(null); } return(SearchMinNode(node.Right) ?? SearchNextAncestor(node)); }
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); }
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); }
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)); } }
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)); } }
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)); } }
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)); } }