public void insert(double data) { if (root == null) { root = new RBTreeNode(data); } else { RBTreeNode parent = null; RBTreeNode current = root; do { parent = current; if (current.Data >= data) { current = current.Left; } else { current = current.Right; } } while (current != null); current = new RBTreeNode(data, null, null, parent); if (current.Data >= data) { parent.Left = current; } else { parent.Right = current; } fixAfterInsert(current); } }
private void InsertCase1_2(RBTreeNode <TKey> node) { var parent = node.parent; if (parent == null) { node.color = NodeColor.Black; return; } else if (parent.color == NodeColor.Black) //case 2 parent is black; { return; } else { InsertCase3(node); } }
/// <summary> /// Initializes an new instance of Collections.System.RBTreeEnumerator /// class. The current position is before the first element. /// </summary> /// <param name="t">The RBTree which will be enumerate.</param> internal Enumerator(RBTree <T> t) { tree = t; if (t.root == null && t.Count > 0) { throw new InvalidOperationException("The RBTree has null root but non-zero size"); } started = false; isValid = true; current = tree.root; if (current != null) { while (current.Left != null) { current = current.Left; } } }
public void LinkNode(RBTreeNode node1, RBTreeNode node2) { if (node1 == null || node2 == null) { return; } var pos1 = node1.trans.anchoredPosition; var pos2 = node2.trans.anchoredPosition; if (pos1 == pos2) { return; } trans.anchoredPosition = (pos1 + pos2) * 0.5f; trans.rotation = Quaternion.Euler(0, 0, _CalcAngle(pos1, pos2)); trans.sizeDelta = new Vector3(Vector3.Distance(pos1, pos2), 5); }
// copy entire subtree, recursively private RBTreeNode <T> Copy(RBTreeNode <T> oldRoot, RBTreeNode <T> newFather) { RBTreeNode <T> newRoot = null; // copy a node, then any subtrees if (oldRoot != null) { newRoot = new RBTreeNode <T>(oldRoot); if (newFather != null) { newRoot.Father = newFather; } newRoot.Left = Copy(oldRoot.Left, newRoot); newRoot.Right = Copy(oldRoot.Right, newRoot); } return(newRoot); // return newly constructed tree }
private static void PreOrderTraversal(RBTreeNode <T> root, RBTreeNode <T> sentryNode, List <string> contents, Dictionary <RBTreeNode <T>, string> dictionary) { if (root != sentryNode) { var rootName = GetTreeNodeName(root, dictionary); if (root.Left != sentryNode) { var leftName = GetTreeNodeName(root.Left, dictionary); contents.Add($"\"{rootName}\":f0 -> \"{leftName}\":f1;"); } if (root.Right != sentryNode) { var rightName = GetTreeNodeName(root.Right, dictionary); contents.Add($"\"{rootName}\":f2 -> \"{rightName}\":f1;"); } PreOrderTraversal(root.Left, sentryNode, contents, dictionary); PreOrderTraversal(root.Right, sentryNode, contents, dictionary); } }
void Update() { if (DoInsert) { if (!string.IsNullOrEmpty(values)) { var temps = values.Split(' '); foreach (var value in temps) { AddValue(int.Parse(value)); } } else { AddValue(Random.Range(1, 101)); } DoInsert = false; } if (DoDelete) { if (!string.IsNullOrEmpty(values)) { var temps = values.Split(' '); foreach (var value in temps) { DelValue(int.Parse(value)); } } else { DelValue(Random.Range(1, 101)); } DoDelete = false; } if (DoResetx) { _RemoveNode(m_root, true); m_root = null; DoResetx = false; } }
private RBTreeNode <T> UpperBoundNode(T x) { RBTreeNode <T> node = root; RBTreeNode <T> upperBoundNode = null; while (node != null) { if (Comparer.Compare(x, node.Key) < 0) { // node greater than x, remember it upperBoundNode = node; node = node.Left; // descend left subtree } else { node = node.Right; // descend right subtree } } return(upperBoundNode); // return best remembered candidate }
public RBTreeNode search(double data) { RBTreeNode loop = root; while (loop != null) { if (loop.Data == data) { return(loop); } if (loop.Data < data) { loop = loop.Right; } else { loop = loop.Left; } } return(null); }
/* * 找结点(x)的后继结点。即,查找"红黑树中数据值大于该结点"的"最小结点"。 */ public RBTreeNode <TKey> Successor(RBTreeNode <TKey> x) { // 如果x存在右孩子,则"x的后继结点"为 "以其右孩子为根的子树的最小结点"。 if (x.right != null) { return(DsAndAlg.BstUtils.Min(x.right)); } // 如果x没有右孩子。则x有以下两种可能: // (01) x是"一个左孩子",则"x的后继结点"为 "它的父结点"。 // (02) x是"一个右孩子",则查找"x的最低的父结点,并且该父结点要具有左孩子",找到的这个"最低的父结点"就是"x的后继结点"。 var y = x.parent; while ((y != null) && (x == y.right)) { x = y; y = y.parent; } return(y); }
/* * 找结点(x)的前驱结点。即,查找"红黑树中数据值小于该结点"的"最大结点"。 */ public RBTreeNode <TKey> Predecessor(RBTreeNode <TKey> x) { // 如果x存在左孩子,则"x的前驱结点"为 "以其左孩子为根的子树的最大结点"。 if (x.left != null) { return(DsAndAlg.BstUtils.Max(x.left)); } // 如果x没有左孩子。则x有以下两种可能: // (01) x是"一个右孩子",则"x的前驱结点"为 "它的父结点"。 // (01) x是"一个左孩子",则查找"x的最低的父结点,并且该父结点要具有右孩子",找到的这个"最低的父结点"就是"x的前驱结点"。 var y = x.parent; while ((y != null) && (x == y.left)) { x = y; y = y.parent; } return(y); }
//删除节点修正 protected void _FixDelNode(RBTreeNode deletedNode, RBTreeNode replaceNode) { if (deletedNode.IsRed) { return; } if (deletedNode.parent == null) { return; } //删黑,有右子(必红), 涂黑即可 if (replaceNode.IsRed) { replaceNode.SetBlack(); return; } if (_FixDelNodeCase1(replaceNode)) { return; } }
private RBTreeNode <TKey> _Find(RBTreeNode <TKey> root, TKey key) { RBTreeNode <TKey> p = root; while (p != null) { var cmp = p.value.CompareTo(key); if (cmp == 0) { return(p); } else if (cmp > 0) { p = p.left; } else { p = p.right; } } return(p); }
/// <summary> /// Removes the first occurrence of the element in the RBTree. /// The operation is performed in a guaranteed logarithmic time /// of the RBTree size. /// </summary> public bool Remove(T x) { //rwLock.AcquireWriterLock( Timeout.Infinite ); try { RBTreeNode <T> node = FindNode(x); if (node != null) { RemoveRBTreeNode(node); //if( Count != EnumeratedCount ) throw new InvalidOperationException( "EnumeratedCount does not match stored count" ); return(true); } else { return(false); } } finally { //rwLock.ReleaseWriterLock(); } }
protected RBTreeNode _RemoveNode(RBTreeNode node, bool includeChild = false) { if (node == null) { return(null); } if (node.parentLink != null) { Destroy(node.parentLink.gameObject); node.parentLink = null; } Destroy(node.gameObject); if (includeChild) { if (node.isNil) { return(null); } _RemoveNode(node.lNode, true); _RemoveNode(node.rNode, true); return(null); } RBTreeNode childNode = null; if (node.lNode.isNil) { childNode = node.rNode; _RemoveNode(node.lNode, true); } else { childNode = node.lNode; _RemoveNode(node.rNode, true); } _ChangeParent(node.parent, node, childNode); return(childNode); }
/// <summary> /// Returns a node of the tree which contains the object /// as Key. If the tree does not contain such node, then /// null is returned. /// </summary> /// <param name="node">The root of the tree.</param> /// <param name="x">The researched object.</param> private RBTreeNode <T> RecContains(RBTreeNode <T> node, T x) { if (node == null) { return(null); } int c = Comparer.Compare(x, node.Key); if (c == 0) { return(node); } if (c < 0) { return(RecContains(node.Left, x)); } else { return(RecContains(node.Right, x)); } }
void rotate_left(RBTreeNode <TKey> p) { if (p.parent == null) { root_ = p; return; } var gp = Grandparent(p); var fa = p.parent; var y = p.left; fa.right = y; if (y != null) { y.parent = fa; } p.left = fa; fa.parent = p; if (root_ == fa) { root_ = p; } p.parent = gp; if (gp != null) { if (gp.left == fa) { gp.left = p; } else { gp.right = p; } } }
//替换节点 node1的父节点设置为node2的父节点 protected void _ChangeParent(RBTreeNode parent, RBTreeNode node1, RBTreeNode node2) { if (parent == null)//说明是跟节点 { m_root = node2; m_root.parent = null; return; } if (parent.lNode == node1) { node2.SetLeft(); node2.parent = parent; node2.parent.lNode = node2; return; } if (parent.rNode == node1) { node2.SetRight(); node2.parent = parent; node2.parent.rNode = node2; return; } }
/// <summary> /// 假设不重复值 /// </summary> /// <param name="node"></param> /// <param name="value"></param> private void Insert(RBTreeNode <TKey> node, TKey data) { var p = node; if (p.value.CompareTo(data) >= 0) { if (p.left != null) { Insert(p.left, data); } else { var tmp = new RBTreeNode <TKey>(); tmp.value = data; tmp.left = tmp.right = null; tmp.parent = p; p.left = tmp; InsertCase1_2(tmp); } } else { if (p.right != null) { Insert(p.right, data); } else { var tmp = new RBTreeNode <TKey>(); tmp.value = data; tmp.left = tmp.right = null; tmp.parent = p; p.right = tmp; InsertCase1_2(tmp); } } }
private static double LeftBreakpoint(RBTreeNode <BeachSection> node, double directrix) { var leftNode = node.Previous; //degenerate parabola if ((node.Data.Site.Y - directrix).ApproxEqual(0)) { return(node.Data.Site.X); } //node is the first piece of the beach line if (leftNode == null) { return(double.NegativeInfinity); } //left node is degenerate if ((leftNode.Data.Site.Y - directrix).ApproxEqual(0)) { return(leftNode.Data.Site.X); } var site = node.Data.Site; var leftSite = leftNode.Data.Site; return(ParabolaMath.IntersectParabolaX(leftSite.X, leftSite.Y, site.X, site.Y, directrix)); }
private static double RightBreakpoint(RBTreeNode <BeachSection> node, double directrix) { var rightNode = node.Next; //degenerate parabola if ((node.Data.Site.Y - directrix).ApproxEqual(0)) { return(node.Data.Site.X); } //node is the last piece of the beach line if (rightNode == null) { return(double.PositiveInfinity); } //left node is degenerate if ((rightNode.Data.Site.Y - directrix).ApproxEqual(0)) { return(rightNode.Data.Site.X); } var site = node.Data.Site; var rightSite = rightNode.Data.Site; return(ParabolaMath.IntersectParabolaX(site.X, site.Y, rightSite.X, rightSite.Y, directrix)); }
/// <summary> /// Returns the node that contains the successor of node.Key. /// If such node does not exist then null is returned. /// </summary> /// <param name="node"> /// node must be contained by RBTree.</param> private RBTreeNode <T> Successor(RBTreeNode <T> node) { RBTreeNode <T> node1, node2; if (node.Right != null) { // We find the Min node1 = node.Right; while (node1.Left != null) { node1 = node1.Left; } return(node1); } node1 = node; node2 = node.Father; while (node2 != null && node1 == node2.Right) { node1 = node2; node2 = node2.Father; } return(node2); }
private RBTreeNode <T> LowerBoundNode(T x) { RBTreeNode <T> node = root; RBTreeNode <T> lowerBoundNode = null; int compare; while (node != null) { compare = Comparer.Compare(node.Key, x); if (compare < 0) { node = node.Right; // descend right subtree } else { // node not less than x, remember it lowerBoundNode = node; node = node.Left; // descend left subtree } } return(lowerBoundNode); // return best remembered candidate }
//父红,叔红 protected bool _FixAddNodeCase5(RBTreeNode node) { var uncle = _GetUncleNode(node); if (uncle == null) { return(true); } if (!uncle.IsRed) { return(false); } node.parent.SetBlack(); uncle.SetBlack(); var grand = node.parent.parent; grand.SetRed(); if (grand.parent != null && grand.parent.IsRed) { return(_FixAddNodeCase(node.parent.parent)); } return(true); }
private RBTreeNode <TKey> ReplaceNode(RBTreeNode <TKey> x) { // when node have 2 children if (x.left != null && x.right != null) { return(Successor(x.right)); } // when leaf if (x.left == null && x.right == null) { return(null); } // when single child if (x.left != null) { return(x.left); } else { return(x.right); } }
protected bool _FixAddNodeCase(RBTreeNode node) { if (_FixAddNodeCase5(node)) { return(true); } if (_FixAddNodeCase1(node)) { return(true); } if (_FixAddNodeCase2(node)) { return(true); } if (_FixAddNodeCase3(node)) { return(true); } if (_FixAddNodeCase4(node)) { return(true); } return(false); }
private void fixAfterDelete(RBTreeNode x) { while (x != root && colorOf(x) == CType.BLACK) { if (leftOf(parentOf(x)) == x) { RBTreeNode uncle = rightOf(parentOf(x)); if (colorOf(uncle)==CType.RED) { setColor(uncle, CType.BLACK); setColor(parentOf(x), CType.RED); rotateLeft(parentOf(x)); uncle = rightOf(parentOf(x)); } if (colorOf(leftOf(uncle)) == CType.BLACK && colorOf(rightOf(x))==CType.RED) { setColor(uncle, CType.RED); x = parentOf(x); } else { if (colorOf(rightOf(uncle)) == CType.BLACK) { setColor(leftOf(uncle), CType.BLACK); setColor(uncle, CType.RED); rotateRight(uncle); uncle = rightOf(parentOf(x)); } setColor(uncle, colorOf(parentOf(x))); setColor(parentOf(x), CType.BLACK); setColor(rightOf(uncle), CType.BLACK); rotateLeft(parentOf(x)); x = root; } } else { RBTreeNode uncle = leftOf(parentOf(x)); if (colorOf(uncle) == CType.RED) { setColor(uncle, CType.BLACK); setColor(parentOf(x), CType.RED); rotateRight(parentOf(x)); uncle = leftOf(parentOf(x)); } if (colorOf(leftOf(uncle)) == CType.BLACK && colorOf(rightOf(uncle))==CType.BLACK) { setColor(uncle, CType.RED); x = parentOf(x); } else { if (colorOf(leftOf(uncle))==CType.BLACK) { setColor(rightOf(uncle), CType.BLACK); setColor(uncle, CType.RED); rotateLeft(uncle); uncle = leftOf(parentOf(x)); } setColor(uncle, colorOf(parentOf(x))); setColor(parentOf(x), CType.BLACK); setColor(leftOf(uncle), CType.BLACK); rotateRight(parentOf(x)); x = root; } } } setColor(x, CType.BLACK); }
//兄有红左子,兄与左子换色,兄右旋,然后_FixDelNodeCase2_1 protected bool _FixDelNodeCase2_2(RBTreeNode node) { return(false); }
private void fixAfterInsert(RBTreeNode x) { x.Color = CType.RED; if (parentOf(x) == leftOf(parentOf(parentOf(x)))) { RBTreeNode uncle = rightOf(parentOf(parentOf(x))); if (colorOf(uncle) == CType.RED) { setColor(uncle, CType.BLACK); setColor(parentOf(x), CType.BLACK); setColor(parentOf(parentOf(x)), CType.RED); x = parentOf(parentOf(x)); } else { if (rightOf(parentOf(x)) == x) { x = parentOf(x); rotateLeft(x); } setColor(parentOf(x), CType.BLACK); setColor(parentOf(parentOf(x)), CType.RED); rotateRight(parentOf(parentOf(x))); } } else { RBTreeNode uncle = leftOf(parentOf(parentOf(x))); if (colorOf(uncle) == CType.RED) { setColor(uncle, CType.BLACK); setColor(parentOf(x), CType.BLACK); setColor(parentOf(parentOf(x)), CType.RED); x = parentOf(parentOf(x)); } else { if (leftOf(parentOf(x)) == x) { x = parentOf(x); rotateRight(x); } setColor(parentOf(x), CType.BLACK); setColor(parentOf(parentOf(x)), CType.RED); rotateLeft(parentOf(parentOf(x))); } } setColor(root, CType.BLACK); }
private RBTreeNode leftOf(RBTreeNode x) { return x == null ? null : x.Left; }
private RBTreeNode parentOf(RBTreeNode x) { return x == null ? null : x.Parent; }
//TODO: Clean this up public void RemoveNode(RBTreeNode <T> node) { //fix up linked list structure if (node.Next != null) { node.Next.Previous = node.Previous; } if (node.Previous != null) { node.Previous.Next = node.Next; } //replace the node var original = node; var parent = node.Parent; var left = node.Left; var right = node.Right; RBTreeNode <T> next; //figure out what to replace this node with if (left == null) { next = right; } else if (right == null) { next = left; } else { next = GetFirst(right); } //fix up the parent relation if (parent != null) { if (parent.Left == node) { parent.Left = next; } else { parent.Right = next; } } else { Root = next; } bool red; if (left != null && right != null) { red = next.Red; next.Red = node.Red; next.Left = left; left.Parent = next; // if we reached down the tree if (next != right) { parent = next.Parent; next.Parent = node.Parent; node = next.Right; parent.Left = node; next.Right = right; right.Parent = next; } else { // the direct right will replace the node next.Parent = parent; parent = next; node = next.Right; } } else { red = node.Red; node = next; } if (node != null) { node.Parent = parent; } if (red) { return; } if (node != null && node.Red) { node.Red = false; return; } //node is null or black // fair warning this code gets nasty //how do we guarantee sibling is not null RBTreeNode <T> sibling = null; do { if (node == Root) { break; } if (node == parent.Left) { sibling = parent.Right; if (sibling.Red) { sibling.Red = false; parent.Red = true; RotateLeft(parent); sibling = parent.Right; } if ((sibling.Left != null && sibling.Left.Red) || (sibling.Right != null && sibling.Right.Red)) { //pretty sure this can be sibling.Left!= null && sibling.Left.Red if (sibling.Right == null || !sibling.Right.Red) { sibling.Left.Red = false; sibling.Red = true; RotateRight(sibling); sibling = parent.Right; } sibling.Red = parent.Red; parent.Red = sibling.Right.Red = false; RotateLeft(parent); node = Root; break; } } else { sibling = parent.Left; if (sibling.Red) { sibling.Red = false; parent.Red = true; RotateRight(parent); sibling = parent.Left; } if ((sibling.Left != null && sibling.Left.Red) || (sibling.Right != null && sibling.Right.Red)) { if (sibling.Left == null || !sibling.Left.Red) { sibling.Right.Red = false; sibling.Red = true; RotateLeft(sibling); sibling = parent.Left; } sibling.Red = parent.Red; parent.Red = sibling.Left.Red = false; RotateRight(parent); node = Root; break; } } sibling.Red = true; node = parent; parent = parent.Parent; } while (!node.Red); if (node != null) { node.Red = false; } }
private void rotateRight(RBTreeNode p) { if (p != null) { RBTreeNode x = leftOf(p); RBTreeNode tail = rightOf(x); p.Left = tail; if (tail != null) { tail.Parent = p; } x.Parent = p.Parent; if (parentOf(p) == null) { root = x; } else if (rightOf(parentOf(p)) == p) { p.Parent.Right = x; } else { p.Parent.Left = x; } x.Right = p; p.Parent = x; } }
private RBTreeNode rightOf(RBTreeNode x) { return x == null ? null : x.Right; }
private void setColor(RBTreeNode x, CType color) { if (x != null) { x.Color = color; } }
public bool IsOnLeft(RBTreeNode <TKey> n) => n.parent.left == n;
public RBTreeNode(double data, RBTreeNode left, RBTreeNode right, RBTreeNode parent) { Color = CType.BLACK; this.Data = data; this.Left = left; this.Right = right; this.Parent = parent; }
private CType colorOf(RBTreeNode x) { return x == null ? CType.BLACK : x.Color; }
public RBTreeNode <T> InsertSuccessor(RBTreeNode <T> node, T successorData) { var successor = new RBTreeNode <T> { Data = successorData }; RBTreeNode <T> parent; if (node != null) { //insert new node between node and its successor successor.Previous = node; successor.Next = node.Next; if (node.Next != null) { node.Next.Previous = successor; } node.Next = successor; //insert successor into the tree if (node.Right != null) { node = GetFirst(node.Right); node.Left = successor; } else { node.Right = successor; } parent = node; } else if (Root != null) { //if the node is null, successor must be inserted //into the left most part of the tree node = GetFirst(Root); //successor.Previous = null; successor.Next = node; node.Previous = successor; node.Left = successor; parent = node; } else { //first insert //successor.Previous = successor.Next = null; Root = successor; parent = null; } //successor.Left = successor.Right = null; successor.Parent = parent; successor.Red = true; //the magic of the red black tree RBTreeNode <T> grandma; RBTreeNode <T> aunt; node = successor; while (parent != null && parent.Red) { grandma = parent.Parent; if (parent == grandma.Left) { aunt = grandma.Right; if (aunt != null && aunt.Red) { parent.Red = false; aunt.Red = false; grandma.Red = true; node = grandma; } else { if (node == parent.Right) { RotateLeft(parent); node = parent; parent = node.Parent; } parent.Red = false; grandma.Red = true; RotateRight(grandma); } } else { aunt = grandma.Left; if (aunt != null && aunt.Red) { parent.Red = false; aunt.Red = false; grandma.Red = true; node = grandma; } else { if (node == parent.Left) { RotateRight(parent); node = parent; parent = node.Parent; } parent.Red = false; grandma.Red = true; RotateLeft(grandma); } } parent = node.Parent; } Root.Red = false; return(successor); }
public void Initialize(VPoint lowest, double yCenter, RBTreeNode <BeachSection> toDelete) { Lowest = lowest; YCenter = yCenter; ToDelete = toDelete; }
public void remove(double data) { RBTreeNode pending = search(data); if (pending == null) { return; } if (pending.Left != null && pending.Right != null) { RBTreeNode l = pending.Left; while (l.Right != null) { l = l.Right; } pending.Data = l.Data; pending = l; } RBTreeNode replace = pending.Left != null ? pending.Left : pending.Right; if (replace != null) { replace.Parent = pending.Parent; if (parentOf(pending) == null) { root = replace; } else if (leftOf(parentOf(pending)) == pending) { pending.Parent.Left = replace; } else { pending.Parent.Right = replace; } pending = pending.Left = pending.Right = pending.Parent = null; if (colorOf(pending) == CType.BLACK) { fixAfterDelete(replace); } } else if (parentOf(replace) == null) { root = null; } else { if (colorOf(pending) == CType.BLACK) { fixAfterDelete(pending); } if (parentOf(pending) != null) { if (leftOf(parentOf(pending)) == pending) { pending.Parent.Left = null; } else if (rightOf(parentOf(pending)) == pending) { pending.Parent.Right = null; } pending.Parent = null; } } }