public static int ValidateTree( IRedBlackLeaf<string,string> leaf ) { int lh, rh; if( leaf == null ) return 1; else { var left = leaf.Left; var right = leaf.Right; if( leaf.IsRed() && ( left.IsRed() || right.IsRed() ) ) throw new Exception("Red violation!"); lh = ValidateTree( left ); rh = ValidateTree( right ); if( ( left != null && ( left.GreaterThan( leaf ) || leaf.Key.Equals( left.Key ) ) ) || ( right != null && ( right.LessThan( leaf ) || leaf.Key.Equals( right.Key ) ) ) ) { throw new Exception("Invalid binary tree structure!"); } if( lh != 0 && rh != 0 && lh != rh ) throw new Exception("Black violation!"); if( lh != 0 && rh != 0 ) return leaf.IsRed() ? lh : lh + 1; else { return 0; } } }
public static int ValidateTree(IRedBlackLeaf <string, string> leaf) { int lh, rh; if (leaf == null) { return(1); } else { var left = leaf.Left; var right = leaf.Right; if (leaf.IsRed() && (left.IsRed() || right.IsRed())) { throw new Exception("Red violation!"); } lh = ValidateTree(left); rh = ValidateTree(right); if ((left != null && (left.GreaterThan(leaf) || leaf.Key.Equals(left.Key))) || (right != null && (right.LessThan(leaf) || leaf.Key.Equals(right.Key)))) { throw new Exception("Invalid binary tree structure!"); } if (lh != 0 && rh != 0 && lh != rh) { throw new Exception("Black violation!"); } if (lh != 0 && rh != 0) { return(leaf.IsRed() ? lh : lh + 1); } else { return(0); } } }
public virtual IRedBlackLeaf <TKey, TValue> Remove(IRedBlackLeaf <TKey, TValue> root, TKey key, ref bool done) { if (root.IsEmpty()) { done = true; } else { bool direction; if (root.Key.Equals(key)) { if (root.Left.IsEmpty() || root.Right.IsEmpty()) { var save = root[root.Left.IsEmpty()]; if (root.IsRed()) { done = true; } else if (save.IsRed()) { save.Color = LeafColor.BLACK; done = true; } return(save); } else { var heir = root.Left; while (!heir.Right.IsEmpty()) { heir = heir.Right; } root.Value = heir.Value; root.Key = heir.Key; key = heir.Key; } } direction = root.LessThan(CreateLeaf(key, default(TValue))); root[direction] = Remove(root[direction], key, ref done); if (!done) { root = BalancePostDelete(root, direction, ref done); } } return(root); }
public virtual IRedBlackLeaf <TKey, TValue> BalancePostDelete(IRedBlackLeaf <TKey, TValue> root, bool direction, ref bool done) { IRedBlackLeaf <TKey, TValue> worker = root; IRedBlackLeaf <TKey, TValue> leaf = root[!direction]; if (leaf.IsRed()) { root = Rotate(root, direction); leaf = worker[!direction]; } if (!leaf.IsEmpty()) { if (leaf.Left.IsBlack() && leaf.Right.IsBlack()) { if (worker.IsRed()) { done = true; } worker.Color = LeafColor.BLACK; leaf.Color = LeafColor.RED; } else { var save = worker.IsRed(); var newRoot = root.Key.Equals(worker.Key); if (leaf[!direction].IsRed()) { worker = Rotate(worker, direction); } else { worker = DoubleRotate(worker, direction); } worker.Color = save ? LeafColor.RED : LeafColor.BLACK; if (!worker.Left.IsEmpty()) { worker.Left.Color = LeafColor.BLACK; } if (!worker.Right.IsEmpty()) { worker.Right.Color = LeafColor.BLACK; } if (newRoot) { root = worker; } else { root[direction] = worker; } done = true; } } return(root); }