Introduction to Algorithms Learning Notes--13th chapter red and black Trees

Source: Internet
Author: User

Red and black Trees

A red-black tree is a binary lookup tree, but adds a storage node color to each node, which can be red or black. By limiting the color of the nodes on any path from root to leaf, the red-black tree ensures that no path is twice times longer than the other paths, and thus is nearly balanced.

Each node contains 5 domains, color,key,left,right,p

Meets the following red-black properties:

1. Each knot is red or black

2. The root node is black

3. Each leaf node (nil) is black

4, if a node is red, then its two sub-nodes are black

5. For each node, the number of black nodes is the same from the node to the path of each descendant node.

Left rotation

1left-ROTATE (t,x)2 Y←right[x]3 Right[x]←left[y]4 p[left[y]]←x5 P[y]←p[x]6 ifp[x]==Nil[t]7 Then root[t]←y8     Else ifx=Left[p[x]]9 Then left[p[x]]←yTen         ElseRight[p[x]]←y One left[y]←x AP[x]←y

Insert

1rb-INSERT (t,z)2 Y←nil[t]3 X←root[t]4  whilex!=Nil[t]5      Doy←x6         ifkey[z]<Key[x]7 Then X←left[x]8             ElseX←right[x]9 P[z]←yTen ify=Nil[t] One Then root[t]←z A     Else ifkey[z]<Key[y] - Then left[y]←z -         Elseright[y]←z the Left[z]←nil[t] - Right[z]←nil[t] - color[z]←red -rb-insert-fixup (t,z) +  - //keep the red and black nature +rb-insert-fixup (t,z) A  whilecolor[p[x]]=RED at      Do ifp[z]=Left[p[p[z] ] - Then Y←right[p[p[z] ] -             ifcolor[y]=RED - Then color[p[z]]←black - Color[y]←black - color[p[p[z]]]←red in Z←p[p[z]] -                 Else ifz=Right[p[x]] to Then Z←p[x] +left-ROTATE (t,z) - Color[p[z]]←black the color[p[p[z]]]←red *right-ROTATE (t,p[p[z]) $             Else(Same asThen clause with" Right"and" Left"exchanged)Panax NotoginsengColor[root[t]]←black

Delete

1rb-DELETE (t,z)2 ifLeft[z]=nil[t] or right[z]=Nil[t]3 Then y←z4     Elsey←tree-successor (z)5 ifleft[y]!=Nil[t]6 Then X←left[y]7     ElseX←right[y]8 P[x]←p[y]9 ifp[y]=Nil[t]Ten Then root[t]←x One     Else ify=Left[p[y]] A Then left[p[y]]←x -         Elseright[p[y]]←x - ify!=Z the Then Key[x]←key[y] -Copy y's satellite data into Z - ifcolor[y]=BLACK -Then rb-delete-fixup (t,x) + returny -  + //keep the red and black nature Arb-delete-fixup (t,x) at  whileX!=root[t] and color[x]=BLACK -      Do ifx=Left[p[x]] - Thenω←right[p[x]] -             ifcolor[ω]=RED -Then color[ω]=BLACK - color[p[x]]←red inleft-ROTATE (t,p[x]) - Ω←right[p[x]] to             ifColor[left[ω]]=black and color[right[ω]]=BLACK + Then color[ω]←red - X←p[x] the                 Else ifcolor[right[ω]]=BLACK * Then color[left[ω]]←black $ color[ω]←redPanax Notoginsengright-ROTATE (t,ω) - Ω←right[p[x]] the Color[ω]←color[p[x]] + Color[p[x]]←black A Color[right[ω]]←black theleft-ROTATE (t,p[x]) + X←root[t] -         Else(Same asThen clause with" Right"and" Left"exchanged) $Color[x]←black

Introduction to Algorithms Learning Notes--13th chapter red and black Trees

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.