private static int GetNodeID(RedBlackTreeNode<DirectoryEntry> node) { if (node == RedBlackTreeNode<DirectoryEntry>.Nil) { return -1; } else { return node.Data.ID; } }
private void AssertRoot(RedBlackTreeNode <int, int> root) { Assert.AreEqual(root.Parent, null); }
private void BalanceRedBlack(RedBlackTreeNode node) { var parentNode = node.Parent as RedBlackTreeNode; if (parentNode == null) { //We are inserting root node node.Color = TreeNodeColor.Black; } else if (parentNode.Color != TreeNodeColor.Black) { //Note: parent's color is Red means there must be a grand parent there. //Otherwise the parent will be the root node, //it will break the rule: 'Root node is always Black'. //Parent is also Red, we need to look at Uncle's color var uncleNode = (RedBlackTreeNode)GetUncleTreeNode(node); var grandParent = parentNode.Parent as RedBlackTreeNode; if (uncleNode != null && uncleNode.Color != TreeNodeColor.Black) { //New Node, parent and Uncle are both Red, push grand parent's color down grandParent.Color = TreeNodeColor.Red; uncleNode.Color = TreeNodeColor.Black; parentNode.Color = TreeNodeColor.Black; //When grand parent change its color, it may break the rule //Still need to balance the tree from grand parent to top BalanceRedBlack(grandParent); } else { //New node and parent are both Red, make rotation based on below rules: //If New node is a LEFT child and Parent node is a LEFT child //If New node is a LEFT child and Parent node is a RIGHT child //If New node is a RIGHT child and Parent node is a LEFT child //If New node is a RIGHT child and Parent node is a RIGHT child //Swap colors between parent and new node after rotation if (node.Value < parentNode.Value) { if (parentNode.Value < grandParent.Value) { //LEFT + LEFT -> Right rotation RotationRight(grandParent); } else { //LEFT + RIGHT -> Right rotation + Left rotation RotationRight(parentNode); RotationLeft(grandParent); } } else { if (parentNode.Value > grandParent.Value) { //RIGHT + RIGHT -> Left rotation RotationLeft(grandParent); } else { //RIGHT + LEFT -> Left rotation + Right rotation RotationLeft(parentNode); RotationRight(grandParent); } } grandParent.Color = TreeNodeColor.Red; (grandParent.Parent as RedBlackTreeNode).Color = TreeNodeColor.Black; } } }
public void Reset() { this.node = this.startNode; }
public RedBlackTreeIterator(RedBlackTreeNode node) { this.startNode = this.node = node; }
/// <summary> /// Determines whether the specified n is leaf. /// </summary> /// <param name="n">The n.</param> /// <returns> /// <c>true</c> if the specified n is leaf; otherwise, <c>false</c>. /// </returns> protected bool IsLeaf(RedBlackTreeNode <K, T> n) { return((n.left == null) && (n.right == null)); }
private void AssertStructure(RedBlackTreeNode<int, int> elem) { if (elem.Left != null) { Assert.IsTrue(elem.Left.Value < elem.Value); Assert.AreEqual(elem.Left.Parent, elem); AssertStructure(elem.Left); } if (elem.Right != null) { Assert.IsTrue(elem.Value < elem.Right.Value); Assert.AreEqual(elem.Right.Parent, elem); AssertStructure(elem.Right); } }
private void AssertRoot(RedBlackTreeNode<int, int> root) { Assert.AreEqual(root.Parent, null); }
public void UpdateAfterRotateRight(RedBlackTreeNode <RBNode> node) { UpdateAfterChildrenChange(node); UpdateAfterChildrenChange(node.parent); }
private void Insert_2_SKIP(RedBlackTreeNode node) { return; // Tree is Valiid }
public RedBlackTreeNode(TKey value, int height, RedBlackTreeNode <TKey> parent, RedBlackTreeNode <TKey> left, RedBlackTreeNode <TKey> right) { base.Value = value; Color = RedBlackTreeColors.Red; Parent = parent; LeftChild = left; RightChild = right; }
/// <summary> /// Determines whether the specified node is red. /// </summary> /// <param name="node">The node.</param> /// <returns> /// <c>true</c> if the specified node is red; otherwise, <c>false</c>. /// </returns> internal static bool IsRed(RedBlackTreeNode <TKey, TValue> node) { return((node != null) && (node.Color == NodeColor.Red)); }
/// <summary> /// Determines whether the specified node is black. /// </summary> /// <param name="node">The node.</param> /// <returns> /// <c>true</c> if the specified node is black; otherwise, <c>false</c>. /// </returns> internal static bool IsBlack(RedBlackTreeNode <TKey, TValue> node) { return((node == null) || (node.Color == NodeColor.Black)); }
public RedBlackTreeNode(T newData, RedBlackTreeNode newParent) { this.color = RED; data = newData; parent = newParent; }
private void AssertParent(RedBlackTreeNode <int, int> child, RedBlackTreeNode <int, int> parent) { Assert.AreEqual(child.Parent, parent); }
}//end-test-case /// <summary> /// Helper function to calculate the Maximum Height /// </summary> private static int GetMaxHeight(RedBlackTreeNode<int> tree) { if (tree == null) return 0; else return 1 + Math.Max(GetMaxHeight(tree.LeftChild), GetMaxHeight(tree.RightChild)); }
static bool GetColorSafe(RedBlackTreeNode node) { return(node != null ? node.color : black); }
private void AssertParent(RedBlackTreeNode<int, int> child, RedBlackTreeNode<int, int> parent) { Assert.AreEqual(child.Parent, parent); }
void DeleteOneChild(RedBlackTreeNode node) { // case 1 if (node == null || node.parent == null) { return; } RedBlackTreeNode parent = node.parent; RedBlackTreeNode sibling = node.Sibling; if (sibling == null) { return; } // case 2 if (sibling.color == red) { parent.color = red; sibling.color = black; if (node == parent.left) { RotateLeft(parent); } else { RotateRight(parent); } sibling = node.Sibling; if (sibling == null) { return; } } // case 3 if (parent.color == black && sibling.color == black && GetColorSafe(sibling.left) == black && GetColorSafe(sibling.right) == black) { sibling.color = red; DeleteOneChild(parent); return; } // case 4 if (parent.color == red && sibling.color == black && GetColorSafe(sibling.left) == black && GetColorSafe(sibling.right) == black) { sibling.color = red; parent.color = black; return; } // case 5 if (node == parent.left && sibling.color == black && GetColorSafe(sibling.left) == red && GetColorSafe(sibling.right) == black) { sibling.color = red; if (sibling.left != null) { sibling.left.color = black; } RotateRight(sibling); } else if (node == parent.right && sibling.color == black && GetColorSafe(sibling.right) == red && GetColorSafe(sibling.left) == black) { sibling.color = red; if (sibling.right != null) { sibling.right.color = black; } RotateLeft(sibling); } // case 6 sibling = node.Sibling; if (sibling == null) { return; } sibling.color = parent.color; parent.color = black; if (node == parent.left) { if (sibling.right != null) { sibling.right.color = black; } RotateLeft(parent); } else { if (sibling.left != null) { sibling.left.color = black; } RotateRight(parent); } }
public RedBlackTreeNodeEventArgs(RedBlackTreeNode node) { this.Node = node; }
/// <summary> /// Clears this instance. /// </summary> public void Clear() { root = null; size = 0; }