Binary search tree
-
If the left subtree of any node is not empty, the value of all nodes in the left subtree is less than the value of its root node.
-
If the right subtree of any node is not empty, the value of all nodes in the right subtree is greater than the value of its root node.
-
The left and right subtrees of any node are binary search trees respectively.
-
No duplicate nodes have the same key value.
Red and black tree
2) The root is black.
3) Every leaf (leaf refers to NIL pointer or NULL at the end of the tree) is black.
4) If a node is red, both of its sons are black.
5) For any node, each path to the NIL pointer to the end of the leaf node tree contains the same number of black nodes.
Tree rotation knowledge
1. Left-handed
Left-rotate (T, x) 1 y ← right[x] ▹ Set y. 2 Right [x] ← LEFT [y] ▹ Turn y's left subtree into x'S Right subtree. 3 p[left[y]] ← x 4 p[y] ← p[x] ▹ Link xParent to y. 5 if p[x] = nil[T] 6 then root[T] ← y 7 else if x = left[p[x]] 8 then left[p[x]] 9 else right[p[x]] ← Y 10 left[y] ← x ▹ Put x on y'P [x] ← yCopy the code
2. Right
Red-black tree insertion
Insertion of binary lookup tree
Tree-insert (T, z) 1 Y ← NIL 2 X ← root[T] 3while4 x indicates a NILdoY please x 5if key[z] < key[x]
6 then7 x please left [x]elseX ← right[x] 8 P [z] ← y 9if y = NIL
10 thenRoot [T] ← z ⊹ Tree T was empty 11else if key[z] < key[y]
12 thenLeft [y] please zelseRight [y] please zCopy the code
Red-black tree insertion and insertion repair
RB-INSERT(T, z)
1 y ← nil[T]
2 x ← root[T]
3 while4 x indicates a nil [T]doY please x 5if key[z] < key[x]
6 then7 x please left [x]elseX ← right[x] 8 P [z] ← y 9if y = nil[T]
10 thenRoot [T] please zelse if key[z] < key[y]
12 thenLeft [y] please zelseRight [y] ← Z 14 left[z] ← nil[T] 15 Right [z] ← nil[T] 16 color[z] ← RED 17 RB- insert-fixup (T, z)Copy the code
-
If I’m inserting the root, because the tree is empty, I’m just going to violate property 2, so I’m just going to color it black.
-
If I insert a node whose parent is black, and since that doesn’t violate properties 2 and 4, the red-black tree is not broken, I don’t do anything.
-
Insert fix case 1: If the parent of the current node is red and the other child of the grandfather node is red
-
Insert repair case 2: the parent of the current node is red, the uncle node is black, and the current node is the right child of its parent
-
Insert repair case 3: The parent of the current node is red, the uncle node is black, and the current node is the left child of its parent
RB- insert-fixup (T,z) 1while color[p[z]] = RED
2 do if p[z] = left[p[p[z]]]
3 thenY please right [p [p] [z]] 4if color[y] = RED
5 thenColor [P [z]] ← BLACK ▹ Case 1 6 color[Y] ← BLACK ▹ Case 1 7 color[P [P [z]]] ← RED ▹ Case 1 8 Z ← P [P [z]] ▹ Case 1 9else if z = right[p[z]]
10 thenZ ← p[z] ▹ Case 2 11 left-rotate (T, Z) ▹ Case 2 12 color[P [z]] ← BLACK ▹ Case 3 13 color[P [p[z]]] ← RED ▹ Case 3 14 right-rotate (T, P [p[z]]) ▹ Case 3 15else (same as then clause
with "right" and "left"Only very thrifty) 16 color[root[T]] ← BLACKCopy the code
Insert repair condition 1:
The parent of the current node is red and the other child of the grandparent is red.
1 while color[p[z]] = RED
2 do if p[z] = left[p[p[z]]]
3 thenY please right [p [p] [z]] 4if color[y] = RED Copy the code
5 thenColor [P [z]] ← BLACK ▹ Case 1 6 color[Y] ← BLACK ▹ Case 1 7 color[P [P [z]]] ← RED ▹ Case 1 8 Z ← P [P [z]] ▹ Case 1Copy the code
For case 1, before the change [the current node is 4 nodes] :
Insert repair condition 2:
The parent of the current node is red, the uncle is black, and the current node is the right child of its parent
9 else if z = right[p[z]]
10 thenZ ← p[z] ▹ Case 2 11 Left-rotate (T, z) ▹ Case 2Copy the code
Before the change[Current node is 7] :
Insert repair condition 3:
The parent of the current node is red, the uncle is black, and the current node is the left child of its parent
12 color[P [z]] ← BLACK ▹ Case 3 13 color[P [P [z]]] ← RED ▹ Case 3 14 right-rotate (T, P [p[z]]) ▹ Case 3Copy the code
Delete red black tree
Deletion of binary lookup tree
-
No sons, leaves. Set the parent’s child pointer to NULL and delete the child.
-
Only one son. If the parent pointer points to the only child of the child, delete the child.
-
He has two sons. This is the most troublesome case, because after you delete the nodes, you have to make sure that the structure of the search binary tree is satisfied. And it’s actually pretty easy, because we can pick the largest element in the left child or the smallest element in the right child and put it where we want to delete the node, and we can keep the structure unchanged. Of course, you have to remember to adjust the subtree, because there is deletion again. It’s customary to pick the largest of the left sons, and it’s the same thing to pick the smallest of the right sons, it doesn’t make any difference, it just goes from left to right. Here we also select the largest element of the left son and place it at the node to be deleted. The largest element of the left son is actually easy to find, as long as the left son continues to search the right subtree, until it finds a node that does not have a right subtree. That’s the biggest.
TREE-DELETE(T, z)
1 if left[z] = NIL or right[z] = NIL
2 thenY please z 3elseY please TREE - SUCCESSOR (z) 4ifLeft [y] indicates a NIL 5thenX please left [y] 6else[y] 7 x please rightifX indicates a NIL 8thenPlease p p [x] [y] 9if p[y] = NIL
10 thenRoot [T] please x 11else if y = left[p[y]]
12 thenPlease left [p [y]] 13 xelseRight [p] [y] please 14 xifY indicates z 15thenKey [Z] ← Key [y] 16 Copy y's satellite data into z 17 return yCopy the code
Red black tree deletion and deletion repair
1 if left[z] = nil[T] or right[z] = nil[T]
2 thenY please z 3elseY please TREE - SUCCESSOR (z) 4ifLeft [y] indicates a nil [T] 5thenX please left [y] 6elseX ← right[y] 7 P [x] ← P [y] 8if p[y] = nil[T]
9 thenRoot [T] please 10 xelse if y = left[p[y]]
11 thenLeft [p] [y] please x 12elseRight [p] [y] please 13 xifY indicates z 14thenKey [Z] ← Key [y] 15 Copy y's satellite data into z 16 if color[y] = BLACK 17 then RB-DELETE-FIXUP(T, x) 18 return yCopy the code
1 whileX ≠ root[T] and color[x] = BLACK 2do if x = left[p[x]]
3 thenW please right [p] [x] 4if color[w] = RED
5 thenColor [W] ← BLACK ▹ Case 1 6 color[P [x]] ← RED ▹ Case 1 7 left-rotate (T, P [x]) ▹ Case 1 8 W ← right[P [x]] ▹ Case 1 9if color[left[w]] = BLACK and color[right[w]] = BLACK
10 thenColor [W] RED ▹ Case 2 11 x ← p[x] ▹ Case 2 12else if color[right[w]] = BLACK
13 thenColor [left[w]] ← BLACK ▹ Case 3 14 color[W] ← RED ▹ Case 3 15 right-rotate (T, W) ▹ Case 3 16 W ← right[P [x]] ▹ Case 3 17 color[W] ← color[P [x]] ▹ Case 4 18 color[P [x]] ← BLACK ▹ Case 4 19 Color [right[w]] ← BLACK ▹ Case 4 20 left-rotate (T, P [x]) ▹ Case 4 21 x ← root[T] ▹ Case 4 22else (same as then clause with "right" and "left"[X] ← BLACK (for a money rattle)Copy the code
-
A) The current node is red + black
-
B) The current node is black + black and the root node, solution: do nothing, end
-
1: The current node is black + black and the sibling node is red (the children of the parent node and sibling node are black)
-
Case 2: The current node is black plus black and the sibling is black and the two children of the sibling are all black
-
3: The current node is black + black, the sibling node is black, the sibling child is red, the right child is black
-
4: The current node is black-black, its sibling is black, but the right child of the sibling is red, and the left child of the sibling is any color
Delete Fix case 1:
The current node is black + black and the sibling is red (the children of the parent and sibling are black).
// Call line 1-8 of rb-delete-fixup (T, x)whileX ≠ root[T] and color[x] = BLACK 2do if x = left[p[x]]
3 thenW please right [p] [x] 4if color[w] = RED
5 thenColor [W] ← BLACK ▹ Case 1 6 color[P [x]] ← RED ▹ Case 1 7 left-rotate (T, P [x]) ▹ Case 1 8 W ← right[P [x]] ▹ Case 1Copy the code
Delete Fix case 2:
The current node is black plus black and the sibling is black and the two children of the sibling are all black.
// Call rb-delete-fixup (T, x) on line 9-11if color[left[w]] = BLACK and color[right[w]] = BLACK
10 thenColor [W] ← RED ▹ Case 2 11 x p[x] ▹ Case 2Copy the code
Deleted Fix Case 3:
The current node is black + black, the sibling is black, the left sibling is red, the right sibling is black.
// Call line 12-16 of rb-delete-fixup (T, x)else if color[right[w]] = BLACK
13 thenColor [left[W]] ← BLACK ▹ Case 3 14 color[W] ← RED ▹ Case 3 15 right-rotate (T, W) ▹ Case 3 16 W ← RIGHT [P [x]] ▹ Case 3Copy the code
Deleted Fix Case 4:
The current node is black-black, its sibling is black, but the sibling’s right child is red, and the sibling’s left child is any color.
/ / call the RB - DELETE - FIXUP (T, X) Line 17-21 code 17 color[W] ← color[P [x]] ▹ Case 4 18 color[P [x]] ← BLACK ▹ Case 4 19 color[right[W]] ← BLACK ▹ Case 4 20 Left-rotate (T, P [x]) ▹ Case 4 21 x ← root[T] ▹ Case 4Copy the code