Chapter 13 "The Red-Black Tree": A brief introduction to the red-black tree

Red black tree

The red-black tree is a binary search tree. It adds a storage bit to each node to indicate the color of the node, which can be RED or BLACK. Each node in the tree contains 5 attributes: color, key, left, right, and p. If a node has no child or parent, the value of the corresponding pointer attribute of the node is NIL, and a red-black tree satisfies the following red and black Nature of binary search tree:

  • Each node is either red or black
  • The root node is black
  • Each leaf node (NIL) is black
  • If a node is red, then its two child nodes are black
  • For each node, the simple path from that node to all its descendant leaf nodes contains the same number of black nodes

The following figure (a) shows a red-black tree, and in figure (b), instead of using NIL one by one, it is replaced by a black sentinel T.nil, and its black height is also omitted. The parent node of the root node is also this sentinel.Just
Insert picture description here

From a node xxThe number of black nodes on any simple path starting from x (excluding this node) to a leaf node is called the black height of the node, denoted asbh (x) bh(x)b h ( x ) . From the nature of the red-black tree, one tree hasnnThe height of the red-black tree with n internal nodes is at most2 lg (n + 1) 2lg(n+1)2lg(n+1)

Spin

Search tree operations insert and delete contain nnOn a red-black tree with n keywords, the running time isO (lgn) O(lgn)O ( l g n ) . Because these two operations modify the tree, it may violate the red-black nature. In order to maintain these properties, the color and pointer structure of some nodes in the tree must be changed. The modification of the pointer structure is done by rotation (ratation), which is a local operation of the search tree that can maintain the nature of the binary search tree. As shown in the figure below, two kinds of rotation are given: left-hand and right-hand. Left hand toxxxyyThe chain of y is carried out as a "pivot", which makesyyy becomes the new root node of the subtree,xxx becomesyyy 's left child,yyy 's left child becomesxxThe right child of x .
Insert picture description here
Python implements rotation as follows:

	#左旋转
	def left_rotate(self, x):
		# set y
		y = x.rchild
		# turn y left subtree into x right subtree
		x.rchild = y.lchild
		if y.lchild != self.nil:
			y.lchild.parent = x
		# link x parent to y
		y.parent = x.parent
		if x.parent == self.nil:
			# x is root
			self.root = y
		elif x == x.parent.lchild:
			x.parent.lchild = y
		else:
			x.parent.rchild = y
		# put x on y left
		y.lchild = x
		x.parent = y
	#右旋转
	def right_rotate(self, y):
		# set x
		x = y.lchild
		# turn x right subtree into y left subtree
		y.lchild = x.rchild
		if x.rchild != self.nil:
			x.rchild.parent = y
		# link y parent to x
		x.parent = y.parent
		if y.parent == self.nil:
			# x is root
			self.root = x
		elif y == y.parent.lchild:
			y.parent.lchild = x
		else:
			y.parent.rchild = x
		# put y on x right
		x.rchild = y
		y.parent = x

Guess you like

Origin blog.csdn.net/BGoodHabit/article/details/105927933