public void RotateRight() { if (!(left is RBBranch <T>)) { throw new InvalidOperationException("Can't rotate right a branch with a left leaf"); } RBBranch <T> newLocalRoot = (RBBranch <T>)left; RBNode <T> middle = newLocalRoot.right; if (parent != null) { if (this == parent.left) { parent.left = newLocalRoot; } else { parent.right = newLocalRoot; } } newLocalRoot.parent = this.parent; this.parent = newLocalRoot; newLocalRoot.right = this; this.left = middle; middle.parent = this; }
public RBBranch(T value, RBBranch <T> parent) { this.parent = parent; this.value = value; red = true; left = new RBLeaf <T> (this); right = new RBLeaf <T> (this); }
public T Successor() { if (right is RBBranch <T> ) { RBBranch <T> branch = (RBBranch <T>)right; return(branch.MinBranch().value); } return(NextRightParent().value); }
public T Predecessor() { if (left is RBBranch <T> ) { RBBranch <T> branch = (RBBranch <T>)left; return(branch.MaxBranch().value); } return(NextLeftParent().value); }
void Insert(RBBranch <T> node) { root.Insert(node); node.InsertRepair(); root = node; while (root.parent != null) { root = root.parent; } }
RBBranch <T> MinBranch() { if (!(left is RBBranch <T>)) { return(this); } RBBranch <T> branch = (RBBranch <T>)left; return(branch.MinBranch()); }
RBBranch <T> MaxBranch() { if (!(right is RBBranch <T>)) { return(this); } RBBranch <T> branch = (RBBranch <T>)right; return(branch.MaxBranch()); }
public void Delete() { RBBranch <T> minBranch = this; if (right is RBBranch <T> ) { RBBranch <T> rightBranch = (RBBranch <T>)right; minBranch = rightBranch.MinBranch(); value = minBranch.value; } minBranch.DeleteBranchWithNoMoreThanOneChildBranch(); }
public override void Insert(RBBranch <T> newNode) { if (parent != null) { if (parent.left == this) { parent.left = newNode; } else { parent.right = newNode; } } newNode.parent = parent; }
RBBranch <T> RepairRotateToOutside() { RBBranch <T> originalParent = parent; if (parent == Grandparent().left&& this == parent.right) { parent.RotateLeft(); return(originalParent); } else if (parent == Grandparent().right&& this == parent.left) { parent.RotateRight(); return(originalParent); } return(this); }
public override void Insert(RBBranch <T> newNode) { T t = newNode.value; int compareResult = t.CompareTo(value); if (compareResult == 0) { throw new ArgumentException(string.Format("Duplicate value {0} specified", t)); } else if (compareResult > 0) { right.Insert(newNode); } else { left.Insert(newNode); } }
public void Remove(T value) { root.Remove(value); RBBranch <T> rootBranch = (RBBranch <T>)root; if (rootBranch.left.parent == null) { root = rootBranch.left; } else if (rootBranch.right.parent == null) { root = rootBranch.right; } while (root.parent != null) { root = root.parent; } }
public RBLeaf(RBBranch <T> parent) { this.parent = parent; red = false; }
public abstract void Insert(RBBranch <T> newNode);
public void DeleteRepair() { if (parent == null) { return; } if (Sibling().red) { parent.red = true; Sibling().red = false; if (this == parent.left) { parent.RotateLeft(); } else { parent.RotateRight(); } } // Sibling is a branch (not a leaf) because it has more black children than we do. RBBranch <T> sibling = (RBBranch <T>)Sibling(); if ((!parent.red) && (!sibling.red) && (!sibling.left.red) && (!sibling.right.red)) { sibling.red = true; parent.DeleteRepair(); return; } if ((parent.red) && (!sibling.red) && (!sibling.left.red) && (!sibling.right.red)) { sibling.red = true; parent.red = false; return; } if ((this == parent.left) && (!sibling.right.red)) { sibling.left.red = false; sibling.red = true; sibling.RotateRight(); } else if ((this == parent.right) && (!sibling.left.red)) { sibling.left.red = false; sibling.red = true; sibling.RotateLeft(); } // Sibling is still a branch either from the earlier reasoning, or because our // earlier sibling was rotated and a rotation must put a branch at the root. sibling = (RBBranch <T>)Sibling(); sibling.red = parent.red; parent.red = false; if (this == parent.left) { sibling.right.red = false; parent.RotateLeft(); } else { sibling.left.red = false; parent.RotateRight(); } }