chapter 6: transform and conquer 2-3-4 trees, red-black trees the design and analysis of algorithms
Post on 20-Dec-2015
226 views
TRANSCRIPT
![Page 1: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/1.jpg)
Chapter 6: Transform and
Conquer
2-3-4 Trees, Red-Black Trees
The Design and Analysis of Algorithms
![Page 2: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/2.jpg)
2
Chapter 6. Chapter 6. 2-3-4 Trees, Red-Black Trees Basic Idea 2-3-4 Trees
Definition Operations Complexity
Red-Black Trees Definition Properties Insertion
![Page 3: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/3.jpg)
3
Basic Idea Disadvantages of Binary Search trees –
worst case complexity is O(N)Solution to the problem: AVL trees – keep the tree balanced Multi-way search tree – decrease tree levels
2-3 and 2-3-4 trees Disadvantages: nodes with different structure
Red-Black trees – use advantages of 2-3-4 trees with binary nodes
![Page 4: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/4.jpg)
4
Multi-Way Trees
k1 < k2 < … < kn-1
< k1 [k1, k2 ) kn-1
![Page 5: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/5.jpg)
5
2-3-4 Trees - Definition Three types of nodes:
2-node: contains one key, has two links 3-node: contains 2 ordered keys, has 3 links 4-node: contains 3 ordered keys, has 4 links
All leaves must be on the same level, i.e. the tree is perfectly height-balanced. This is achieved by allowing more than one key in a node
![Page 6: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/6.jpg)
6
2-3-4 Trees - Example
J P
D L N R T W
B F H K M O Q S U V X
![Page 7: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/7.jpg)
7
2-3-4 Trees - Operations
Search – straightforward: start comparing with the root and branch accordingly
Insert: The new key is inserted at the lowest internal level
![Page 8: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/8.jpg)
8
Insert in a 2-node
The 2-node becomes a 3-node .
P M PM
![Page 9: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/9.jpg)
9
Insert in a 3-node
The 3-node becomes a 4-node .
M P M P RR
![Page 10: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/10.jpg)
10
Insert in a 4-node
Bottom-up Insertion: Promotion The 4-node is split, and the middle element is
moved up – inserted in the parent node. The process is called promotion and may continue up the top of the tree.
If the 4-node is a root (no parent), then a new root is created.
After the split the insertion proceeds as in the previous cases.
![Page 11: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/11.jpg)
11
Insert in a 4-node - Example
N
F G L
C
G N
C F L
![Page 12: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/12.jpg)
12
Top-down Insertion
In our way down the tree, whenever we reach a 4-node, we break it up into two
2-nodes, and move the middle element up into the parent node.
In this way we make sure there will be place for the new key
![Page 13: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/13.jpg)
13
Complexity of Search and Insert
Height of the tree:A 2–3–4 tree with minimum number of keys will correspond to a perfect binary tree
N ≥ 1 + 2 + … + 2h = 2 h+1 – 1 h ≤ log(N+1) – 1
A 2–3–4 tree with maximum number of keys will correspond to a perfect 4-tree tree
N ≤ 3(1 + 4 + 42 + … + 4h) = 3. (4 h+1 -1)/3 4 (h+1) ≥ N + 1
h ≥ log4(N + 1) -1 = 1/2 log(N + 1) -1
Therefore h = Θ(log(N))
![Page 14: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/14.jpg)
14
Complexity of Search and Insert
• A search visits O(log N) nodes
• An insertion requires O(log N) node splits
• Each node split takes constant time
• Hence, operations Search and Insert each take time O(log N)
![Page 15: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/15.jpg)
15
Red-Black Trees - Definition
edges are colored red or black
no two consecutive red edges on any root-leaf path
same number of black edges on any root-leaf path (=black height of the tree)
edges connecting leaves are black
![Page 16: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/16.jpg)
16
2-3-4 and Red-Black Trees
2-3-4 tree red-black tree 2-node 2-node 3-node two nodes connected with a red link (left or right)
G N
C F L
G
NF
C L
![Page 17: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/17.jpg)
17
2-3-4 and Red-Black Trees
4-node three nodes connected with red links
G N P
C L
N
PG
C L O
![Page 18: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/18.jpg)
18
2-3-4 and Red-Black Trees
2-3-4 tree Red-black tree
or
![Page 19: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/19.jpg)
19
Red-Black Trees
1/2 log(N+1) B log(N + 1) log(N+1) H 2 log(N + 1)where :
N is the number of internal nodesL is the number of leaves (L = N + 1)H - heightB - black height (count the black edges only)
This implies that searches take time O(logN)
![Page 20: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/20.jpg)
20
Red-Black Trees: Insertion
Perform a standard search to find the leaf where the key should be added
Replace the leaf with an internal node with the new key
Color the incoming edge of the new node red
Add two new leaves, and color their incoming edges black
If the parent had an incoming red edge, we now have two consecutive red edges. We must reorganize tree to remove that violation. What must be done depends on the sibling of the parent.
![Page 21: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/21.jpg)
21
Restructuring Incoming edge of p is red and its sibling is black
single rotation
g
p
n
g
p
n
g - grandparent, p – parent, n – new node)
![Page 22: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/22.jpg)
22
RestructuringDouble Rotations: the new node is between its parent and grandparent in the inorder sequence
p
n
g
g
p
n
Left-right double rotation
![Page 23: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/23.jpg)
23
RestructuringRight-left double rotation
g
p
n
g
n
p
![Page 24: Chapter 6: Transform and Conquer 2-3-4 Trees, Red-Black Trees The Design and Analysis of Algorithms](https://reader035.vdocument.in/reader035/viewer/2022062300/56649d435503460f94a1feb2/html5/thumbnails/24.jpg)
24
Promotion: bottom up rebalancing
Incoming edge of p is red and its sibling is also red
g
p
n
g
p
n
The black depth remains unchanged for all of the
descendants of g
This process will continue
upward beyond g if
necessary: rename g as n and repeat.
Promotions may continue up the tree and are executed O(log N) times.
The time complexity of an insertion is O(logN).