Red-black Tree (c) Delete

Source: Internet
Author: User

Delete operations are more complex than inserts. First, let's look at some of the characteristics of red and black trees. These are my random list, for everyone's reference.

1, the red node Father black child must be black. (Nil is a black node)

2, single branch node, can only be black. (Red , black, black , not in accordance with rule 4, to the same number of tree tail black nodes)

3, the real delete node must be a single node or leaf node. (nodes with no children)

Next we talk about how to find the real delete node.

There's a case of left and right subtrees.

If 8 is the deletion node, then 10 is the real delete node.

The lookup method is to find the smallest node in the right subtree of the 8 node, according to the nature of the binary tree, we can know that the left child of the left child of 8 node right child ... To the left for nil node. is 10. (While(y->left! = nil)  y = y->left;)

The case of a single node or no leaf node

Deleting a node is one with a real delete node.

After we find the real delete node, we turn the value of the deleted node into the value of the true delete node. The real deletion node is removed from the red-black tree.

If the true delete node is black, it destroys the nature of the red-black tree and adjusts it. (such as delete 8, the real delete node is 10,10 is the black node, so need to adjust the red black tree)

voidRb_remove (Const intKey, Tree *tree) {Node* x, * y, * z;//x is the delete point, Y is the true delete point, and Z is the child node of Y.x=Search_node (key, tree); if(x = Nil | | X->value! =key)return; if(X->left! = Nil && x->right! = nil)//Find a real delete node{y= x->Right ;  while(Y->left! =nil) y= y->Left ; }    Elsey=x; if(Y->left! =nil) Z= y->Left ; ElseZ= y->Right ; if(y = = tree->root) {Tree->root =Z; Z->parent =Nil; }    Else    {        if(y = = y->parent->Left ) y->parent->left =Z; Elsey->parent->right =Z; if(Z! =nil) Z->parent = y->parent;    } assign (x, y); if(Y->color = =black) remove_fixup (z, y-parent, tree);  Free(y);}

When removing a true delete node, note that if it has a child, if the right child is pointing to the child with a pointer to a really deleted node, do not forget to change the child's father. Otherwise, the pointer to the true delete node is pointed to nil.

Next Delete adjustment

There are many kinds of implementations for deleting adjustments, but the answer is the same. If you write your own words, you need to consider all kinds of situations clearly.

Static voidRemove_fixup (node * x, Node * y, Tree *tree) {Node* Z;//Z is the brother of X     while(x! = Tree->root && X->color = =Black) {        if(x = = Y->Left ) {Z= y->Right ; //Case 1: Brother is red//Processing Method://1. Set the brother to black//2. Set the father to red//3. With the father as the rotation point, l//4. Resetting the sibling node of x//into Case 2, 3, 4            if(Z->color = =Red) {Z->color =Black; Y->color =Red;                Left_rotate (y, tree); Z= y->Right ; }            //Case 2: Brother is black, and two children are black//Processing Method://1. Set the brother to Red//2. Set X as Father            if(Z->left->color = = Black && Z->right->color = =Black) {Z->color =Red; X=y; Y= x->parent; }            //Case 3: Brother is black, left child is red, right child is black//processing methods; //1. Set the brother's left child to black//2. Set the brother to Red//3. Rotate the point with the brother, right -//4. Reset Sibling Nodes            Else             {                if(Z->right->color = =Black) {Z->left->color =Black; Z->color =Red;                    Right_rotate (z, tree); Z= y->Right ; }                //Case 4: Brother is black, right child is red//Processing Method://1. Set the brother's color to the father's color//2. Set the father's color to black//3. Set the brother's right child to black//4. With the father as the rotation point, lZ->color = y->color; Y->color =Black; Z->right->color =Black;                Left_rotate (y, tree);  Break; }        }        Else{z= y->Left ; if(Z->color = =Red) {y->color =Red; Z->color =Black;                Right_rotate (y, tree); Z= y->Left ; }            if(Z->left->color = = Black && Z->right->color = =Black) {Z->color =Red; X=y; Y= x->parent; }            Else            {                if(Z->left->color = =Black) {Z->right->color =Black; Z->color =Red;                    Left_rotate (z, tree); Z= y->Left ; } z->color = y->color; Y->color =Black; Z->left->color =Black;                Right_rotate (y, tree);  Break; }        }    }    if(X! =nil) x->color =Black;}

If x is the right child of Y, the operation is the same as the left child.

Delete operation completed, it is recommended that everyone in the debugging red black tree draw out.

Here is my debug code attached.

#include <stdio.h>#include<stdlib.h>#include<time.h>#include"rbtree.h"voidPrint (Node *x) {printf ("%d\t", x->value); if(X->color = =red) printf ("red\t"); Elseprintf ("black\t"); if(x->parent) printf ("Parent value =%d\n", x->parent->value); Elseprintf ("\ n");}intMainvoid) {tree tree; inti;    Srand (Time (NULL)); Rb_init (&tree);  for(i =0; I < -; i++) {Rb_insert (rand ()% +, &tree); } rb_treaverse (&tree, print);  for(i =0; I < -; i++) {Rb_remove (rand ()% +, &tree); }//Rb_insert (ten, &tree);//Rb_insert (7, &tree);//Rb_insert (8, &tree);//Rb_insert (&tree);//Rb_insert (5, &tree);//Rb_insert (6, &tree);//Rb_insert (one, &tree);//Rb_insert (&tree);//Rb_insert (&tree);//Rb_insert (2, &tree);////rb_treaverse (&tree, print);//Rb_remove (5, &tree);//Rb_remove (7, &tree);//Rb_remove (6, &tree);//Rb_remove (8, &tree);//rb_treaverse (&tree, print);//Rb_remove (2, &tree);//Rb_remove (ten, &tree);//Rb_remove (one, &tree);//Rb_remove (&tree);//Rb_remove (&tree);//Rb_remove (&tree);Rb_treaverse (&tree, print); return 0;}

The two for loop in front of the main test red black tree Whether there is a bug, if you find a bug, use the following insert to find the bug.

The code below is basically used in four different situations.

Red-black Tree (c) Delete

Related Article

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.