private void RightRotate(RBNodeBase x) { RBNodeBase y = x._left; x._left = y._right; if (y._right != _null) { y._right._parent = x; } y._parent = x._parent; RBNodeBase xParent = x._parent; if (xParent == _null) { _root = y; } else if (x == xParent._right) { xParent._right = y; } else { xParent._left = y; } y._right = x; x._parent = y; }
public RBNodeBase GetMaximumLess(object searchKey) { if (_root == _null) { return(null); } RBNodeBase x = _root; RBNodeBase prev = _null; int iCompare; while (x != _null) { iCompare = CompareKeys(searchKey, x.Key); if (iCompare <= 0) { x = x._left; } else { prev = x; x = x._right; } } if (prev == _null) { return(null); } return(prev); }
public RBNodeBase GetEqualOrMore(object searchKey, object key2) { if (_root == _null) { return(null); } RBNodeBase x = _root; RBNodeBase next = _null; int iCompare; while (x != _null) { iCompare = CompareKeys(searchKey, x.Key); if (iCompare == 0) { return(x); } if (iCompare > 0) { x = x._right; } else { next = x; x = x._left; } } if (next == _null) { return(null); } return(next); }
private void Insert(RBNodeBase node) { RBNodeBase current = _root; RBNodeBase parent = _null; object insertedKey = node.Key; while (current != _null) { parent = current; if (CompareKeys(insertedKey, current.Key) < 0) { current = current._left; } else { current = current._right; } } node._parent = parent; if (parent == _null) { _root = node; return; } if (CompareKeys(insertedKey, parent.Key) < 0) { parent._left = node; } else { parent._right = node; } }
public RedBlackTree() { _null = new RBNode(); _null.Key = "sentinel"; _null._left = _null; _null._right = _null; _null._parent = _null; _root = _null; }
private BTreeNode SearchBTreeNode(IFixedLengthKey key, int offset, out RBNodeBase rbNode) { _searchNode.ChangeMinKey(key, offset); rbNode = _rbTree.GetEqualOrLess(_searchNode); if (rbNode == null) { rbNode = _rbTree.GetMinimumNode(); } return((rbNode != null) ? (BTreeNode)rbNode.Key : null); }
private void RB_Insert(RBNodeBase x) { x._color = RBColor.Red; while (x != _root && x._parent._color == RBColor.Red) { RBNodeBase xParent = x._parent; RBNodeBase xParentParent = xParent._parent; if (xParent == xParentParent._left) { RBNodeBase y = xParentParent._right; if (/*y != _null && */ y._color == RBColor.Red) { xParent._color = RBColor.Black; y._color = RBColor.Black; xParentParent._color = RBColor.Red; x = xParentParent; } else if (x == xParent._right) { x = xParent; LeftRotate(x); } else { xParent._color = RBColor.Black; xParentParent._color = RBColor.Red; RightRotate(xParentParent); } } else { RBNodeBase y = xParentParent._left; if (/*y != _null && */ y._color == RBColor.Red) { xParent._color = RBColor.Black; y._color = RBColor.Black; xParentParent._color = RBColor.Red; x = xParentParent; } else if (x == xParent._left) { x = xParent; RightRotate(x); } else { xParent._color = RBColor.Black; xParentParent._color = RBColor.Red; LeftRotate(xParentParent); } } } _root._color = RBColor.Black; }
private RBNodeBase GetMinimum(RBNodeBase x) { if (_root == _null) { return(null); } while (x._left != _null) { x = x._left; } return(x); }
private RBNodeBase GetMaximum(RBNodeBase x) { if (_root == _null) { return(null); } while (x._right != _null) { x = x._right; } return(x); }
private void InOrderEnum(INodeFetcher fetcher, RBNodeBase node) { fetcher.NodeFetched(node); if (node._left != _null) { InOrderEnum(fetcher, node._left); } if (node._right != _null) { InOrderEnum(fetcher, node._right); } }
public RBNodeBase GetPrevious(RBNodeBase x) { RBNodeBase node = GetPredecessor(x); if (node != _null) { return(node); } else { return(null); } }
public RBNodeBase GetNext(RBNodeBase x) { RBNodeBase node = GetSuccessor(x); if (node != _null) { return(node); } else { return(null); } }
public void RB_Delete(RBNodeBase node) { if (node == null || _count == 0) { return; } _count--; RBNodeBase z = node; RBNodeBase y; if (z._left == _null || z._right == _null) { y = z; } else { y = GetSuccessor(z); } RBNodeBase x; if (y._left != _null) { x = y._left; } else { x = y._right; } x._parent = y._parent; if (y._parent == _null) { _root = x; } else if (y == y._parent._left) { y._parent._left = x; } else { y._parent._right = x; } if (y != z) { z.Key = y.Key; // copy of additional data } if (y._color == RBColor.Black) { RB_Delete_Fixup(x); } }
public override void GetAllKeys(ArrayList keys_offsets) { RBNodeBase rbNode = _rbTree.GetMinimumNode(); while (rbNode != null) { BTreePage page = PreparePage((BTreeNode)rbNode.Key); if (page.GetAllKeys(keys_offsets) == 0) { break; } rbNode = _rbTree.GetNext(rbNode); } }
public RBNodeBase GetSuccessor(RBNodeBase x) { if (x._right != _null) { return(GetMinimum(x._right)); } RBNodeBase y = x._parent; while (y != _null && x == y._right) { x = y; y = y._parent; } return(y); }
public RBNodeBase GetPredecessor(RBNodeBase x) { if (x._left != _null) { return(GetMaximum(x._left)); } RBNodeBase y = x._parent; while (y != _null && x == y._left) { x = y; y = y._parent; } return(y); }
public override void SearchForRange(IFixedLengthKey beginKey, IFixedLengthKey endKey, ArrayList keys_offsets) { _searchNode.ChangeMinKey(beginKey, 0); RBNodeBase rbNode = _rbTree.GetMaximumLess(_searchNode); if (rbNode == null) { rbNode = _rbTree.GetMinimumNode(); } while (rbNode != null && ((BTreeNode)rbNode.Key).MinKey.CompareTo(endKey) <= 0) { BTreePage page = PreparePage((BTreeNode)rbNode.Key); page.SearchForRange(beginKey, endKey, keys_offsets); rbNode = _rbTree.GetNext(rbNode); } }
public bool SearchOrInsert(object insertedKey, out RBNodeBase foundOrNew) { RBNodeBase current = _root; RBNodeBase parent = _null; while (current != _null) { parent = current; int iCompare = CompareKeys(insertedKey, current.Key); if (iCompare < 0) { current = current._left; } else if (iCompare > 0) { current = current._right; } else { foundOrNew = current; return(true); } } _count++; foundOrNew = new RBNode(insertedKey, _null); foundOrNew._parent = parent; if (parent == _null) { _root = foundOrNew; RB_Insert(foundOrNew); return(false); } if (CompareKeys(insertedKey, parent.Key) < 0) { parent._left = foundOrNew; } else { parent._right = foundOrNew; } RB_Insert(foundOrNew); return(false); }
public override void InsertKey(IFixedLengthKey key, int offset) { _count++; RBNodeBase rbNode; BTreeNode foundNode = SearchBTreeNode(key, offset, out rbNode); if (foundNode == null) { if (_rbTree.Count == 0) { BTreeNode newNode = new BTreeNode(key.FactoryMethod(), offset, GetOffsetForNewPage()); _rbTree.RB_Insert(newNode); BTreePage page = NewPage(newNode); page.InsertKey(key, offset); page.Write(); _cache.CacheObject(newNode.PageOffset, page); return; } else { RBNodeBase rbMinNode = _rbTree.GetMinimumNode(); foundNode = (BTreeNode)rbMinNode.Key; } } else { BTreePage page = PreparePage(foundNode); if (page.Full()) { float splitFactor = (_rbTree.GetNext(rbNode) == null) ? 0.875f : 0.5f; BTreePage splittedPage = page.Split(key, offset, GetOffsetForNewPage(), splitFactor); _rbTree.RB_Insert(splittedPage.BTreeNode); splittedPage.Write(); _cache.CacheObject(splittedPage.BTreeNode.PageOffset, splittedPage); } else { page.InsertKey(key, offset); } } }
private void InorderPrint(RBNodeBase node) { /* * if ( node != _null ) * { * if ( node.Key.Equals( _root.Key ) ) * { * Trace.WriteLine("___ root ___"); * Console.WriteLine("___ root ___"); * } * node.Print(); * if ( node.Key.Equals( _root.Key ) ) * { * Trace.WriteLine("___ root ___"); * Console.WriteLine("___ root ___"); * } * * if ( node._left != _null ) * { * Trace.Indent(); * Trace.WriteLine("go left ___ begin"); * Console.WriteLine("go left ___ begin"); * InorderPrint( node._left ); * Trace.WriteLine("go left ___ end"); * Console.WriteLine("go left ___ end"); * Trace.Unindent(); * } * if ( node._right != _null ) * { * Trace.Indent(); * Trace.WriteLine("go right ___ begin"); * Console.WriteLine("go right ___ begin"); * InorderPrint( node._right ); * Trace.WriteLine("go right ___ end"); * Console.WriteLine("go right ___ end"); * Trace.Unindent(); * } * } */ }
public RBNodeBase GetEqualOrLess(object searchKey) { if (_root == _null) { return(null); } RBNodeBase x = _root; RBNodeBase prev = _null; RBNodeBase found = null; int iCompare; while (x != _null) { iCompare = CompareKeys(searchKey, x.Key); if (iCompare == 0) { found = x; } if (iCompare <= 0) { x = x._left; } else { prev = x; x = x._right; } } if (found != null) { return(found); } if (prev == _null) { return(null); } return(prev); }
public RBNodeBase Search(object searchKey) { RBNodeBase x = _root; int iCompare; while (x != _null) { iCompare = CompareKeys(searchKey, x.Key); if (iCompare < 0) { x = x._left; } else if (iCompare > 0) { x = x._right; } else { return(x); } } return(null); }
public void RB_InsertNode(RBNodeBase node) { Insert(node); RB_Insert(node); _count++; }
private void RB_Delete_Fixup(RBNodeBase x) { while (x != _root && x._color == RBColor.Black) { RBNodeBase w; if (x == x._parent._left) { 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; } else { w._color = x._parent._color; x._parent._color = RBColor.Black; w._right._color = RBColor.Black; LeftRotate(x._parent); x = _root; } } else { 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; } else { 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 RBNode(object key, RBNodeBase NullNode) { _key = key; _right = _left = _parent = NullNode; }
public void SetEntry(int priority, object obj, RBNodeBase nullNode) { _priority = priority; _object = obj; _right = _left = _parent = nullNode; }