Introduction to algorithms-13.3-6-red-black tree stack-based RB-INSERT

Source: Internet
Author: User
Question:

Explain how to effectively implement RB-INSERT if the expression of the Red-black tree does not provide the parent pointer

 

Thoughts:

Make the element to be inserted Z. Record the path from the root node to Z during the insert process and store it in the stack. Then, the parent node of Z is the top element of the stack, and the grandfather node of Z is the top element of the stack.

In the process of iteration up at the same time out of the stack, control the time of the stack, the correct implementation of RB-INSERT

 

Code:
# Include <iostream> using namespace STD; # define black 0 # define Red 1 // structure of the red/black tree node struct node {node * left; node * right; int key; bool color; node (node * init, int K): Left (init), right (init), key (K), color (black ){}}; // chain stack node Structure struct s_node {node * data; s_node * Next; s_node (node * k): Data (K), next (null ){}}; // chain stack structure struct stack {s_node * Top; stack (): Top (null) {}} s; // void stack_insert (stack & S, node * z) {s_node * s = new S _ Node (z); s-> next = S. top; S. top = s;} // void stack_pop (stack & S) {s_node * temp = S. top; S. top = S. top-> next; Delete temp;} // struct red_black_tree {node * root; // root node * nil; // Sentel red_black_tree () {nil = new node (null,-1); root = nil ;};}; // left-hand, y = x-> right, left-hand rotation is based on the chain between x and y. // The involved nodes include: X, Y, Y-> left, so that node = {P, L, r }, the specific changes are as follows: // X = {X-> P, X-> left, y} is changed to {Y, X-> left, y-> left} // y = {X, Y-> left, Y-> right} is changed to {X-> P, X, Y-> righ T} // y-> left = {Y, Y-> left, Y-> left-> right} is changed to {X, Y-> left, y-> left-> right} void left_rotate (red_black_tree * t, node * X) {// F points to P [x] node * f = S. top-> data; // make y = x-> rightnode * Y = x-> right; // modify the pointers of the three nodes in the preceding way, note the order of changing the pointer X-> right = Y-> left; If (F = T-> nil) // special case: X is the root node T-> root = y; else if (x = f-> left) F-> left = y; else f-> right = y; y-> left = x;} // right-hand, so that Y = x-> left, the left-hand rotation is based on the chain between x and y. // The rotation process is similar to void right_rot. Ate (red_black_tree * t, node * X) {// F points to P [x] node * f = S. top-> data; node * Y = x-> left; X-> left = Y-> right; if (F = T-> nil) t-> root = y; else if (x = f-> right) F-> right = y; else f-> left = y; y-> right = x;} // adjust void rb_insert_fixup (red_black_tree * t, node * z) {node * Y; // The only condition to be adjusted, is the violation of the Nature 2, if the violation of the Nature 2, adjust the end while (S. top-> data-> color = red) {// F points to P [Z], f2 points to P [p [Z] // when P [Z] is a left child, node * f = S. top-> data; node * F2 = S. t Op-> next-> data; If (F = F2-> left) {// make y the uncle node of z y = F2-> right; // In the first case, Uncle y of Z is red if (Y-> color = red) {// Black P [Z] and y to solve the problem that both Z and P [Z] are red. F-> color = black; y-> color = black; // place P [p [Z] in red to keep the property 5f2-> color = Red; // use P [p [Z] as the newly added node Z to repeat the while loop stack_pop (s); stack_pop (s) ;}// the second case: z's uncle is black, and Z is the right child else {If (Z = f-> right) {// to P [Z] Left-hand, in the third case, // to ensure correct left-hand operations, the stack element should be P [f] stack_pop (s); left_rotate (T, f);} // in the third case: z's uncle is black and Z is left child // exchange P [Z] and P [P [Z] color, and right-hand F = F2-> left; F-> color = black; F2-> color = Red; // to ensure correct left-hand, the stack element is P [F2] If (S. top-> DATA = f) stack_pop (s); right_rotate (T, F2) ;}// P [Z] is the right child, there are three cases, similar to the above else if (F = F2-> right) {Y = F2-> left; If (Y-> color = red) {f-> color = black; y-> color = black; F2-> color = Red; stack_pop (s );} else {If (Z = f-> left) {stack_pop (s); right_rotate (T, f);} f = F2-> right; F-> color = black; f2-> Co Lor = red; If (S. top-> DATA = f) stack_pop (s); left_rotate (T, F2) ;}}t-> root-> color = black ;} // insert void rb_insert (red_black_tree * t, node * z) into the red/black tree {// clear the stack, T-> NIL is the top element of the stack while (S. top-> data! = T-> nil) stack_pop (s); node * Y = T-> nil, * x = T-> root; while (X! = T-> nil) {// records the points in the path from root to Z, excluding zstack_insert (S, x); y = X; if (Z-> key <X-> key) x = x-> left; elsex = x-> right;} If (y = T-> nil) t-> root = z; else if (Z-> key <Y-> key) Y-> left = z; Elsey-> right = z; z-> left = T-> nil; Z-> right = T-> nil; Z-> color = Red; rb_insert_fixup (T, Z );} void print (node * X) {If (X-> key =-1) return; print (X-> left ); cout <X-> key <''<X-> color <Endl; print (X-> right);} void print (red_black_tree * t) {print (t-> root); cout <Endl;} int main () {red_black_tree * t = new red_black_tree; stack_insert (S, T-> nil ); int s [6] = {41, 38, 31, 12, 19, 8}, I; for (I = 0; I <6; I ++) {node * z = new node (t-> nil, s [I]); rb_insert (T, Z); print (t) ;}return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.