public void Inorder(RBNode x, List <int> l, List <RBNode> n) { if (x.leftchild != null) { Inorder((RBNode)x.leftchild, l, n); } l.Add(x.key); n.Add(x); if (x.rightchild != null) { Inorder((RBNode)x.rightchild, l, n); } }
void searchBtn_Click(object sender, RoutedEventArgs e) { if (input.Text == "") { MessageBoxResult message1 = MessageBox.Show("Enter a Value!"); } else { int searchItem = Convert.ToInt32(input.Text); foundNode = (RBNode)tree.Search(tree.root, searchItem); if (foundNode == null) { MessageBoxResult message1 = MessageBox.Show("Value Doesn't Exist!"); } else { search = true; } drawStructure(); } }
public int getBlackHeight(int searchValue) { // Find the Node in question RBNode subTreeNode = (RBNode)search(searchValue); // Note that black height starts at 0 - It should start at -1, but we compensate because this algorithm always skips the leaf int blackHeight = 0; // Traverse down the tree and collect the black height while (subTreeNode.Field != null) { subTreeNode = (RBNode)subTreeNode.LChild; if (subTreeNode.NodeColor == COLOR.BLACK) { blackHeight++; } } return(blackHeight); }
public int BlackHeight(RBNode n) { int sum = 0; if (n == this.root) { return(0); } else { while (n != this.root) { if (n.color == "Black") { sum++; } n = (RBNode)n.parent; } return(sum); } }
void bhBtn_Click(object sender, RoutedEventArgs e) { if (input.Text == "") { MessageBoxResult message1 = MessageBox.Show("Enter a Height"); } else { int value = Convert.ToInt32(input.Text); RBNode bhNode = (RBNode)tree.Search((RBNode)tree.root, value); if (bhNode == null) { MessageBoxResult message1 = MessageBox.Show("Node Doesn't Exist!"); } else { int bh = tree.BlackHeight(bhNode); MessageBoxResult message1 = MessageBox.Show("Black Height = " + bh.ToString()); } drawStructure(); } }
public void Insert(int k) { RBNode n = new RBNode(k); RBNode a = root; RBNode b = null; while (a != null) { b = a; if (n.key < a.key) { a = (RBNode)a.leftchild; } else { a = (RBNode)a.rightchild; } } n.parent = b; if (b == null) { root = n; } else if (n.key < b.key) { b.leftchild = n; } else if (n.key > b.key) { b.rightchild = n; } n.leftchild = null; n.rightchild = null; n.color = "Red"; fixUp(n); }
// Functions to find the ancestors of nodes public RBNode parent(RBNode N) { return((RBNode)N.Parent); }
// Fix nodes public void FixUpRB(RBTree tree, RBNode N) { while (parent(N).NodeColor == COLOR.RED) { // If parent(N) is an LChild of grandparent(N) if (parent(N) == grandparent(N).LChild) { // Y is the uncle of Node N RBNode Y = (RBNode)grandparent(N).RChild; // Case 1: RED Uncle, if (Y.NodeColor == COLOR.RED) { // Make the parent and uncle black parent(N).NodeColor = COLOR.BLACK; Y.NodeColor = COLOR.BLACK; grandparent(N).NodeColor = COLOR.RED; N = grandparent(N); } // Case 2: BLACK Uncle else { if (N == parent(N).RChild) { N = parent(N); LeftRotate(N); } parent(N).NodeColor = COLOR.BLACK; grandparent(N).NodeColor = COLOR.RED; RightRotate(grandparent(N)); } } else { // Y is the uncle of Node N RBNode Y = (RBNode)grandparent(N).LChild; // Case 1: RED Uncle, if (Y.NodeColor == COLOR.RED) { // Make the parent and uncle black parent(N).NodeColor = COLOR.BLACK; Y.NodeColor = COLOR.BLACK; grandparent(N).NodeColor = COLOR.RED; N = grandparent(N); } // Case 2: BLACK Uncle else { if (N == parent(N).LChild) { N = parent(N); RightRotate(N); } parent(N).NodeColor = COLOR.BLACK; grandparent(N).NodeColor = COLOR.RED; LeftRotate(grandparent(N)); } } } // Color it black ((RBNode)tree.root).NodeColor = COLOR.BLACK; }
public RBNode grandparent(RBNode N) { return((RBNode)N.Parent.Parent); }
public void fixUp(RBNode n) { RBNode parent = (RBNode)n.parent; while (parent != null && parent.color == "Red") { parent = (RBNode)n.parent; RBNode grandparent = (RBNode)n.parent.parent; RBNode leftuncle = (RBNode)grandparent.leftchild; RBNode rightuncle = (RBNode)grandparent.rightchild; if (parent == leftuncle) { RBNode b = rightuncle; if (b == null) { return; } else if (b.color == "Red") { parent.color = "Black"; b.color = "Black"; grandparent.color = "Red"; n = grandparent; } else if (b.color == "Black") { if (n == parent.rightchild) { n = parent; rotateLeft(n); } parent.color = "Black"; grandparent.color = "Red"; rotateRight(grandparent); } } else { RBNode b = leftuncle; if (b == null) { return; } else if (b.color == "Red") { parent.color = "Black"; b.color = "Black"; grandparent.color = "Red"; n = grandparent; } else if (b.color == "Black") { if (n == n.parent.leftchild) { n = parent; rotateRight(n); } parent.color = "Black"; grandparent.color = "Red"; rotateLeft(grandparent); } } parent = (RBNode)n.parent; } this.root.color = "Black"; }