Download presentation
Presentation is loading. Please wait.
Published byErlin Kartawijaya Modified over 6 years ago
1
Monday, April 16, 2018 Announcements… For Today…
Self-Balancing Search Trees Announcements… Lab 09 – Pokemon Reviews due Tonight Lab 10 – Quicksort due Tonight Bonus AVL Lab and Quicksort Reviews due Friday Final Exam Richard's Building April 20-25th. Exam review. Taxes due Tomorrow Direct s to (w/netid) For Today… 11.3 Red-Black Trees
2
Attendance Quiz #40 Self-Balancing Search Trees
3
11.3 Red-Black Trees Insertion into a Red-Black Tree Removal from a Red-Black Tree Performance of a Red-Black Tree 11.3, pgs
4
Red-Black Trees Self-Balancing Search Trees Rudolf Bayer developed the Red-Black tree as a special case of his B-tree. Leo Guibas and Robert Sedgewick later refined the concept and introduced the color convention. Each node of the binary tree has an extra bit (red or black) which are used to ensure the tree remains approximately balanced during insertions and deletions. Balance is preserved by always satisfying certain properties, which collectively constrain how unbalanced the tree can become in the worst case. When the tree is modified, the new tree is efficiently rearranged and repainted to restore the coloring properties. The balancing of the tree is not perfect, but it is good enough to allow it to guarantee searching in O(log n) time.
5
Red-Black Trees A Red-Black tree maintains the following invariants:
Self-Balancing Search Trees A Red-Black tree maintains the following invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 2 14 5 8 7 1
6
Red-Black Trees Height is determined by counting only black nodes.
Self-Balancing Search Trees Height is determined by counting only black nodes. A Red-Black tree is always balanced because the root node’s left and right subtrees must be the same height. By the standards of the AVL tree this tree is out of balance and would be considered a Left-Right tree. However, by the standards of the Red-Black tree, it is balanced, because there are two black nodes (counting the root) in any path from the root to a leaf. 11 2 14 5 8 7 1
7
Insertion into a Red-Black Tree
Self-Balancing Search Trees The algorithm follows the same recursive search process used for all binary search trees to reach the insertion point. When a leaf position is found, the new item is inserted and initially given the color red. If the parent is black, we are done; otherwise there is some rearranging to do. We introduce three situations ("cases") that may occur when a node is inserted; more than one can occur after an insertion. Parent and uncle are red (easy to balance.) Parent is red and has no uncle (1 rotation fixes balance.) Parent is red and has no uncle (1 rotation doesn't fix balance.) Move up tree and fix balance as needed.
8
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 1 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 10 30
9
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 1 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 10 30 35 If a parent is red, and its sibling is also red, they can both be changed to black, and the grandparent to red
10
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 1 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 10 30 35 If a parent is red, and its sibling is also red, they can both be changed to black, and the grandparent to red
11
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 1 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 10 30 35 The root can be changed to black and still maintain invariant 4
12
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 1 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 10 30 35 The root can be changed to black and still maintain invariant 4
13
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 1 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 10 30 35 Balanced tree
14
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 2 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30
15
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 2 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 35 If a parent is red (with no sibling), it can be changed to black, and the grandparent to red
16
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 2 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 35 If a parent is red (with no sibling), it can be changed to black, and the grandparent to red
17
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 2 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 35 There is one black node on the right and none on the left, which violates invariant 4
18
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 2 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 35 Rotate left around the grandparent to correct this
19
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 2 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 30 20 35 Rotate left around the grandparent to correct this
20
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 2 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 30 20 35 Balanced tree
21
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30
22
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 25 If a parent is red (with no sibling), it can be changed to black, and the grandparent to red
23
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 25 If a parent is red (with no sibling), it can be changed to black, and the grandparent to red
24
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 25 A rotation left does not fix the violation of #4
25
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 30 20 25 A rotation left does not fix the violation of #4
26
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 25 Back-up to the beginning (don't perform rotation or change colors)
27
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 30 25 Rotate right about the parent so that the red child is on the same side of the parent as the parent is to the grandparent
28
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 25 30 Rotate right about the parent so that the red child is on the same side of the parent as the parent is to the grandparent
29
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 25 30 NOW, change colors
30
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 25 30 NOW, change colors
31
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 20 25 30 and rotate left . . .
32
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 25 20 30 and rotate left . . .
33
Insertion into a Red-Black Tree
Self-Balancing Search Trees CASE 3 Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 25 20 30 Balanced tree
34
Insertion into a Red-Black Tree
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 2 14 5 8 7 1
35
Insertion into a Red-Black Tree
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 2 14 5 8 7 1 4
36
Insertion into a Red-Black Tree
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 2 14 5 8 7 1 CASE 1 If a parent is red, and its sibling is also red, they can both be changed to black, and the grandparent to red 4
37
Insertion into a Red-Black Tree
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 2 14 5 8 7 1 CASE 1 If a parent is red, and its sibling is also red, they can both be changed to black, and the grandparent to red 4
38
Insertion into a Red-Black Tree
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 2 14 5 8 7 1 The problem has now shifted up the tree 4
39
Insertion into a Red-Black Tree
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 2 14 5 8 7 1 CASE 3 We cannot change 2 to black because its sibling 14 is already black (both siblings have to be red unless there is no sibling) to do the color change 4
40
Insertion into a Red-Black Tree
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 2 14 5 8 7 1 CASE 3 We need to rotate left around 2 so that the red child is on the same side of the parent as the parent is to the grandparent 4
41
Insertion into a Red-Black Tree
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 7 14 5 1 8 2 CASE 3 We need to rotate left around 2 so that the red child is on the same side of the parent as the parent is to the grandparent 4
42
Insertion into a Red-Black Tree
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 7 14 5 1 8 2 CASE 3 Change colors 4
43
Insertion into a Red-Black Tree
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 7 14 5 1 8 2 CASE 3 Change colors 4
44
Insertion into a Red-Black Tree
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 11 7 14 5 1 8 2 CASE 3 Rotate right around 11 to restore the balance 4
45
Insertion into a Red-Black Tree
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 7 2 11 5 1 14 8 CASE 3 4 Rotate right around 11 to restore the balance
46
Insertion into a Red-Black Tree
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. 7 2 11 5 1 14 8 4 Balanced tree
47
Red-Black Tree Example
Self-Balancing Search Trees Build a Red-Black tree for the words in "The quick brown fox jumps over the lazy dog"
48
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. The quick
49
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. The quick brown CASE 3 Rotate so that the child is on the same side of its parent as its parent is to the grandparent
50
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. The brown quick CASE 3 Change colors
51
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. The brown quick CASE 3 Change colors
52
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. The brown quick CASE 3 Rotate left
53
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick
54
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick fox
55
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick fox CASE 1 fox's parent and its parent's sibling are both red. Change colors.
56
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick fox CASE 1 fox's parent and its parent's sibling are both red. Change colors.
57
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick fox CASE 1 We can change brown's color to black and not violate #4
58
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick fox CASE 1 We can change brown's color to black and not violate #4
59
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick fox jumps
60
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick fox jumps CASE 3 Rotate so that red child is on same side of its parent as its parent is to the grandparent
61
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick jumps fox CASE 3 Change fox's parent and grandparent colors
62
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick jumps fox CASE 3 Change fox's parent and grandparent colors
63
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The quick jumps fox CASE 3 Rotate right about quick
64
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick CASE 3 Rotate right about quick
65
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick over
66
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick over CASE 1 Change colors of parent, parent's sibling and grandparent
67
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick over CASE 1 Change colors of parent, parent's sibling and grandparent
68
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick over the No changes needed
69
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick over the lazy
70
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick over the lazy CASE 1 Because over and the are both red, change parent, parent's sibling and grandparent colors
71
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick over the lazy CASE 2
72
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. brown The jumps fox quick over the lazy CASE 2
73
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. jumps brown quick The fox over the lazy CASE 2
74
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. jumps brown quick The fox over the dog lazy
75
Red-Black Tree Example
Self-Balancing Search Trees Invariants: Every node is either red or black. The root node is always black. Every red node must either have zero or two black children. Every root-null path much have the same number of black nodes. jumps brown quick The fox over the dog lazy Balanced tree
76
Red-Black Tree Class Implementation
Self-Balancing Search Trees
77
Red-Black Tree Class Implementation
Self-Balancing Search Trees
78
Red-Black Tree Insertion Algorithm
Self-Balancing Search Trees The insertion algorithm can be implemented with a data structure that has a pointer to the parent of each node. The following algorithm detects the need for fix-ups from the grandparent level. Also, whenever a black node with two red children is detected on the way down the tree, it is changed to red and the children are changed to black; any resulting problems can be fixed on the way back up.
79
Red-Black Tree Insertion Algorithm
Self-Balancing Search Trees
80
The insert Starter Function
Self-Balancing Search Trees template<typename Item_Type> bool Red_Black_Tree<Item_Type>::insert(const Item_Type& item) { if (this->root == NULL) RBNode<Item_Type>* new_root = new RBNode<Item_Type>(item); new_root->is_red = false; this->root = new_root; return true; } else // Call the recursive insert function. bool return_value = insert(this->root, item); // Force the root to be black set_red(this->root, false); return return_value;
81
The insert Starter Function
Self-Balancing Search Trees
82
The Function is_red Self-Balancing Search Trees
83
Removal from a Red-Black Tree
Self-Balancing Search Trees Remove a node only if it is a leaf or has only one child. Otherwise, the node containing the inorder predecessor of the value being removed is removed. If the node removed is red, nothing further is done. If the node removed is black and has a red child, then the red child takes its place and is colored black. If a black leaf is removed, the black height becomes unbalanced. A programming project at the end of the chapter describes other cases.
84
Performance of a Red-Black Tree
Self-Balancing Search Trees The upper limit on the height for a Red-Black tree is 2 log2 n + 2 which is still O(log n). As with AVL trees, the average performance is significantly better than the worst-case performance. Empirical studies show that the average cost of searching a Red-Black tree built from random values is log2 n. Red-Black trees and AVL trees both give performance close to the performance of a complete binary tree. Red-Black Trees vs. AVL Trees Insertion and removal are faster for Red-Black trees. Retrieval is faster for AVL trees.
85
Pros and Cons of AVL Trees
Self-Balancing Search Trees Argument for AVL trees: Search is O(log n) since AVL trees are always balanced. Insertion and deletions are also O(log n). The height balancing adds no more than a constant factor to the speed of insertion. Arguments against using AVL trees: Difficult to program & debug; more space for balance factor. Asymptotically faster but rebalancing costs time. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). May be OK to have O(n) for a single operation if total run time for many consecutive operations is fast (e.g. Splay trees).
86
Pros and Cons of AVL Trees
Self-Balancing Search Trees
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.