internal RBNode <T> Insert(T v) { RBNode <T> x = TreeInsert(v); InsertPrivate(x); return(ToNull(x)); }
void RightRotate(RBNode <T> x) { RBNode <T> y = x.left; x.left = y.right; if (y.right != nil) { y.right.parent = x; } y.parent = x.parent; if (x.parent == nil) { root = y; } else if (x == x.parent.right) { x.parent.right = y; } else { x.parent.left = y; } y.right = x; x.parent = y; }
RBNode <T> TreeInsert(T z) { var y = nil; var x = root; var compareRes = 0; while (x != nil) { y = x; #if SHARPKIT //https://code.google.com/p/sharpkit/issues/detail?id=368 compareRes = Comparer.Compare(z, x.Item); x = compareRes < 0 ? x.left : x.right; #else x = (compareRes = Comparer.Compare(z, x.Item)) < 0 ? x.left : x.right; #endif } var nz = new RBNode <T>(RBColor.Black, z, y, nil, nil); if (y == nil) { root = nz; } else if (compareRes < 0) { y.left = nz; } else { y.right = nz; } return(ToNull(nz)); }
internal RBNode(RBColor color, T item, RBNode <T> p, RBNode <T> left, RBNode <T> right) { this.color = color; this.parent = p; this.left = left; this.right = right; this.Item = item; }
RBNode <T> TreeMaximum(RBNode <T> x) { while (x.right != nil) { x = x.right; } return(ToNull(x)); }
RBNode <T> TreeMinimum(RBNode <T> x) { while (x.left != nil) { x = x.left; } return(ToNull(x)); }
void InsertPrivate(RBNode <T> x) { count++; x.color = RBColor.Red; while (x != root && x.parent.color == RBColor.Red) { if (x.parent == x.parent.parent.left) { RBNode <T> y = x.parent.parent.right; if (y.color == RBColor.Red) { x.parent.color = RBColor.Black; y.color = RBColor.Black; x.parent.parent.color = RBColor.Red; x = x.parent.parent; } else { if (x == x.parent.right) { x = x.parent; LeftRotate(x); } x.parent.color = RBColor.Black; x.parent.parent.color = RBColor.Red; RightRotate(x.parent.parent); } } else { RBNode <T> y = x.parent.parent.left; if (y.color == RBColor.Red) { x.parent.color = RBColor.Black; y.color = RBColor.Black; x.parent.parent.color = RBColor.Red; x = x.parent.parent; } else { if (x == x.parent.left) { x = x.parent; RightRotate(x); } x.parent.color = RBColor.Black; x.parent.parent.color = RBColor.Red; LeftRotate(x.parent.parent); } } } root.color = RBColor.Black; //checkTheTree(); }
internal RBNode <T> Remove(T i) { RBNode <T> n = Find(i); if (n != null) { count--; return(DeleteSubtree(n)); } return(null); }
RBNode <T> Find(RBNode <T> x, T i) { int compareResult; while (x != nil && (compareResult = Comparer.Compare(i, x.Item)) != 0) { x = compareResult < 0 ? x.left : x.right; } return(ToNull(x)); }
internal RBNode <T> DeleteSubtree(RBNode <T> z) { System.Diagnostics.Debug.Assert(z != nil); RBNode <T> y; if (z.left == nil || z.right == nil) { /* y has a nil node as a child */ y = z; } else { /* find tree successor with a nil node as a child */ y = z.right; while (y.left != nil) { y = y.left; } } /* x is y's only child */ RBNode <T> x = y.left != nil ? y.left : y.right; x.parent = y.parent; if (y.parent == nil) { root = x; } else { if (y == y.parent.left) { y.parent.left = x; } else { y.parent.right = x; } } if (y != z) { z.Item = y.Item; } if (y.color == RBColor.Black) { DeleteFixup(x); } // checkTheTree(); return(ToNull(z)); }
RBNode <T> FindLast(RBNode <T> n, Func <T, bool> p) { if (n == nil) { return(null); } RBNode <T> good = null; while (n != nil) { n = p(n.Item) ? (good = n).right : n.left; } return(good); }
internal RBNode <T> Previous(RBNode <T> x) { if (x.left != nil) { return(TreeMaximum(x.left)); } RBNode <T> y = x.parent; while (y != nil && x == y.left) { x = y; y = y.parent; } return(ToNull(y)); }
internal RBNode <T> Next(RBNode <T> x) { if (x.right != nil) { return(TreeMinimum(x.right)); } RBNode <T> y = x.parent; while (y != nil && x == y.right) { x = y; y = y.parent; } return(ToNull(y)); }
public bool MoveNext() { if (tree.IsEmpty()) { return(false); } if (initialState == true) { initialState = false; c = tree.TreeMinimum(); } else { c = tree.Next(c); } return(c != null); }
internal void Clear() { root = nil = new RBNode <T>(RBColor.Black); }
internal RbTree() { comparer = new DefaultComperer <T>(); root = nil = new RBNode <T>(RBColor.Black); }
void DeleteFixup(RBNode <T> x) { while (x != root && x.color == RBColor.Black) { if (x == x.parent.left) { RBNode <T> w = x.parent.right; if (w.color == RBColor.Red) { w.color = RBColor.Black; x.parent.color = RBColor.Red; LeftRotate(x.parent); w = x.parent.right; } if (w.left.color == RBColor.Black && w.right.color == RBColor.Black) { w.color = RBColor.Red; x = x.parent; } else { if (w.right.color == RBColor.Black) { w.left.color = RBColor.Black; w.color = RBColor.Red; RightRotate(w); w = x.parent.right; } w.color = x.parent.color; x.parent.color = RBColor.Black; w.right.color = RBColor.Black; LeftRotate(x.parent); x = root; } } else { RBNode <T> w = x.parent.left; if (w.color == RBColor.Red) { w.color = RBColor.Black; x.parent.color = RBColor.Red; RightRotate(x.parent); w = x.parent.left; } if (w.right.color == RBColor.Black && w.left.color == RBColor.Black) { w.color = RBColor.Red; x = x.parent; } else { if (w.left.color == RBColor.Black) { w.right.color = RBColor.Black; w.color = RBColor.Red; LeftRotate(w); w = x.parent.left; } w.color = x.parent.color; x.parent.color = RBColor.Black; w.left.color = RBColor.Black; RightRotate(x.parent); x = root; } } } x.color = RBColor.Black; }
internal void DeleteNodeInternal(RBNode <T> x) { count--; DeleteSubtree(x); }
internal RbTree(IComparer <T> comparer) { root = nil = new RBNode <T>(RBColor.Black); this.comparer = comparer; }
RBNode <T> ToNull(RBNode <T> y) { return(y != nil ? y : null); }