/// <summary> /// Generates a leaf /// </summary> public RBNode(RBNode parent) { this.color = Color.Black; this.leaf = Leaf.LEAF; this.RBNodeLeft = null; this.RBNodeRight = null; this.parent = parent; }
public RBNode AddNode(RBNode root, int key) { RBNode newNode = new RBNode(key, Color.Black, null); InsertNode(root, newNode); RepairTree(newNode); return(newNode); }
public RedBlackTree(int[] data) { this.root = AddNode(null, data[0]); this.output = new List <int>(); for (int i = 1; i < data.Length; i++) { AddNode(this.root, data[i]); } }
public RBNode(int key, Color color, RBNode parent) { this.RBNodeLeft = new RBNode(parent); this.RBNodeRight = new RBNode(parent); this.parent = parent; this.key = key; this.color = color; this.leaf = Leaf.not; }
public void InOrder(RBNode node) { if (node == null) { return; } if (node.leaf != Leaf.LEAF) { InOrder(node.RBNodeLeft); this.output.Add(node.Key); InOrder(node.RBNodeRight); } else { return; } }
public RBNode Search(int key, RBNode startRBNode) { while (startRBNode != null) { if (startRBNode.Key == key) { return(startRBNode); } else if (startRBNode.Key < key) { startRBNode = startRBNode.RBNodeRight; } else { startRBNode = startRBNode.RBNodeLeft; } } return(null); }
private void RotateLeft(RBNode node) { RBNode pivot = node; RBNode root = node.Parent; RBNode parent = root.Parent; pivot.Parent = parent; root.RBNodeRight = pivot.RBNodeLeft; if (parent != this.root.Parent) { parent.RBNodeLeft = pivot; } else { this.root = pivot; } pivot.RBNodeLeft = root; root.Parent = pivot; }
private void Case2(RBNode node) { if (node == node.Grandparent().RBNodeLeft.RBNodeRight) { RotateLeft(node.Parent); if (node.RBNodeLeft.leaf != Leaf.LEAF) { node = node.RBNodeLeft; } } else if (node == node.Grandparent().RBNodeRight.RBNodeLeft) { RotateRight(node.Parent); if (node.RBNodeRight.leaf != Leaf.LEAF) { node = node.RBNodeRight; } } if (node == node.Parent.RBNodeLeft) { RotateRight(node.Parent); } else { RotateLeft(node.Parent); } node.Parent.Color = Color.Black; if (node.Grandparent() != null) { node.Grandparent().Color = Color.Red; } }