Introduction to Algorithms 13th Chapter Red Black Tree (Python)-1 insert

Source: Internet
Author: User

Red and black trees are the last chapter of the two-fork search tree improvement, to achieve a balance, to ensure that there will be no two-fork tree linked list of cases, the basic dynamic set operation of the time complexity of O (LGN)

Practical use: The Set,map in C++stl is realized by him.

The nature of the red-black tree:

1. Each node is either red or black.

2. The knot point is black.

3. Each leaf node (NIL) is black

4. If a node is red, its two nodes are black

5. For each node, the same number of black nodes (the number referred to as black high bh (x)) is included on the simple path from the node to all its descendants ' leaf nodes.

Such as:

(T.nil Sentinel behind is ignored None)

Red black tree is the improvement of the two-fork search tree, in order to ensure the relative balance of the tree, the main difference is to increase the color of the property, and then color as the starting point of the 5 properties, in order to achieve the 5 properties we want to rotate and colour (insert, delete).

Node Code:

Class Node: #红黑树结点类    def __init__ (self,data):        self.left = none        self.right = none        self.parent = None        Self.data = data        Self.color = ' Red ' #初始化为red不是black看第5条黑高变化不好调节而red要好些
The rotation of the red and black trees: a key to ensuring balance

Preserve the relative balance of the tree by rotating 5 properties of the red and black tree during insert/delete

This is the basic conversion process.

Main regulation x and beta (beta code with B instead) X.parent and y X and y relationships

Code procedures:

   def left_rotate (self,root): "Go around self to root root node" ' x = self #y必须存在 y        = X.right if y = = None:return; b = Y.left #x and b x.right = b if b! = None:b.parent = x #y和x. Parent Y.paren            t = x.parent if x.parent = = None: #x为root结点 root = y elif x = = X.parent.left: X.parent.left = y Else:x.parent.right = y #x和y y.left = x x.parent = y def r Ight_rotate (self,root): "Go around self to root root node" y = self #x必须存在 x = Y.lef        t if x = = None:return; b = X.right #y and b y.left = b if b! = None:b.parent = y #x和y. Parent X.paren            t = y.parent if y.parent = = None: #y为root结点 root = x elif y = = Y.parent.left:     Y.parent.left = x Else:       Y.parent.right = x #x和y x.right = y y.parent = x 

Red-black Tree insert:

I want to first write the premise of our insertion:

1. We want to make sure that the 5-point nature of the red-black tree (will use rotational discoloration to maintain the key to the first balance)

2. We are inserting the red dot by default-(destruction 2nd, 4) Contrast insertion black point (destruction 5) The change in black height requires that each node is more difficult to meet

3. We inserted the position in the leaf node location (can recall the binary search tree code)

Insert the first part of the Code: (Modified on the binary search tree)

def tree_insert (self,data):        #插入data        node = self while        node:            if data < node.data:                next = Node.left            Else:                next = Node.right            if Next:                node = next            else:                break        nn = Self.createnode (data) #nn初始化颜色为red        if data < node.data: #注data为根节点 cannot use this function            node.left = nn            node.left.parent = node< C15/>else:            node.right = nn            node.right.parent = node        #我没有使用哨兵        #变化        nn.re_insert_fixup (self) #旋转变色保持性质        return nn
Draw all the possibilities: #带有z的为插入位置

If there is no Father node (root): #在RBTree中直接改成黑色

Graph graphname{//Figure Zz[color = Red,style = filled];//properties of the midpoint of the graph}
 

Yesterday: Found a language in the DIT to draw a software gvedit (360 software tube Home, the official website seems to be a wall?? Use edge learning side to use) My reference URL: http://blog.csdn.net/zhangskd/article/details/8250470

Elif has a father knot:

If Father node is black:

Or

Graph graphname{7--5--z;//z The point we inserted 5--null[color = white];//I used a NULL to write a disguise for whites, making the diagram look more like a binary tree tentative solution 7--8;7,z[color = red, style = Filled];5,8[color = Gray,style = Filled,fontcolor = white];//Use Gray instead of black black to show too heavy Null[color = White,fontcolor=white]}
Apparently the father's knot was not affected by the five properties of the black pair of trees {Note: About 3rd I'm omitting the leaf node is the leaf node is black}

Elif father Knot is red://Come here to start the pseudo-code on the book

If father on grandfather's left

If uncle is red: #8为红色 book on condition 1 #这里省略了一般部分 uncle on the right below 8

Or

#省去dot代码篇幅太长不好复习, it's just that the code on the above is not going to talk anymore.

See this I'm always thinking it's impossible to turn around, but according to 4 is not possible, so our choice is to change color

Or

Obviously the black height has not changed, if the 7 node is rooted directly to black, if it should not be recursive processing//7 discoloration, so to deal with

if node z->p->p = = Root

Turn it black.

Else

z = z->p->p#z is moved up here because the top 7 is discolored.

Start again

Elif Uncle is black:

If z on Father's right: Case2

-----change to---->

#z是表示我们在代码中要变化的位置 #这图画的不好 Lazy--

#显然情况变成了case3

#为什么要么换?? Didn't want to clear

Elif Z on the left of the father: Case3

----Change to--->

Apparently, now it's in line

Father of the Else:z on the left of grandfather: symmetrical situation

How to draw a lot of ideas or a bit of chaos I will be conditional judgment to take out, from the overall look. #和书上的代码可能不同 he omitted a lot.

If there is no Father node (root):

Elif has a father knot:

If Father node is black:

elif father knot for red:

If father on grandfather's left #如果是红必有父结点

If uncle is red: #case1

#z在父亲的左边和右边都没有变化

if node z->p->p = = Root:

else: recursive re-processing

Elif Uncle Black: Case 2 + 3

If z on Father's right: Case2

Elif Z on the left of the father: Case3

Elif father on grandfather's left

  def re_insert_fixup (self,root): #插入时调节平衡部分 z = self while z.parent! = None and Z.parent.color = = ' R                Ed ': #如果有父亲结点且他为红色 if z.parent = = Z.parent.parent.left:y = Z.parent.parent.right #y是z的叔父 if Y.color = = ' Red ': #case 1 z.parent.color = ' black ' Y.color = ' b                    Lack ' Z.parent.parent.color = ' red ' z = z.parent.parent else: if z = = z.parent.right: #case 2--->case3 z = z.parent Z.righ                    T_rotate (root) Z.parent.color = ' black ' z.parent.parent.color = ' red '                Z.parent.parent.right_rotate (root) else:y = Z.parent.parent.left #y是z的叔父                    if Y.color = = ' Red ': #case 1 z.parent.color = ' black ' Y.color = ' black ' Z.parent.parent = ' Red ' z = z.parent.parent else:if z = = Z.parent.left: #case 2--->case3 z = z.parent z.left_rotate (root) z.parent.        color = ' black ' z.parent.parent.color = ' red ' z.parent.parent.left_rotate (root) Root.color = ' Black '
Reference:

http://blog.csdn.net/fxjtoday/article/details/6448083

http://www.wutianqi.com/?p=2449

http://blog.csdn.net/zhangskd/article/details/8250470

Introduction to Algorithms 13th Chapter Red Black Tree (Python)-1 insert

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.