balanced trees - pepperdine university · design patterns for data structures chapter 11 balanced...
TRANSCRIPT
Design Patterns for Data Structures
Balanced Trees
Chapter 10
Design Patterns for Data Structures
Four height-balanced trees:
Red-Black binary treeFaster than AVL for insertion and removal
Adelsen-Velskii Landis (AVL) binary treeFaster than red-black for lookup
B-treen-way balanced tree
Nguyen-Wong B-treesComposite pattern n-way balanced tree
Chapter 10
Design Patterns for Data Structures LLRBTree
Demo LLRBTree
Insert: 10 20 30 40 50 60 70 80 90
Design Patterns for Data Structures
Chapter 11
Balanced Trees
[Introduce the concept of a balanced tree.]
11.1 The Left-Leaning Red-Black Tree[Introduce the concept of a red-black tree.]
Definitions and performanceHere is the definition of a red-black tree.
• A red-black tree is a binary search tree.• Every link is either red or black.• Every child that is an empty tree is a leaf with a black link to its parent.• No path from the root to any leaf has two red links in a row.• All paths from each node to all its leaves have the same number of black links.
The number of black links from the root to any leaf is called the black height of the tree,denoted bh(). In practice, the color of a link is stored in its child node.
Here is the definition of a left-leaning red-black tree.
• A left-leaning red-black tree is a red-black tree.• Every red link is to a left child.
Figure 11.1 is a left-leaning red-black tree with a black height of 3. Many implemen-tations of red-black trees in the literature do not have this extra left-leaning restriction.However, Sedgewick, whose algorithms for red-black trees we present here, has shownthat this restriction greatly simplifies the code for both insertion and deletion.
Hello, here is some text without a meaning. This text should show, how a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like≫Huardest gefburn≪. Kjift – Never mind! A blind text like this gives you informationabout the selected font, how the letters are written and the impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original
275
Properties of a red-black tree
Chapter 10
Design Patterns for Data Structures Chapter 10
Chapter 11
Balanced Trees
[Introduce the concept of a balanced tree.]
11.1 The Left-Leaning Red-Black Tree[Introduce the concept of a red-black tree.]
Definitions and performanceHere is the definition of a red-black tree.
• A red-black tree is a binary search tree.• Every link is either red or black.• Every child that is an empty tree is a leaf with a black link to its parent.• No path from the root to any leaf has two red links in a row.• All paths from each node to all its leaves have the same number of black links.
The number of black links from the root to any leaf is called the black height of the tree,denoted bh(). In practice, the color of a link is stored in its child node.
Here is the definition of a left-leaning red-black tree.
• A left-leaning red-black tree is a red-black tree.• Every red link is to a left child.
Figure 11.1 is a left-leaning red-black tree with a black height of 3. Many implemen-tations of red-black trees in the literature do not have this extra left-leaning restriction.However, Sedgewick, whose algorithms for red-black trees we present here, has shownthat this restriction greatly simplifies the code for both insertion and deletion.
Hello, here is some text without a meaning. This text should show, how a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like≫Huardest gefburn≪. Kjift – Never mind! A blind text like this gives you informationabout the selected font, how the letters are written and the impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original
275
Properties of a left-leaning red-black tree
Sedgewick, has shown that this restriction greatly simplifies the code for both insertion and deletion.
Design Patterns for Data Structures Chapter 10
40
10
5 15
20 50
70
60
45 55
65 80
35
25
75h = 6
12nil nil
nil nil
nil
nil nil
nil nil
nil nil nil nil nil
nil
nil nil
The number of black links from the root to any leaf is called the black height of the tree, denoted bh ( ).
bh (60) = 3
Design Patterns for Data Structures LLRBTree
Performance of LLRBTree
Design Patterns for Data Structures
276 Chapter 11 Balanced Trees
40
10
5 15
20 50
70
60
45 55
65 80
35
25
75h = 6
12nil nil
nil nil
nil
nil nil
nil nil
nil nil nil nil nil
nil
nil nil
Figure 11.1 A left-leaning red-black tree with a height of 5 and a black height of 3.
language. There is no need for a special contents, but the length of words should matchto the language.
Here is a lemma that puts a lower limit on the black height of a tree with a givenheight.
Lemma 1: No path from the root to a leaf is more than twice as long as any other. In ared-black tree of height h rooted at x, bh(x)≥ h/2.
Proof : Given a tree rooted at x with a black height of bh(x), the shortest path to a leafis a path with no red links. Therefore, the shortest path has length bh(x). For example,the path ⟨60,70,65,nil⟩ of length 3 in Figure 11.1 is such a path.
The longest path is a path with the maximum number of red links. Because twoconsecutive red links are not allowed, the longest path begins with a red link, alter-nates red and black links, and ends with a black link to a leaf. For example, the path⟨60,40,20,10,15,12,nil⟩ of length 6 in Figure 11.1 is such a path. In general, the longestpath has length 2bh(x). All paths from the root x to a leaf must have lengths betweenthe two extremes bh(x) and 2bh(x). So, no path is more than twice as long as any otherpath.
The maximum height of a red-black tree is the height of a tree that contains thelongest possible path from the root to a leaf. Its height is therefore equal to the lengthof the longest path, which is 2bh(x). For example, in Figure 11.1 the longest path haslength 6 and the height of the tree is 6. So, in general h ≤ 2bh(x), or bh(x)≥ h/2.
The constraint of no two consecutive red links is what gives a red-black tree itsbalance. Lemma 1 shows that the constraint puts a lower limit on the black height of ared-black tree for a tree with a given height.
Copyright c⃝: 1998, Dung X. Nguyen and J. Stanley Warford Revised: August 30, 2015
40
10
5 15
20 50
70
60
45 55
65 80
35
25
75h = 6
12nil nil
nil nil
nil
nil nil
nil nil
nil nil nil nil nil
nil
nil nil
LLRBTree
Design Patterns for Data Structures
40
10
5 15
20 50
70
60
45 55
65 80
35
25
75h = 6
12nil nil
nil nil
nil
nil nil
nil nil
nil nil nil nil nil
nil
nil nil
LLRBTree
Define nx, h as the number of internal nodes in ared-black tree of height h rooted at node x.
Example: n60, 6 = 16Example: n20, 4 = 7
Design Patterns for Data Structures
11.1 The Left-Leaning Red-Black Tree 277
Define nx,h as the number of internal nodes in a red-black tree of height h rooted atnode x. For example, in Figure 11.1 n60,6 equals 16 because there are 16 internal nodesin the red-black tree of height 6 rooted at 60. Here is a lemma that puts a lower limit onthe number of internal nodes nx,h for a given black height.
Lemma 2: nx,h ≥ 2bh(x)−1.Proof : The proof is by mathematical induction on the height of the tree.Base case:
nx,h ≥ 2bh(x)−1= ⟨Take as the base case that the root is nil⟩
nnil,h ≥ 2bh(nil)−1= ⟨With a root of nil, there are no internal nodes⟩
0 ≥ 2bh(nil)−1= ⟨With a root of nil, the black height is 0⟩
0 ≥ 20 −1= ⟨Math⟩
true
Induction case: Must prove that nx,h ≥ 2bh(x)−1 for a height h tree using nw,h−1 ≥2bh(w)−1 for a height h−1 tree as the inductive hypothesis.
If the link from x to a child c is red, the black height of the child equals the blackheight of x, bh(c) = bh(x). If the link from x to a child c is black, the black height ofthe child is one less than the black height of x, bh(c) = bh(x)− 1. Furthermore, theminimum number of nodes for the tree rooted at x occurs when both its children havethe minimum number of nodes, that is, when the links to them are both black. Definingl as the left child of x and r as the right child of x,
nx,h
= ⟨Definition of binary tree⟩1+nl,h−1 +nr,h−1
≥ ⟨Inductive hypothesis, twice⟩1+2bh(l)−1+2bh(r)−1
≥ ⟨Minimum occurs when links to l and r are black⟩1+2bh(x)−1 −1+2bh(x)−1 −1
= ⟨Math⟩1+2(2bh(x)−1 −1)
= ⟨Math⟩2bh(x)−1
Lemma 3: nx,h ≥ 2h/2 −1.Proof :
nx,h
≥ ⟨Lemma 2⟩2bh(x)−1
Revised: August 30, 2015 Copyright c⃝: 1998, Dung X. Nguyen and J. Stanley Warford
LLRBTree
Design Patterns for Data Structures
11.1 The Left-Leaning Red-Black Tree 277
Define nx,h as the number of internal nodes in a red-black tree of height h rooted atnode x. For example, in Figure 11.1 n60,6 equals 16 because there are 16 internal nodesin the red-black tree of height 6 rooted at 60. Here is a lemma that puts a lower limit onthe number of internal nodes nx,h for a given black height.
Lemma 2: nx,h ≥ 2bh(x)−1.Proof : The proof is by mathematical induction on the height of the tree.Base case:
nx,h ≥ 2bh(x)−1= ⟨Take as the base case that the root is nil⟩
nnil,h ≥ 2bh(nil)−1= ⟨With a root of nil, there are no internal nodes⟩
0 ≥ 2bh(nil)−1= ⟨With a root of nil, the black height is 0⟩
0 ≥ 20 −1= ⟨Math⟩
true
Induction case: Must prove that nx,h ≥ 2bh(x)−1 for a height h tree using nw,h−1 ≥2bh(w)−1 for a height h−1 tree as the inductive hypothesis.
If the link from x to a child c is red, the black height of the child equals the blackheight of x, bh(c) = bh(x). If the link from x to a child c is black, the black height ofthe child is one less than the black height of x, bh(c) = bh(x)− 1. Furthermore, theminimum number of nodes for the tree rooted at x occurs when both its children havethe minimum number of nodes, that is, when the links to them are both black. Definingl as the left child of x and r as the right child of x,
nx,h
= ⟨Definition of binary tree⟩1+nl,h−1 +nr,h−1
≥ ⟨Inductive hypothesis, twice⟩1+2bh(l)−1+2bh(r)−1
≥ ⟨Minimum occurs when links to l and r are black⟩1+2bh(x)−1 −1+2bh(x)−1 −1
= ⟨Math⟩1+2(2bh(x)−1 −1)
= ⟨Math⟩2bh(x)−1
Lemma 3: nx,h ≥ 2h/2 −1.Proof :
nx,h
≥ ⟨Lemma 2⟩2bh(x)−1
Revised: August 30, 2015 Copyright c⃝: 1998, Dung X. Nguyen and J. Stanley Warford
LLRBTree
Link from x to child c is red => bh(c) = bh(x).Link from x to child c is black => bh(c) = bh(x) – 1.
Minimum number of nodes in tree rooted at x => both children have minimum => links from x to both are black
Design Patterns for Data Structures
���� 7KH�/HIW�/HDQLQJ�5HG�%ODFN�7UHH ���
WKH�QXPEHU�RI�LQWHUQDO�QRGHV nx,h IRU�D�JLYHQ�EODFN�KHLJKW�
/HPPD��� nx,h ≥ 2bh(x)−1�3URRI � 7KH�SURRI�LV�E\�PDWKHPDWLFDO�LQGXFWLRQ�RQ�WKH�KHLJKW�RI�WKH�WUHH�%DVH�FDVH�
nx,h ≥ 2bh(x)−1= ⟨7DNH�DV�WKH�EDVH�FDVH�WKDW�WKH�URRW�LV�QLO⟩
nQLO,h ≥ 2bh(QLO)−1= ⟨:LWK�D�URRW�RI�QLO� WKHUH�DUH�QR�LQWHUQDO�QRGHV⟩
0 ≥ 2bh(QLO)−1= ⟨:LWK�D�URRW�RI�QLO� WKH�EODFN�KHLJKW�LV��⟩
0 ≥ 20 −1= ⟨0DWK⟩
WUXH
,QGXFWLRQ�FDVH� 0XVW�SURYH�WKDW nx,h ≥ 2bh(x)−1 IRU�D�KHLJKW h WUHH�XVLQJ nw,h−1 ≥2bh(w)−1 IRU�D�KHLJKW h−1 WUHH�DV�WKH�LQGXFWLYH�K\SRWKHVLV�
,I�WKH�OLQN�IURP x WR�D�FKLOG c LV�UHG� WKH�EODFN�KHLJKW�RI�WKH�FKLOG�HTXDOV�WKH�EODFNKHLJKW�RI x� bh(c) = bh(x)� ,I�WKH�OLQN�IURP x WR�D�FKLOG c LV�EODFN� WKH�EODFN�KHLJKW�RIWKH�FKLOG�LV�RQH�OHVV�WKDQ�WKH�EODFN�KHLJKW�RI x� bh(c) = bh(x)− 1� )XUWKHUPRUH� WKHPLQLPXP�QXPEHU�RI�QRGHV�IRU�WKH�WUHH�URRWHG�DW x RFFXUV�ZKHQ�ERWK�LWV�FKLOGUHQ�KDYHWKH�PLQLPXP�QXPEHU�RI�QRGHV� WKDW�LV� ZKHQ�WKH�OLQNV�WR�WKHP�DUH�ERWK�EODFN� 'HÀQLQJl DV�WKH�OHIW�FKLOG�RI x DQG r DV�WKH�ULJKW�FKLOG�RI x�
nx,h
= ⟨'HÀQLWLRQ�RI�ELQDU\�WUHH⟩1+nl,h−1 +nr,h−1
≥ ⟨,QGXFWLYH�K\SRWKHVLV� WZLFH⟩1+2bh(l)−1+2bh(r)−1
≥ ⟨0LQLPXP�RFFXUV�ZKHQ�OLQNV�WR l DQG r DUH�EODFN⟩1+2bh(x)−1 −1+2bh(x)−1 −1
= ⟨0DWK⟩2 ·2bh(x)−1 −1
= ⟨0DWK⟩2bh(x)−1
:KHUHDV�/HPPD���SXWV�D�ORZHU�OLPLW�RQ�WKH�QXPEHU�RI�LQWHUQDO�QRGHV nx,h IRU�D�JLYHQEODFN�KHLJKW bh(x)� /HPPD���SXWV�D�ORZHU�OLPLW�RQ�WKH�QXPEHU�RI�LQWHUQDO�QRGHV nx,h IRUD�JLYHQ�WUHH�KHLJKW h�
/HPPD��� nx,h ≥ 2h/2 −1�3URRI �
nx,h
≥ ⟨/HPPD��⟩2bh(x)−1
5HYLVHG� 1RYHPEHU���� ���� &RS\ULJKW��� ����� 'XQJ�;��1JX\HQ�DQG�-��6WDQOH\�:DUIRUG
11.1 The Left-Leaning Red-Black Tree 277
Define nx,h as the number of internal nodes in a red-black tree of height h rooted atnode x. For example, in Figure 11.1 n60,6 equals 16 because there are 16 internal nodesin the red-black tree of height 6 rooted at 60. Here is a lemma that puts a lower limit onthe number of internal nodes nx,h for a given black height.
Lemma 2: nx,h ≥ 2bh(x)−1.Proof : The proof is by mathematical induction on the height of the tree.Base case:
nx,h ≥ 2bh(x)−1= ⟨Take as the base case that the root is nil⟩
nnil,h ≥ 2bh(nil)−1= ⟨With a root of nil, there are no internal nodes⟩
0 ≥ 2bh(nil)−1= ⟨With a root of nil, the black height is 0⟩
0 ≥ 20 −1= ⟨Math⟩
true
Induction case: Must prove that nx,h ≥ 2bh(x)−1 for a height h tree using nw,h−1 ≥2bh(w)−1 for a height h−1 tree as the inductive hypothesis.
If the link from x to a child c is red, the black height of the child equals the blackheight of x, bh(c) = bh(x). If the link from x to a child c is black, the black height ofthe child is one less than the black height of x, bh(c) = bh(x)− 1. Furthermore, theminimum number of nodes for the tree rooted at x occurs when both its children havethe minimum number of nodes, that is, when the links to them are both black. Definingl as the left child of x and r as the right child of x,
nx,h
= ⟨Definition of binary tree⟩1+nl,h−1 +nr,h−1
≥ ⟨Inductive hypothesis, twice⟩1+2bh(l)−1+2bh(r)−1
≥ ⟨Minimum occurs when links to l and r are black⟩1+2bh(x)−1 −1+2bh(x)−1 −1
= ⟨Math⟩1+2(2bh(x)−1 −1)
= ⟨Math⟩2bh(x)−1
Lemma 3: nx,h ≥ 2h/2 −1.Proof :
nx,h
≥ ⟨Lemma 2⟩2bh(x)−1
Revised: August 30, 2015 Copyright c⃝: 1998, Dung X. Nguyen and J. Stanley Warford
LLRBTree
Design Patterns for Data Structures LLRBTree��� &KDSWHU��� %DODQFHG�7UHHV
:KHUHDV�/HPPD���SXWV�D�ORZHU�OLPLW�RQ�WKH�QXPEHU�RI�LQWHUQDO�QRGHV nx,h IRU�D�JLYHQEODFN�KHLJKW bh(x)� /HPPD���SXWV�D�ORZHU�OLPLW�RQ�WKH�QXPEHU�RI�LQWHUQDO�QRGHV nx,h IRUD�JLYHQ�WUHH�KHLJKW h�
/HPPD��� nx,h ≥ 2h/2 −1�3URRI �
nx,h
≥ ⟨/HPPD��⟩2bh(x)−1
≥ ⟨/HPPD��⟩2h/2 −1
7KHRUHP� UHG�EODFN�WUHH�KHLJKW�ERXQG� $ UHG�EODFN�WUHH�ZLWK n LQWHUQDO�QRGHV�KDVKHLJKW h ≤ 2lg(n+1)�
3URRI � 6WDUWLQJ�ZLWK�/HPPD���
n ≥ 2h/2 −1= ⟨0DWK⟩
2h/2 ≤ n+1= ⟨7DNH�OJ�RI�ERWK�VLGHV� PRQRWRQLFLW\�RI lg()⟩
h/2 ≤ lg(n+1)= ⟨0DWK⟩
h ≤ 2lg(n+1)
)RU�FRPSDULVRQ� KHLJKW�EDODQFHG�$9/ WUHHV�KDYH h≤ 1.44lg(n+2)−0.33� 7KH\�DUHPRUH�EDODQFHG�WKDQ�UHG�EODFN�WUHHV�DQG�KDYH�IDVWHU�UHWULHYDO�������FRHIÀFLHQW�FRPSDUHGWR���IRU�UHG�EODFN�WUHHV��EXW�KDYH�VORZHU�LQVHUWLRQ�DQG�UHPRYDO�
,PSOHPHQWDWLRQ�DQG�EDVLF�RSHUDWLRQV+HOOR� KHUH�LV�VRPH�WH[W�ZLWKRXW�D�PHDQLQJ� 7KLV�WH[W�VKRXOG�VKRZ�ZKDW�D�SULQWHG�WH[WZLOO�ORRN�OLNH�DW�WKLV�SODFH� ,I�\RX�UHDG�WKLV�WH[W� \RX�ZLOO�JHW�QR�LQIRUPDWLRQ� 5HDOO\",V�WKHUH�QR�LQIRUPDWLRQ" ,V�WKHUH�D�GLIIHUHQFH�EHWZHHQ�WKLV�WH[W�DQG�VRPH�QRQVHQVH�OLNH´+XDUGHVW�JHIEXUQµ" .MLIW�²�QRW�DW�DOO� $ EOLQG�WH[W�OLNH�WKLV�JLYHV�\RX�LQIRUPDWLRQDERXW�WKH�VHOHFWHG�IRQW� KRZ�WKH�OHWWHUV�DUH�ZULWWHQ�DQG�DQ�LPSUHVVLRQ�RI�WKH�ORRN� 7KLVWH[W�VKRXOG�FRQWDLQ�DOO�OHWWHUV�RI�WKH�DOSKDEHW�DQG�LW�VKRXOG�EH�ZULWWHQ�LQ�RI�WKH�RULJLQDOODQJXDJH� 7KHUH�LV�QR�QHHG�IRU�VSHFLDO�FRQWHQW� EXW�WKH�OHQJWK�RI�ZRUGV�VKRXOG�PDWFK�WKHODQJXDJH�
+HOOR� KHUH�LV�VRPH�WH[W�ZLWKRXW�D�PHDQLQJ� 7KLV�WH[W�VKRXOG�VKRZ�ZKDW�D�SULQWHGWH[W�ZLOO�ORRN�OLNH�DW�WKLV�SODFH� ,I�\RX�UHDG�WKLV�WH[W� \RX�ZLOO�JHW�QR�LQIRUPDWLRQ� 5HDOO\",V�WKHUH�QR�LQIRUPDWLRQ" ,V�WKHUH�D�GLIIHUHQFH�EHWZHHQ�WKLV�WH[W�DQG�VRPH�QRQVHQVH�OLNH´+XDUGHVW�JHIEXUQµ" .MLIW�²�QRW�DW�DOO� $ EOLQG�WH[W�OLNH�WKLV�JLYHV�\RX�LQIRUPDWLRQDERXW�WKH�VHOHFWHG�IRQW� KRZ�WKH�OHWWHUV�DUH�ZULWWHQ�DQG�DQ�LPSUHVVLRQ�RI�WKH�ORRN� 7KLVWH[W�VKRXOG�FRQWDLQ�DOO�OHWWHUV�RI�WKH�DOSKDEHW�DQG�LW�VKRXOG�EH�ZULWWHQ�LQ�RI�WKH�RULJLQDOODQJXDJH� 7KHUH�LV�QR�QHHG�IRU�VSHFLDO�FRQWHQW� EXW�WKH�OHQJWK�RI�ZRUGV�VKRXOG�PDWFK�WKHODQJXDJH�
&RS\ULJKW��� ����� 'XQJ�;��1JX\HQ�DQG�-��6WDQOH\�:DUIRUG 5HYLVHG� 1RYHPEHU���� ����
Design Patterns for Data Structures
278 Chapter 11 Balanced Trees
≥ ⟨Lemma 1⟩2h/2 −1
Theorem, red-black tree height bound: A red-black tree with n internal nodes hasheight h ≤ 2lg(n+1).
Proof : Starting with Lemma 3,
n ≥ 2h/2 −1= ⟨Math⟩
2h/2 ≤ n+1= ⟨Take lg of both sides, monotonicity of lg()⟩
h/2 ≤ lg(n+1)= ⟨Math⟩
h ≤ 2lg(n+1)
For comparison, height-balanced AVL trees have h≤ 1.44lg(n+2)−0.33. They aremore balanced than red-black trees and have faster retrieval (1.44 coefficient comparedto 2 for red-black trees) but have slower insertion and removal.
Hello, here is some text without a meaning. This text should show, how a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like≫Huardest gefburn≪. Kjift – Never mind! A blind text like this gives you informationabout the selected font, how the letters are written and the impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for a special contents, but the length of words should matchto the language.
Hello, here is some text without a meaning. This text should show, how a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like≫Huardest gefburn≪. Kjift – Never mind! A blind text like this gives you informationabout the selected font, how the letters are written and the impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for a special contents, but the length of words should matchto the language.
Hello, here is some text without a meaning. This text should show, how a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like≫Huardest gefburn≪. Kjift – Never mind! A blind text like this gives you informationabout the selected font, how the letters are written and the impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for a special contents, but the length of words should matchto the language.
Hello, here is some text without a meaning. This text should show, how a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like≫Huardest gefburn≪. Kjift – Never mind! A blind text like this gives you informationabout the selected font, how the letters are written and the impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original
Copyright c⃝: 1998, Dung X. Nguyen and J. Stanley Warford Revised: August 30, 2015
278 Chapter 11 Balanced Trees
≥ ⟨Lemma 1⟩2h/2 −1
Theorem, red-black tree height bound: A red-black tree with n internal nodes hasheight h ≤ 2lg(n+1).
Proof : Starting with Lemma 3,
n ≥ 2h/2 −1= ⟨Math⟩
2h/2 ≤ n+1= ⟨Take lg of both sides, monotonicity of lg()⟩
h/2 ≤ lg(n+1)= ⟨Math⟩
h ≤ 2lg(n+1)
For comparison, height-balanced AVL trees have h≤ 1.44lg(n+2)−0.33. They aremore balanced than red-black trees and have faster retrieval (1.44 coefficient comparedto 2 for red-black trees) but have slower insertion and removal.
Hello, here is some text without a meaning. This text should show, how a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like≫Huardest gefburn≪. Kjift – Never mind! A blind text like this gives you informationabout the selected font, how the letters are written and the impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for a special contents, but the length of words should matchto the language.
Hello, here is some text without a meaning. This text should show, how a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like≫Huardest gefburn≪. Kjift – Never mind! A blind text like this gives you informationabout the selected font, how the letters are written and the impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for a special contents, but the length of words should matchto the language.
Hello, here is some text without a meaning. This text should show, how a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like≫Huardest gefburn≪. Kjift – Never mind! A blind text like this gives you informationabout the selected font, how the letters are written and the impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for a special contents, but the length of words should matchto the language.
Hello, here is some text without a meaning. This text should show, how a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like≫Huardest gefburn≪. Kjift – Never mind! A blind text like this gives you informationabout the selected font, how the letters are written and the impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original
Copyright c⃝: 1998, Dung X. Nguyen and J. Stanley Warford Revised: August 30, 2015
LLRBTree
Design Patterns for Data Structures LLRBTree
Implementation of LLRBTree
Design Patterns for Data Structures LLRBTree10.1 The Left-Leaning Red-Black Tree 371
template<class T>class LLRBTree {private:
Node<T> *_root;
public:LLRBTree(); //Constructor// Post: This left-leaning red-black tree// is initialized to be empty.
~LLRBTree(); //Destructor// Post: This left-leaning red-black tree// is deallocated.
bool contains(T const &data);// Post: Returns true if this tree contains data.
void insert(T const &data);// Post: data is stored in this tree in order// with no duplicates.
bool isEmpty();// Post: Returns true if this tree is empty.
void remove(T const &data);// Pre: This tree contains data.// Post: data is removed from this tree.
void removeMin();// Pre: This tree is not empty.// Post: The minimum value is removed.
void toStream(ostream &os) const;// Post: A string representation of this tree// is streamed to os.
};
Figure 10.2 The LLRBTree specification.
“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?
Revised: November 15, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
Design Patterns for Data Structures LLRBTree
���� 7KH�/HIW�/HDQLQJ�5HG�%ODFN�7UHH ���
WHPSODWH�FODVV 7!FODVV //5%7UHH ^SULYDWH�
1RGH�7! BURRW�
SXEOLF�//5%7UHH��� ��&RQVWUXFWRU�� 3RVW� 7KLV OHIW�OHDQLQJ UHG�EODFN WUHH�� LV LQLWLDOL]HG WR EH HPSW\�
a//5%7UHH��� ��'HVWUXFWRU�� 3RVW� 7KLV OHIW�OHDQLQJ UHG�EODFN WUHH�� LV GHDOORFDWHG�
ERRO FRQWDLQV�7 FRQVW GDWD���� 3RVW� 5HWXUQV WUXH LI WKLV WUHH FRQWDLQV GDWD�
YRLG LQVHUW�7 FRQVW GDWD���� 3RVW� GDWD LV VWRUHG LQ WKLV WUHH LQ RUGHU�� ZLWK QR GXSOLFDWHV�
YRLG UHPRYH�7 FRQVW GDWD���� 3RVW� ,I GDWD LV IRXQG� WKHQ LW LV UHPRYHG�� IURP WKLV WUHH�
YRLG WR6WUHDP�RVWUHDP RV� FRQVW��� 3RVW� $ VWULQJ UHSUHVHQWDWLRQ RI WKLV WUHH�� LV VWUHDPHG WR RV�
`�
)LJXUH ���� 7KH //5%7UHH VSHFLÀFDWLRQ�
´+XDUGHVW�JHIEXUQµ" .MLIW�²�QRW�DW�DOO� $ EOLQG�WH[W�OLNH�WKLV�JLYHV�\RX�LQIRUPDWLRQDERXW�WKH�VHOHFWHG�IRQW� KRZ�WKH�OHWWHUV�DUH�ZULWWHQ�DQG�DQ�LPSUHVVLRQ�RI�WKH�ORRN� 7KLVWH[W�VKRXOG�FRQWDLQ�DOO�OHWWHUV�RI�WKH�DOSKDEHW�DQG�LW�VKRXOG�EH�ZULWWHQ�LQ�RI�WKH�RULJLQDOODQJXDJH� 7KHUH�LV�QR�QHHG�IRU�VSHFLDO�FRQWHQW� EXW�WKH�OHQJWK�RI�ZRUGV�VKRXOG�PDWFK�WKHODQJXDJH�
+HOOR� KHUH�LV�VRPH�WH[W�ZLWKRXW�D�PHDQLQJ� 7KLV�WH[W�VKRXOG�VKRZ�ZKDW�D�SULQWHGWH[W�ZLOO�ORRN�OLNH�DW�WKLV�SODFH� ,I�\RX�UHDG�WKLV�WH[W� \RX�ZLOO�JHW�QR�LQIRUPDWLRQ� 5HDOO\",V�WKHUH�QR�LQIRUPDWLRQ" ,V�WKHUH�D�GLIIHUHQFH�EHWZHHQ�WKLV�WH[W�DQG�VRPH�QRQVHQVH�OLNH´+XDUGHVW�JHIEXUQµ" .MLIW�²�QRW�DW�DOO� $ EOLQG�WH[W�OLNH�WKLV�JLYHV�\RX�LQIRUPDWLRQDERXW�WKH�VHOHFWHG�IRQW� KRZ�WKH�OHWWHUV�DUH�ZULWWHQ�DQG�DQ�LPSUHVVLRQ�RI�WKH�ORRN� 7KLVWH[W�VKRXOG�FRQWDLQ�DOO�OHWWHUV�RI�WKH�DOSKDEHW�DQG�LW�VKRXOG�EH�ZULWWHQ�LQ�RI�WKH�RULJLQDOODQJXDJH� 7KHUH�LV�QR�QHHG�IRU�VSHFLDO�FRQWHQW� EXW�WKH�OHQJWK�RI�ZRUGV�VKRXOG�PDWFK�WKHODQJXDJH�
+HOOR� KHUH�LV�VRPH�WH[W�ZLWKRXW�D�PHDQLQJ� 7KLV�WH[W�VKRXOG�VKRZ�ZKDW�D�SULQWHG
5HYLVHG� 'HFHPEHU���� ���� &RS\ULJKW��� ����� 'XQJ�;��1JX\HQ�DQG�-��6WDQOH\�:DUIRUG
Design Patterns for Data Structures LLRBTree
372 Chapter 10 Balanced Trees
template<class T>class Node {
friend class LLRBTree<T>;
private:Node *_left;T _data;Node *_right;bool _color; // Color of link from parent.
public:Node(T data);// Post: This node is allocated with _data set to data,// _color set to RED, and _left, _right set to nullptr.
~Node();// Post: This node, its left subtree, and its right subtree// are deallocated.
private:Node<T> *fixup(Node<T> *h);void flipColors(Node<T> *h);Node<T> *insert(Node<T> *h, T const &data);bool isRed(Node<T> *h);T min(Node<T> *h);Node<T> *moveRedLeft(Node<T> *h);Node<T> *moveRedRight(Node<T> *h);Node<T> *remove(Node<T> *h, T const &data);Node<T> *removeMin(Node<T> *h);Node<T> *rotateLeft(Node<T> *h);Node<T> *rotateRight(Node<T> *h);void toStream(Node<T> *h, string prRight, string prRoot,
string prLeft, ostream &os) const;};
Figure 10.3 The LLRBTree node specification. Documentation for each method isshown with its implementation.
Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: November 15, 2016
Design Patterns for Data Structures LLRBTree
372 Chapter 10 Balanced Trees
template<class T>class Node {
friend class LLRBTree<T>;
private:Node *_left;T _data;Node *_right;bool _color; // Color of link from parent.
public:Node(T data);// Post: This node is allocated with _data set to data,// _color set to RED, and _left, _right set to nullptr.
~Node();// Post: This node, its left subtree, and its right subtree// are deallocated.
private:Node<T> *fixup(Node<T> *h);void flipColors(Node<T> *h);Node<T> *insert(Node<T> *h, T const &data);bool isRed(Node<T> *h);T min(Node<T> *h);Node<T> *moveRedLeft(Node<T> *h);Node<T> *moveRedRight(Node<T> *h);Node<T> *remove(Node<T> *h, T const &data);Node<T> *removeMin(Node<T> *h);Node<T> *rotateLeft(Node<T> *h);Node<T> *rotateRight(Node<T> *h);void toStream(Node<T> *h, string prRight, string prRoot,
string prLeft, ostream &os) const;};
Figure 10.3 The LLRBTree node specification. Documentation for each method isshown with its implementation.
Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: November 15, 2016
Design Patterns for Data Structures LLRBTree
372 Chapter 10 Balanced Trees
template<class T>class Node {
friend class LLRBTree<T>;
private:Node *_left;T _data;Node *_right;bool _color; // Color of link from parent.
public:Node(T data);// Post: This node is allocated with _data set to data,// _color set to RED, and _left, _right set to nullptr.
~Node();// Post: This node, its left subtree, and its right subtree// are deallocated.
private:Node<T> *fixup(Node<T> *h);void flipColors(Node<T> *h);Node<T> *insert(Node<T> *h, T const &data);bool isRed(Node<T> *h);T min(Node<T> *h);Node<T> *moveRedLeft(Node<T> *h);Node<T> *moveRedRight(Node<T> *h);Node<T> *remove(Node<T> *h, T const &data);Node<T> *removeMin(Node<T> *h);Node<T> *rotateLeft(Node<T> *h);Node<T> *rotateRight(Node<T> *h);void toStream(Node<T> *h, string prRight, string prRoot,
string prLeft, ostream &os) const;};
Figure 10.3 The LLRBTree node specification. Documentation for each method isshown with its implementation.
Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: November 15, 2016
Design Patterns for Data Structures LLRBTree10.1 The Left-Leaning Red-Black Tree 373
const bool RED = true;const bool BLACK = false;
template<class T>class Node {private:
Node *_left;T _data;Node *_right;bool _color; // Color of link from parent.
// ========= isRed =========// Post: Returns false if h == nullptr.// Otherwise returns true if h node is RED.template<class T>bool Node<T>::isRed(Node<T> *h) {
return h == nullptr ? BLACK : h->_color;}
h _color_data
_left _right
Figure 10.4 The node structure for the LLRBTree project.
Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like
Revised: November 15, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
Design Patterns for Data Structures LLRBTree
374 Chapter 10 Balanced Trees
// ========= Constructors =========template<class T>LLRBTree<T>::LLRBTree() {
_root = nullptr; // The empty tree.}
template<class T>Node<T>::Node(T data) :
_color(RED),_data(data),_left(nullptr),_right(nullptr) {
}nil nil
50
Figure 10.5 The constructors for the LLRBTree data structure. New nodes have redlinks from their parents.
“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: November 15, 2016
Design Patterns for Data Structures LLRBTree
11.1 The Left-Leaning Red-Black Tree ���
// Pre: h->_right is RED.// Post: h is rotated left.// Post: A pointer p to the root node of the modified tree is returned.// Post: p->left is RED.template<class T>Node<T> *LLRBTree<T>::rotateLeft(Node<T> *h) {
Node<T> *p = h->_right;h->_right = p->_left;p->_left = h;p->_color = h->_color;h->_color = RED;return p;
}
(a) Before rotate left.
50
60
< 50
> 60> 50< 60
(b) After rotate left.
50
60
< 50
> 60
> 50< 60
ph
)LJXUH ���� 7KH�URWDWH�OHIW�RSHUDWLRQ�IRU�WKH LLRBTree GDWD�VWUXFWXUH�
´+XDUGHVW�JHIEXUQµ" .MLIW�²�QRW�DW�DOO� $ EOLQG�WH[W�OLNH�WKLV�JLYHV�\RX�LQIRUPDWLRQDERXW�WKH�VHOHFWHG�IRQW� KRZ�WKH�OHWWHUV�DUH�ZULWWHQ�DQG�DQ�LPSUHVVLRQ�RI�WKH�ORRN� 7KLVWH[W�VKRXOG�FRQWDLQ�DOO�OHWWHUV�RI�WKH�DOSKDEHW�DQG�LW�VKRXOG�EH�ZULWWHQ�LQ�RI�WKH�RULJLQDOODQJXDJH� 7KHUH�LV�QR�QHHG�IRU�VSHFLDO�FRQWHQW� EXW�WKH�OHQJWK�RI�ZRUGV�VKRXOG�PDWFK�WKHODQJXDJH�
+HOOR� KHUH�LV�VRPH�WH[W�ZLWKRXW�D�PHDQLQJ� 7KLV�WH[W�VKRXOG�VKRZ�ZKDW�D�SULQWHGWH[W�ZLOO�ORRN�OLNH�DW�WKLV�SODFH� ,I�\RX�UHDG�WKLV�WH[W� \RX�ZLOO�JHW�QR�LQIRUPDWLRQ� 5HDOO\",V�WKHUH�QR�LQIRUPDWLRQ" ,V�WKHUH�D�GLIIHUHQFH�EHWZHHQ�WKLV�WH[W�DQG�VRPH�QRQVHQVH�OLNH´+XDUGHVW�JHIEXUQµ" .MLIW�²�QRW�DW�DOO� $ EOLQG�WH[W�OLNH�WKLV�JLYHV�\RX�LQIRUPDWLRQDERXW�WKH�VHOHFWHG�IRQW� KRZ�WKH�OHWWHUV�DUH�ZULWWHQ�DQG�DQ�LPSUHVVLRQ�RI�WKH�ORRN� 7KLVWH[W�VKRXOG�FRQWDLQ�DOO�OHWWHUV�RI�WKH�DOSKDEHW�DQG�LW�VKRXOG�EH�ZULWWHQ�LQ�RI�WKH�RULJLQDOODQJXDJH� 7KHUH�LV�QR�QHHG�IRU�VSHFLDO�FRQWHQW� EXW�WKH�OHQJWK�RI�ZRUGV�VKRXOG�PDWFK�WKHODQJXDJH�
+HOOR� KHUH�LV�VRPH�WH[W�ZLWKRXW�D�PHDQLQJ� 7KLV�WH[W�VKRXOG�VKRZ�ZKDW�D�SULQWHGWH[W�ZLOO�ORRN�OLNH�DW�WKLV�SODFH� ,I�\RX�UHDG�WKLV�WH[W� \RX�ZLOO�JHW�QR�LQIRUPDWLRQ� 5HDOO\",V�WKHUH�QR�LQIRUPDWLRQ" ,V�WKHUH�D�GLIIHUHQFH�EHWZHHQ�WKLV�WH[W�DQG�VRPH�QRQVHQVH�OLNH´+XDUGHVW�JHIEXUQµ" .MLIW�²�QRW�DW�DOO� $ EOLQG�WH[W�OLNH�WKLV�JLYHV�\RX�LQIRUPDWLRQDERXW�WKH�VHOHFWHG�IRQW� KRZ�WKH�OHWWHUV�DUH�ZULWWHQ�DQG�DQ�LPSUHVVLRQ�RI�WKH�ORRN� 7KLVWH[W�VKRXOG�FRQWDLQ�DOO�OHWWHUV�RI�WKH�DOSKDEHW�DQG�LW�VKRXOG�EH�ZULWWHQ�LQ�RI�WKH�RULJLQDO
5HYLVHG� 1RYHPEHU���� ���� &RS\ULJKW��� ����� 'XQJ�;��1JX\HQ�DQG�-��6WDQOH\�:DUIRUG
The rotate left operation
The black height and the order are maintained.
Design Patterns for Data Structures LLRBTree10.1 The Left-Leaning Red-Black Tree 375
// ========= rotateLeft =========// Pre: h->_right is RED.// Post: h is rotated left.// Post: A pointer p to the root node of the modified tree is returned.// Post: p->left is RED.template<class T>Node<T> *Node<T>::rotateLeft(Node<T> *h) {
if (!isRed(h->_right)) {cerr << ”rotateLeft precondition violated: h->_right is not ...throw -1;
}Node<T> *p = h->_right;h->_right = p->_left;p->_left = h;p->_color = h->_color;h->_color = RED;return p;
}
(a) Before rotate left.
50
60
< 50
> 60> 50< 60
(b) After rotate left.
50
60
< 50
> 60
> 50< 60
ph
Figure 10.6 The rotate left operation for the LLRBTree data structure.
language.Hello, here is some text without a meaning. This text should show what a printed
text will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. This
Revised: May 3, 2018 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
Design Patterns for Data Structures LLRBTree��� Chapter 11 Balanced Trees
(a) Initial tree.
50
60
< 50
> 60> 50< 60
(f) h->_color = RED
50
60
< 50
> 60
> 50< 60
p
h 50
60
< 50
> 60> 50< 60
h
(b) p = h->_right
p
50 60
< 50 > 60> 50< 60
h
(c) h->_right = p->_left
p
(d) p->_left = h
50
60
< 50
> 60
> 50< 60
p
h
(e) p->_color = h->_color
50
60
< 50
> 60
> 50< 60
p
h h
)LJXUH ���� 7UDFH�RI�WKH�URWDWH�OHIW�RSHUDWLRQ�IRU�WKH LLRBTree GDWD�VWUXFWXUH�
ODQJXDJH� 7KHUH�LV�QR�QHHG�IRU�VSHFLDO�FRQWHQW� EXW�WKH�OHQJWK�RI�ZRUGV�VKRXOG�PDWFK�WKHODQJXDJH�
+HOOR� KHUH�LV�VRPH�WH[W�ZLWKRXW�D�PHDQLQJ� 7KLV�WH[W�VKRXOG�VKRZ�ZKDW�D�SULQWHGWH[W�ZLOO�ORRN�OLNH�DW�WKLV�SODFH� ,I�\RX�UHDG�WKLV�WH[W� \RX�ZLOO�JHW�QR�LQIRUPDWLRQ� 5HDOO\",V�WKHUH�QR�LQIRUPDWLRQ" ,V�WKHUH�D�GLIIHUHQFH�EHWZHHQ�WKLV�WH[W�DQG�VRPH�QRQVHQVH�OLNH´+XDUGHVW�JHIEXUQµ" .MLIW�²�QRW�DW�DOO� $ EOLQG�WH[W�OLNH�WKLV�JLYHV�\RX�LQIRUPDWLRQDERXW�WKH�VHOHFWHG�IRQW� KRZ�WKH�OHWWHUV�DUH�ZULWWHQ�DQG�DQ�LPSUHVVLRQ�RI�WKH�ORRN� 7KLVWH[W�VKRXOG�FRQWDLQ�DOO�OHWWHUV�RI�WKH�DOSKDEHW�DQG�LW�VKRXOG�EH�ZULWWHQ�LQ�RI�WKH�RULJLQDOODQJXDJH� 7KHUH�LV�QR�QHHG�IRU�VSHFLDO�FRQWHQW� EXW�WKH�OHQJWK�RI�ZRUGV�VKRXOG�PDWFK�WKHODQJXDJH�
+HOOR� KHUH�LV�VRPH�WH[W�ZLWKRXW�D�PHDQLQJ� 7KLV�WH[W�VKRXOG�VKRZ�ZKDW�D�SULQWHGWH[W�ZLOO�ORRN�OLNH�DW�WKLV�SODFH� ,I�\RX�UHDG�WKLV�WH[W� \RX�ZLOO�JHW�QR�LQIRUPDWLRQ� 5HDOO\",V�WKHUH�QR�LQIRUPDWLRQ" ,V�WKHUH�D�GLIIHUHQFH�EHWZHHQ�WKLV�WH[W�DQG�VRPH�QRQVHQVH�OLNH´+XDUGHVW�JHIEXUQµ" .MLIW�²�QRW�DW�DOO� $ EOLQG�WH[W�OLNH�WKLV�JLYHV�\RX�LQIRUPDWLRQDERXW�WKH�VHOHFWHG�IRQW� KRZ�WKH�OHWWHUV�DUH�ZULWWHQ�DQG�DQ�LPSUHVVLRQ�RI�WKH�ORRN� 7KLVWH[W�VKRXOG�FRQWDLQ�DOO�OHWWHUV�RI�WKH�DOSKDEHW�DQG�LW�VKRXOG�EH�ZULWWHQ�LQ�RI�WKH�RULJLQDO
&RS\ULJKW��� ����� 'XQJ�;��1JX\HQ�DQG�-��6WDQOH\�:DUIRUG 5HYLVHG� 1RYHPEHU���� ����
Design Patterns for Data Structures LLRBTree
10.1 The Left-Leaning Red-Black Tree 377
// ========= rotateRight =========// Pre: h->_left is RED.// Post: h is rotated right.// Post: A pointer p to the root node of the modified tree is returned.// Post: p->right is RED.template<class T>Node<T> *Node<T>::rotateRight(Node<T> *h) {
cerr << ”rotateRight(): Exercise for the student.” << endl;throw -1;
}
(a) Before rotate right.
50
60
< 50
> 60> 50< 60
(b) After rotate right.
50
60
< 50
> 60
> 50< 60
h p
Figure 10.8 The rotate right operation for the LLRBTree data structure.
Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Revised: November 15, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
The rotate right operation
The black height and the order are maintained.
Design Patterns for Data Structures LLRBTree
10.1 The Left-Leaning Red-Black Tree 377
// ========= rotateRight =========// Pre: h->_left is RED.// Post: h is rotated right.// Post: A pointer p to the root node of the modified tree is returned.// Post: p->right is RED.template<class T>Node<T> *Node<T>::rotateRight(Node<T> *h) {
cerr << ”rotateRight(): Exercise for the student.” << endl;throw -1;
}
(a) Before rotate right.
50
60
< 50
> 60> 50< 60
(b) After rotate right.
50
60
< 50
> 60
> 50< 60
h p
Figure 10.8 The rotate right operation for the LLRBTree data structure.
Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Revised: November 15, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
Design Patterns for Data Structures LLRBTree
The flip colors operation
10.1 The Left-Leaning Red-Black Tree 379
// ========= flipColors =========// Pre: h has nonempty _left and _right.// Post: The colors of h and its children are flipped.template<class T>void Node<T>::flipColors(Node<T> *h) {
h->_color = !h->_color;cerr << ”flipColors(): Exercise for the student.” << endl;throw -1;
}
h
(a) Before flip colors.
h
(b) After flip colors.
Figure 10.9 The flip colors operation for the LLRBTree data structure.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
InsertionHello, here is some text without a meaning. This text should show what a printed textwill look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Revised: November 15, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
The black height is maintained if h is blackand h->_left and h->_right are both red.
Design Patterns for Data Structures LLRBTree
10.1 The Left-Leaning Red-Black Tree 379
// ========= flipColors =========// Pre: Children of h have the same color.// Post: The colors of h and its children are flipped.template<class T>void Node<T>::flipColors(Node<T> *h) {
if (isRed(h->_left) != isRed(h->_right)) {cerr << ”flipColors precondition violated: Children of h ...throw -1;
}h->_color = !h->_color;cerr << ”flipColors(): Exercise for the student.” << endl;throw -1;
}
h
(a) Before flip colors.
h
(b) After flip colors.
Figure 10.9 The flip colors operation for the LLRBTree data structure.
“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
InsertionHello, here is some text without a meaning. This text should show what a printed textwill look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match the
Revised: May 3, 2018 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
Design Patterns for Data Structures LLRBTree
Inserting intoLLRBTree
Design Patterns for Data Structures LLRBTree380 Chapter 10 Balanced Trees
// ========= insert =========// Post: data is stored in this tree in order with no duplicates.template<class T>void LLRBTree<T>::insert(T const &data) {
_root = _root->insert(_root, data);_root->_color = BLACK;
}
// Post: val is stored in root node h in order as a leaf.// Post: A pointer to the root node of the modified tree is returned.template<class T>Node<T> *Node<T>::insert(Node<T> *h, T const &data) {
if (h == nullptr) {return new Node<T>(data);
}if (data < h->_data) {
h->_left = insert(h->_left, data);}else if (data > h->_data) {
h->_right = insert(h->_right, data);} // else no duplicates.return fixup(h);
}
Figure 10.10 The two insert operations for the LLRBTree data structure.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: November 15, 2016
Design Patterns for Data Structures
Before calling fixup(h),which properties might be violated?
Chapter 10
Chapter 11
Balanced Trees
[Introduce the concept of a balanced tree.]
11.1 The Left-Leaning Red-Black Tree[Introduce the concept of a red-black tree.]
Definitions and performanceHere is the definition of a red-black tree.
• A red-black tree is a binary search tree.• Every link is either red or black.• Every child that is an empty tree is a leaf with a black link to its parent.• No path from the root to any leaf has two red links in a row.• All paths from each node to all its leaves have the same number of black links.
The number of black links from the root to any leaf is called the black height of the tree,denoted bh(). In practice, the color of a link is stored in its child node.
Here is the definition of a left-leaning red-black tree.
• A left-leaning red-black tree is a red-black tree.• Every red link is to a left child.
Figure 11.1 is a left-leaning red-black tree with a black height of 3. Many implemen-tations of red-black trees in the literature do not have this extra left-leaning restriction.However, Sedgewick, whose algorithms for red-black trees we present here, has shownthat this restriction greatly simplifies the code for both insertion and deletion.
Hello, here is some text without a meaning. This text should show, how a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like≫Huardest gefburn≪. Kjift – Never mind! A blind text like this gives you informationabout the selected font, how the letters are written and the impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original
275
Chapter 11
Balanced Trees
[Introduce the concept of a balanced tree.]
11.1 The Left-Leaning Red-Black Tree[Introduce the concept of a red-black tree.]
Definitions and performanceHere is the definition of a red-black tree.
• A red-black tree is a binary search tree.• Every link is either red or black.• Every child that is an empty tree is a leaf with a black link to its parent.• No path from the root to any leaf has two red links in a row.• All paths from each node to all its leaves have the same number of black links.
The number of black links from the root to any leaf is called the black height of the tree,denoted bh(). In practice, the color of a link is stored in its child node.
Here is the definition of a left-leaning red-black tree.
• A left-leaning red-black tree is a red-black tree.• Every red link is to a left child.
Figure 11.1 is a left-leaning red-black tree with a black height of 3. Many implemen-tations of red-black trees in the literature do not have this extra left-leaning restriction.However, Sedgewick, whose algorithms for red-black trees we present here, has shownthat this restriction greatly simplifies the code for both insertion and deletion.
Hello, here is some text without a meaning. This text should show, how a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like≫Huardest gefburn≪. Kjift – Never mind! A blind text like this gives you informationabout the selected font, how the letters are written and the impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original
275
Design Patterns for Data Structures
Before calling fixup(h),which properties might be violated?
Chapter 10
Chapter 11
Balanced Trees
[Introduce the concept of a balanced tree.]
11.1 The Left-Leaning Red-Black Tree[Introduce the concept of a red-black tree.]
Definitions and performanceHere is the definition of a red-black tree.
• A red-black tree is a binary search tree.• Every link is either red or black.• Every child that is an empty tree is a leaf with a black link to its parent.• No path from the root to any leaf has two red links in a row.• All paths from each node to all its leaves have the same number of black links.
The number of black links from the root to any leaf is called the black height of the tree,denoted bh(). In practice, the color of a link is stored in its child node.
Here is the definition of a left-leaning red-black tree.
• A left-leaning red-black tree is a red-black tree.• Every red link is to a left child.
Figure 11.1 is a left-leaning red-black tree with a black height of 3. Many implemen-tations of red-black trees in the literature do not have this extra left-leaning restriction.However, Sedgewick, whose algorithms for red-black trees we present here, has shownthat this restriction greatly simplifies the code for both insertion and deletion.
Hello, here is some text without a meaning. This text should show, how a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like≫Huardest gefburn≪. Kjift – Never mind! A blind text like this gives you informationabout the selected font, how the letters are written and the impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original
275
Chapter 11
Balanced Trees
[Introduce the concept of a balanced tree.]
11.1 The Left-Leaning Red-Black Tree[Introduce the concept of a red-black tree.]
Definitions and performanceHere is the definition of a red-black tree.
• A red-black tree is a binary search tree.• Every link is either red or black.• Every child that is an empty tree is a leaf with a black link to its parent.• No path from the root to any leaf has two red links in a row.• All paths from each node to all its leaves have the same number of black links.
The number of black links from the root to any leaf is called the black height of the tree,denoted bh(). In practice, the color of a link is stored in its child node.
Here is the definition of a left-leaning red-black tree.
• A left-leaning red-black tree is a red-black tree.• Every red link is to a left child.
Figure 11.1 is a left-leaning red-black tree with a black height of 3. Many implemen-tations of red-black trees in the literature do not have this extra left-leaning restriction.However, Sedgewick, whose algorithms for red-black trees we present here, has shownthat this restriction greatly simplifies the code for both insertion and deletion.
Hello, here is some text without a meaning. This text should show, how a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like≫Huardest gefburn≪. Kjift – Never mind! A blind text like this gives you informationabout the selected font, how the letters are written and the impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original
275
Design Patterns for Data Structures
The purpose of fixup(h)is to correct two possible problems.
Chapter 10
• The additional red link might be to a right child.
• The additional red link might produce two red links in a row.
Design Patterns for Data Structures
fixup(h) makes three adjustments.
Chapter 10
• If the red link is a right link, make it a left link.
• If there are two red left links in a row, rotate right in preparation for the last adjustment.
• If the left and right links are both red, flip colors.
Design Patterns for Data Structures LLRBTree
10.1 The Left-Leaning Red-Black Tree 381
// ========= fixup =========// Pre: h->_left and h->_right are roots of left-leaning red-black trees.// Post: h is a root of a left-leaning red-black tree.// Post: A pointer to the root node of the modified tree is returned.template<class T>Node<T> *Node<T>::fixup(Node<T> *h) {
if (isRed(h->_right)) {h = rotateLeft(h);
}if (isRed(h->_left) && isRed(h->_left->_left)) {
h = rotateRight(h);}if (isRed(h->_left) && isRed(h->_right)) {
flipColors(h);}return h;
}
Figure 10.11 The fixup operation for the LLRBTree data structure.
about the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Removing the minimumHello, here is some text without a meaning. This text should show what a printed textwill look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like
Revised: November 15, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
Design Patterns for Data Structures LLRBTree
Inserting as a right leaf with a black sibling.
382 Chapter 10 Balanced Trees
50
60h50h
6050h
(a) Initial tree. (b) Insert 60. (c) Rotate left.nil nil
nil
nil nil
nil
nil nil
Figure 10.12 The fixup operation for inserting as a right leaf with a black sibling.
language. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printed
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: November 18, 2016
LLRBTreeInserting as a left leaf with a red parent.10.1 The Left-Leaning Red-Black Tree 383
70
90h
(a) Initial tree. (b) Insert 60.
h
(c) No fixup.
(d) Rotate right.
90
70h
60
(e) Flip colors.
nil nil
nil
70
90h
h
nil
nil
60
nil nil
70
90h
nil
nil
60
nil nil
nil nil nil nil
90
70h
60
nil nil nil nil
Figure 10.13 The fixup operation for inserting as a left leaf with a red parent.
text will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. This
Revised: November 18, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
LLRBTreeInserting as a right leaf with a red sibling.384 Chapter 10 Balanced Trees
40
50h
(a) Initial tree.nil nil
nil 40
50h
(b) Insert 60.nil nil
60
nil nil
40
50h
(d) Rotate right.nil nil
60
nil nil
(c) Rotate left.
50
60h
nil
nil
40
nil nil
(e) Flip colors.
60
50h
40
nil nil nil nil
Figure 10.14 The fixup operation for inserting as a right leaf with a red sibling.
text should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: November 18, 2016
Design Patterns for Data Structures LLRBTree
Inserting 50 into LLRBTree
LLRBTree
10.1 The Left-Leaning Red-Black Tree 385
(a) Initial tree.
20
10 40
30nil nil
nil nil nil nil
50
20
10 40
30nil nil
nil nil
nil
20
10
40
30
nil nil
nil nil
nil
nil
50
h
h
h
h
h
h
(b) h->right = insert(h->right, data)
(c) h = rotateLeft(h) (d) h = rotateRight(h)
(e) flipColors(h) (f) h = rotateLeft(h)
20
10 40
30nil nil
nil nil nil nil
50
h
h
20
10 40
30nil nil
nil nil nil nil
50
h
h 20
10
40
30
nil nil nil nil
nil nil
50
h
Figure 10.15 Inserting 50 into a left-leaning red-black tree.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information
Revised: November 18, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
LLRBTree
10.1 The Left-Leaning Red-Black Tree 385
(a) Initial tree.
20
10 40
30nil nil
nil nil nil nil
50
20
10 40
30nil nil
nil nil
nil
20
10
40
30
nil nil
nil nil
nil
nil
50
h
h
h
h
h
h
(b) h->right = insert(h->right, data)
(c) h = rotateLeft(h) (d) h = rotateRight(h)
(e) flipColors(h) (f) h = rotateLeft(h)
20
10 40
30nil nil
nil nil nil nil
50
h
h
20
10 40
30nil nil
nil nil nil nil
50
h
h 20
10
40
30
nil nil nil nil
nil nil
50
h
Figure 10.15 Inserting 50 into a left-leaning red-black tree.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you information
Revised: November 18, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
Design Patterns for Data Structures LLRBTree
Deleting fromLLRBTree
Design Patterns for Data Structures
The remove operation
• Find the node to remove.
• Find the minimum of the right child of the node to remove, which is the successor node.
• Copy the data from the successor to the node to remove with the min() operation.
• Remove the minimum of the right child.
Chapter 10
Design Patterns for Data Structures
The remove operation
• Find the node to remove.
• Find the minimum of the right child of the node to remove, which is the successor node.
• Copy the data from the successor to the node to remove with the min() operation.
• Remove the minimum of the right child.
Chapter 10
Design Patterns for Data Structures
Useful fact
• The minimum of a LLRBTree has a nil left child.
• Therefore, it must have a nil right child.- The right child cannot be red because the tree is
left-leaning.- If the right child were not nil, the successor
would not have a unique black height.
• Conclusion: We do not need a remRoot operation!
• We can simply delete the successor.
Chapter 10
Design Patterns for Data Structures
Delete minimum strategy
• If the node to be deleted has a red link to it, the node can be deleted without affecting the black height requirement.
• Problem: The node to be deleted might have a black link to it.
• Solution: Push red links down on the way to the minimum, which creates right-leaning reds.
• Fix the right-leaning reds on the way up.
Chapter 10
Design Patterns for Data Structures
The moveRedLeft operation
• The purpose of the moveRedLeft operation is to push the red links down on the way to the minimum to guarantee that the minimum will be connected to its parent with a red link.
• It is only called on the way down if there are two left black links in a row.
Chapter 10
Design Patterns for Data Structures10.1 The Left-Leaning Red-Black Tree 387
// ========= removeMin =========// Pre: h != nullptr.// Post: The minimum value is removed from root node h.// Post: A pointer to the root node of the modified tree is returned.template<class T>Node<T> *Node<T>::removeMin(Node<T> *h) {
if (h == nullptr) {cerr << ”removeMin precondition violated: h == nullptr” << endl;throw -1;
}if (h->_left == nullptr) {
delete h;return nullptr;
}if (!isRed(h->_left) && !isRed(h->_left->_left)) {
h = moveRedLeft(h);}h->_left = removeMin(h->_left);return fixup(h);
}
Figure 10.17 The remove minimum operation for the LLRBTree data structure.
“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?
Revised: May 2, 2018 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
LLRBTree
Guaranteed to have red link from parent.
Design Patterns for Data Structures10.1 The Left-Leaning Red-Black Tree 387
// ========= removeMin =========// Pre: h != nullptr.// Post: The minimum value is removed from root node h.// Post: A pointer to the root node of the modified tree is returned.template<class T>Node<T> *Node<T>::removeMin(Node<T> *h) {
if (h == nullptr) {cerr << ”removeMin precondition violated: h == nullptr” << endl;throw -1;
}if (h->_left == nullptr) {
delete h;return nullptr;
}if (!isRed(h->_left) && !isRed(h->_left->_left)) {
h = moveRedLeft(h);}h->_left = removeMin(h->_left);return fixup(h);
}
Figure 10.17 The remove minimum operation for the LLRBTree data structure.
“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?
Revised: May 2, 2018 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
LLRBTree
Called on the way down if two left black links in a row
Design Patterns for Data Structures10.1 The Left-Leaning Red-Black Tree 387
// ========= removeMin =========// Pre: h != nullptr.// Post: The minimum value is removed from root node h.// Post: A pointer to the root node of the modified tree is returned.template<class T>Node<T> *Node<T>::removeMin(Node<T> *h) {
if (h == nullptr) {cerr << ”removeMin precondition violated: h == nullptr” << endl;throw -1;
}if (h->_left == nullptr) {
delete h;return nullptr;
}if (!isRed(h->_left) && !isRed(h->_left->_left)) {
h = moveRedLeft(h);}h->_left = removeMin(h->_left);return fixup(h);
}
Figure 10.17 The remove minimum operation for the LLRBTree data structure.
“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?
Revised: May 2, 2018 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
LLRBTree
Recursively move down the left to find the minimum.
Design Patterns for Data Structures10.1 The Left-Leaning Red-Black Tree 387
// ========= removeMin =========// Pre: h != nullptr.// Post: The minimum value is removed from root node h.// Post: A pointer to the root node of the modified tree is returned.template<class T>Node<T> *Node<T>::removeMin(Node<T> *h) {
if (h == nullptr) {cerr << ”removeMin precondition violated: h == nullptr” << endl;throw -1;
}if (h->_left == nullptr) {
delete h;return nullptr;
}if (!isRed(h->_left) && !isRed(h->_left->_left)) {
h = moveRedLeft(h);}h->_left = removeMin(h->_left);return fixup(h);
}
Figure 10.17 The remove minimum operation for the LLRBTree data structure.
“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?
Revised: May 2, 2018 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
LLRBTree
Same fixup as with insert!
Design Patterns for Data Structures386 Chapter 10 Balanced Trees
// ========= moveRedLeft =========// Pre: Both h->_left and h->_left->_left are BLACK.// Post: Either h->_left or one of its children is RED.// Post: A pointer to the root node of the modified tree is returned.template<class T>Node<T> *Node<T>::moveRedLeft(Node<T> *h) {
if (isRed(h->_left) || isRed(h->_left->_left)) {cerr << ”moveRedLeft precondition violated: h->_left is RED or ...throw -1;
}flipColors(h);if (isRed(h->_right->_left)) {
h->_right = rotateRight(h->_right);h = rotateLeft(h);flipColors(h);
}return h;
}
Figure 10.16 The move red left operation for the LLRBTree data structure.
Removing the minimumHello, here is some text without a meaning. This text should show what a printed textwill look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match the
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: May 2, 2018
LLRBTree
Move red left might only flip the colors.
Design Patterns for Data Structures386 Chapter 10 Balanced Trees
// ========= moveRedLeft =========// Pre: Both h->_left and h->_left->_left are BLACK.// Post: Either h->_left or one of its children is RED.// Post: A pointer to the root node of the modified tree is returned.template<class T>Node<T> *Node<T>::moveRedLeft(Node<T> *h) {
if (isRed(h->_left) || isRed(h->_left->_left)) {cerr << ”moveRedLeft precondition violated: h->_left is RED or ...throw -1;
}flipColors(h);if (isRed(h->_right->_left)) {
h->_right = rotateRight(h->_right);h = rotateLeft(h);flipColors(h);
}return h;
}
Figure 10.16 The move red left operation for the LLRBTree data structure.
Removing the minimumHello, here is some text without a meaning. This text should show what a printed textwill look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match the
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: May 2, 2018
LLRBTree
This condition could not be repaired by fixup on the way back up.
Design Patterns for Data Structures LLRBTree
388 Chapter 10 Balanced Trees
40
20
80
60
90
h
10 30 50 70l r
40
20
80
60
90
h
10 30 50 70l r
40
20
80
60
90
h
10 30 50 70l r
(a) Initial tree. (b) moveRedLeft() only flips colors.
(c) moveRedLeft() only flips colors.
h 40
20
80
60
90
h
10 30 50 70l r
(d) moveRedLeft() only flips colors.
h
h
40
20
80
60
90
h
30 50 70l r
(e) Delete minimum with red parent link.
h
h
40
20
80
60
90
h
30
50 70l r
(f) Fixup with rotateLeft().
h
Figure 10.18 Deleting the minimum from a left-leaning red-black tree in the casewhere the move red left operation only flips the colors. The sequence continues in thefollowing figure.
text will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. This
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: November 19, 2016
Deleting minimum. Move red left only flips colors.
Design Patterns for Data Structures LLRBTree
Deleting minimum. Move red left only flips colors.
388 Chapter 10 Balanced Trees
40
20
80
60
90
h
10 30 50 70l r
40
20
80
60
90
h
10 30 50 70l r
40
20
80
60
90
h
10 30 50 70l r
(a) Initial tree. (b) moveRedLeft() only flips colors.
(c) moveRedLeft() only flips colors.
h 40
20
80
60
90
h
10 30 50 70l r
(d) moveRedLeft() only flips colors.
h
h
40
20
80
60
90
h
30 50 70l r
(e) Delete minimum with red parent link.
h
h
40
20
80
60
90
h
30
50 70l r
(f) Fixup with rotateLeft().
h
Figure 10.18 Deleting the minimum from a left-leaning red-black tree in the casewhere the move red left operation only flips the colors. The sequence continues in thefollowing figure.
text will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. This
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: November 19, 2016
Design Patterns for Data Structures LLRBTree
Deleting minimum. Move red left only flips colors.
388 Chapter 10 Balanced Trees
40
20
80
60
90
h
10 30 50 70l r
40
20
80
60
90
h
10 30 50 70l r
40
20
80
60
90
h
10 30 50 70l r
(a) Initial tree. (b) moveRedLeft() only flips colors.
(c) moveRedLeft() only flips colors.
h 40
20
80
60
90
h
10 30 50 70l r
(d) moveRedLeft() only flips colors.
h
h
40
20
80
60
90
h
30 50 70l r
(e) Delete minimum with red parent link.
h
h
40
20
80
60
90
h
30
50 70l r
(f) Fixup with rotateLeft().
h
Figure 10.18 Deleting the minimum from a left-leaning red-black tree in the casewhere the move red left operation only flips the colors. The sequence continues in thefollowing figure.
text will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. This
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: November 19, 2016
Design Patterns for Data Structures LLRBTree
Deleting minimum. Move red left only flips colors.10.1 The Left-Leaning Red-Black Tree 389
40
20
80
60 90
h
30 50
70l r
(g) Fixup with rotateLeft().
40
20
80
60
90
30 50
70l
r
(h) Fixup with rotateLeft().
Figure 10.19 (continued) Deleting the minimum from a left-leaning red-black treein the case where the move red left operation only flips the colors.
text should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the original
Revised: November 19, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
Design Patterns for Data Structures LLRBTree
Deleting minimum.Move red left does more than flipping colors.390 Chapter 10 Balanced Trees
40
20
80
60
90
h
10 30 50l r
40
20
80
60
90
h
10 30 50l r
40
20
80
60
90
h
10 30 50l r
(a) Initial tree. (b) moveRedLeft() only flips colors.
(c) First, moveRedLeft() flips colors.
h
(d) h->_right = rotateRight(h->_right).
40
20
80
60
90
h
10 30
50l r
h
(e) h = rotateLeft(h).
40
20
80
60
90
h
10 30
50
l r
h
(f) h = flipColors(h).
40
20
80
60
90
h
10 30
50
l r
h
70
45 55
70
45 55
70
45 55 7055
45
45 7055 45 7055
Figure 10.20 Deleting the minimum from a left-leaning red-black tree in the casewhere the move red left operation does more than flipping the colors. The sequencecontinues in the following figure.
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: November 20, 2016
Design Patterns for Data Structures LLRBTree
Deleting minimum.Move red left does more than flipping colors.
390 Chapter 10 Balanced Trees
40
20
80
60
90
h
10 30 50l r
40
20
80
60
90
h
10 30 50l r
40
20
80
60
90
h
10 30 50l r
(a) Initial tree. (b) moveRedLeft() only flips colors.
(c) First, moveRedLeft() flips colors.
h
(d) h->_right = rotateRight(h->_right).
40
20
80
60
90
h
10 30
50l r
h
(e) h = rotateLeft(h).
40
20
80
60
90
h
10 30
50
l r
h
(f) h = flipColors(h).
40
20
80
60
90
h
10 30
50
l r
h
70
45 55
70
45 55
70
45 55 7055
45
45 7055 45 7055
Figure 10.20 Deleting the minimum from a left-leaning red-black tree in the casewhere the move red left operation does more than flipping the colors. The sequencecontinues in the following figure.
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: November 20, 2016
Design Patterns for Data Structures LLRBTree
Deleting minimum.Move red left does more than flipping colors.
390 Chapter 10 Balanced Trees
40
20
80
60
90
h
10 30 50l r
40
20
80
60
90
h
10 30 50l r
40
20
80
60
90
h
10 30 50l r
(a) Initial tree. (b) moveRedLeft() only flips colors.
(c) First, moveRedLeft() flips colors.
h
(d) h->_right = rotateRight(h->_right).
40
20
80
60
90
h
10 30
50l r
h
(e) h = rotateLeft(h).
40
20
80
60
90
h
10 30
50
l r
h
(f) h = flipColors(h).
40
20
80
60
90
h
10 30
50
l r
h
70
45 55
70
45 55
70
45 55 7055
45
45 7055 45 7055
Figure 10.20 Deleting the minimum from a left-leaning red-black tree in the casewhere the move red left operation does more than flipping the colors. The sequencecontinues in the following figure.
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: November 20, 2016
Design Patterns for Data Structures LLRBTree
Deleting minimum.Move red left does more than flipping colors.
10.1 The Left-Leaning Red-Black Tree 391
(g) moveRedLeft() is not called. (h) moveRedLeft() only flips colors.
(i) Delete minimum with red parent link.
20
(j) After four fixups.
40
20
80
60
90
h
10 30
50
l r
h
45 7055
h 40
20
80
60
90
h
10 30
50
l r
h
45 7055
h
h
40
20
80
60
90
h
30
50
l r
h
45 7055
h
h
40
80
60
90
30
50
l r45 7055
Figure 10.21 (continued) Deleting the minimum from a left-leaning red-black treein the case where the move red left operation does more than flipping the colors.
language. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?
Revised: November 20, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
Design Patterns for Data Structures LLRBTree
Deleting minimum.Move red left does more than flipping colors.
10.1 The Left-Leaning Red-Black Tree 391
(g) moveRedLeft() is not called. (h) moveRedLeft() only flips colors.
(i) Delete minimum with red parent link.
20
(j) After four fixups.
40
20
80
60
90
h
10 30
50
l r
h
45 7055
h 40
20
80
60
90
h
10 30
50
l r
h
45 7055
h
h
40
20
80
60
90
h
30
50
l r
h
45 7055
h
h
40
80
60
90
30
50
l r45 7055
Figure 10.21 (continued) Deleting the minimum from a left-leaning red-black treein the case where the move red left operation does more than flipping the colors.
language. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?
Revised: November 20, 2016 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
Design Patterns for Data Structures
The remove operation
• Find the node to remove.
• Find the minimum of the right child of the node to remove, which is the successor node.
• Copy the data from the successor to the node to remove with the min() operation.
• Remove the minimum of the right child.
Chapter 10
Design Patterns for Data Structures LLRBTree
Used to copy the data from the successor node.10.1 The Left-Leaning Red-Black Tree 393
// ========= min =========// Pre: h != nullptr.// Post: The minimum of tree rooted at h is returned.template<class T>T Node<T>::min(Node<T> *h) {
cerr << ”min(): Exercise for the student.” << endl;throw -1;
}
Figure 10.23 The min operation for the LLRBTree data structure.
text should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
RemovalHello, here is some text without a meaning. This text should show what a printed textwill look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. This
Revised: May 2, 2018 Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford
Design Patterns for Data Structures
The remove operation
• Find the node to remove.
• Find the minimum of the right child of the node to remove, which is the successor node.
• Copy the data from the successor to the node to remove with the min() operation.
• Remove the minimum of the right child.
Chapter 10
Design Patterns for Data Structures
Find the node to remove
• Same strategy: need to push the red links down.
• Finding the node to remove may require moving to the right as well as to the left.
• If you move to the left on the way to the node to remove, call moveRedLeft() as before.
• If you move to the right, call moveRedRight().
Chapter 10
Design Patterns for Data Structures
392 Chapter 10 Balanced Trees
// ========= moveRedRight =========// Pre: Both h->_right and h->_right->_left are BLACK.// Post: Either h->_right or one of its children is RED.// Post: A pointer to the root node of the modified tree is returned.template<class T>Node<T> *Node<T>::moveRedRight(Node<T> *h) {
if (isRed(h->_right) || isRed(h->_right->_left)) {cerr << ”moveRedRight precondition violated: h->_right is RED or ...throw -1;
}flipColors(h);if (isRed(h->_left->_left)) {
h = rotateRight(h);flipColors(h);
}return h;
}
Figure 10.22 The move red right operation for the LLRBTree data structure.
language.Hello, here is some text without a meaning. This text should show what a printed
text will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printed
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: May 2, 2018
LLRBTree
Move red right might only flip the colors.
Design Patterns for Data Structures
392 Chapter 10 Balanced Trees
// ========= moveRedRight =========// Pre: Both h->_right and h->_right->_left are BLACK.// Post: Either h->_right or one of its children is RED.// Post: A pointer to the root node of the modified tree is returned.template<class T>Node<T> *Node<T>::moveRedRight(Node<T> *h) {
if (isRed(h->_right) || isRed(h->_right->_left)) {cerr << ”moveRedRight precondition violated: h->_right is RED or ...throw -1;
}flipColors(h);if (isRed(h->_left->_left)) {
h = rotateRight(h);flipColors(h);
}return h;
}
Figure 10.22 The move red right operation for the LLRBTree data structure.
language.Hello, here is some text without a meaning. This text should show what a printed
text will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printedtext will look like at this place. If you read this text, you will get no information. Really?Is there no information? Is there a difference between this text and some nonsense like“Huardest gefburn”? Kjift – not at all! A blind text like this gives you informationabout the selected font, how the letters are written and an impression of the look. Thistext should contain all letters of the alphabet and it should be written in of the originallanguage. There is no need for special content, but the length of words should match thelanguage.
Hello, here is some text without a meaning. This text should show what a printed
Copyright ©: 1998, Dung X. Nguyen and J. Stanley Warford Revised: May 2, 2018
LLRBTree
This condition could not be repaired by fixup on the way back up.
Design Patterns for Data Structures LLRBTree
The remove operation��� &KDSWHU��� %DODQFHG�7UHHV
�� UHPRYH �� 3RVW� ,I GDWD LV IRXQG� WKHQ LW LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!YRLG //5%7UHH�7!��UHPRYH�7 FRQVW GDWD� ^
LI ��FRQWDLQV�GDWD�� ^UHWXUQ�
`BURRW BURRW�!UHPRYH�BURRW� GDWD��LI �BURRW � QXOOSWU� ^
BURRW�!BFRORU %/$&.�`
`
�� 3RVW� GDWD LV UHPRYHG IURP URRW QRGH K��� 3RVW� $ SRLQWHU WR WKH URRW QRGH RI WKH PRGLILHG WUHH LV UHWXUQHG�WHPSODWH�FODVV 7!1RGH�7! 1RGH�7!��UHPRYH�1RGH�7! K� 7 FRQVW GDWD� ^
LI �GDWD � K�!BGDWD� ^LI ��LV5HG�K�!BOHIW� �LV5HG�K�!BOHIW�!BOHIW�� ^
K PRYH5HG/HIW�K��`K�!BOHIW UHPRYH�K�!BOHIW� GDWD��
`HOVH ^
LI �LV5HG�K�!BOHIW�� ^K URWDWH5LJKW�K��
`LI ��GDWD K�!BGDWD� �K�!BULJKW QXOOSWU�� ^
GHOHWH K�UHWXUQ QXOOSWU�
`LI ��LV5HG�K�!BULJKW� �LV5HG�K�!BULJKW�!BOHIW�� ^
K PRYH5HG5LJKW�K��`LI �GDWD K�!BGDWD� ^
K�!BGDWD PLQ�K�!BULJKW��K�!BULJKW UHPRYH0LQ�K�!BULJKW��
`HOVH ^
K�!BULJKW UHPRYH�K�!BULJKW� GDWD��`
`UHWXUQ IL[XS�K��
`
)LJXUH ����� 7KH�UHPRYH�RSHUDWLRQ�IRU�WKH //5%7UHH GDWD�VWUXFWXUH�
&RS\ULJKW��� ����� 'XQJ�;��1JX\HQ�DQG�-��6WDQOH\�:DUIRUG 5HYLVHG� 'HFHPEHU���� ����
��� &KDSWHU��� %DODQFHG�7UHHV
�� UHPRYH �� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!YRLG //5%7UHH�7!��UHPRYH�7 FRQVW GDWD� ^
BURRW BURRW�!UHPRYH�BURRW� GDWD��LI �BURRW � QXOOSWU� ^
BURRW�!BFRORU %/$&.�`
`
�� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!1RGH�7! 1RGH�7!��UHPRYH�1RGH�7! K� 7 FRQVW GDWD� ^
LI �GDWD � K�!BGDWD� ^LI ��LV5HG�K�!BOHIW� �LV5HG�K�!BOHIW�!BOHIW�� ^
K PRYH5HG/HIW�K��`K�!BOHIW UHPRYH�K�!BOHIW� GDWD��
`HOVH ^
LI �LV5HG�K�!BOHIW�� ^K URWDWH5LJKW�K��
`LI ��GDWD K�!BGDWD� �K�!BULJKW QXOOSWU�� ^
GHOHWH K�UHWXUQ QXOOSWU�
`LI ��LV5HG�K�!BULJKW� �LV5HG�K�!BULJKW�!BOHIW�� ^
K PRYH5HG5LJKW�K��`LI �GDWD K�!BGDWD� ^
K�!BGDWD PLQ�K�!BULJKW��K�!BULJKW UHPRYH0LQ�K�!BULJKW��
`HOVH ^
K�!BULJKW UHPRYH�K�!BULJKW� GDWD��`
`UHWXUQ IL[XS�K��
`
)LJXUH ����� 7KH�UHPRYH�RSHUDWLRQ�IRU�WKH //5%7UHH GDWD�VWUXFWXUH�
&RS\ULJKW��� ����� 'XQJ�;��1JX\HQ�DQG�-��6WDQOH\�:DUIRUG 5HYLVHG� 1RYHPEHU���� ����
Move down to the left.
��� &KDSWHU��� %DODQFHG�7UHHV
�� UHPRYH �� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!YRLG //5%7UHH�7!��UHPRYH�7 FRQVW GDWD� ^
BURRW BURRW�!UHPRYH�BURRW� GDWD��LI �BURRW � QXOOSWU� ^
BURRW�!BFRORU %/$&.�`
`
�� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!1RGH�7! 1RGH�7!��UHPRYH�1RGH�7! K� 7 FRQVW GDWD� ^
LI �GDWD � K�!BGDWD� ^LI ��LV5HG�K�!BOHIW� �LV5HG�K�!BOHIW�!BOHIW�� ^
K PRYH5HG/HIW�K��`K�!BOHIW UHPRYH�K�!BOHIW� GDWD��
`HOVH ^
LI �LV5HG�K�!BOHIW�� ^K URWDWH5LJKW�K��
`LI ��GDWD K�!BGDWD� �K�!BULJKW QXOOSWU�� ^
GHOHWH K�UHWXUQ QXOOSWU�
`LI ��LV5HG�K�!BULJKW� �LV5HG�K�!BULJKW�!BOHIW�� ^
K PRYH5HG5LJKW�K��`LI �GDWD K�!BGDWD� ^
K�!BGDWD PLQ�K�!BULJKW��K�!BULJKW UHPRYH0LQ�K�!BULJKW��
`HOVH ^
K�!BULJKW UHPRYH�K�!BULJKW� GDWD��`
`UHWXUQ IL[XS�K��
`
)LJXUH ����� 7KH�UHPRYH�RSHUDWLRQ�IRU�WKH //5%7UHH GDWD�VWUXFWXUH�
&RS\ULJKW��� ����� 'XQJ�;��1JX\HQ�DQG�-��6WDQOH\�:DUIRUG 5HYLVHG� 1RYHPEHU���� ����
Found, or move down to the right.
��� &KDSWHU��� %DODQFHG�7UHHV
�� UHPRYH �� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!YRLG //5%7UHH�7!��UHPRYH�7 FRQVW GDWD� ^
BURRW BURRW�!UHPRYH�BURRW� GDWD��LI �BURRW � QXOOSWU� ^
BURRW�!BFRORU %/$&.�`
`
�� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!1RGH�7! 1RGH�7!��UHPRYH�1RGH�7! K� 7 FRQVW GDWD� ^
LI �GDWD � K�!BGDWD� ^LI ��LV5HG�K�!BOHIW� �LV5HG�K�!BOHIW�!BOHIW�� ^
K PRYH5HG/HIW�K��`K�!BOHIW UHPRYH�K�!BOHIW� GDWD��
`HOVH ^
LI �LV5HG�K�!BOHIW�� ^K URWDWH5LJKW�K��
`LI ��GDWD K�!BGDWD� �K�!BULJKW QXOOSWU�� ^
GHOHWH K�UHWXUQ QXOOSWU�
`LI ��LV5HG�K�!BULJKW� �LV5HG�K�!BULJKW�!BOHIW�� ^
K PRYH5HG5LJKW�K��`LI �GDWD K�!BGDWD� ^
K�!BGDWD PLQ�K�!BULJKW��K�!BULJKW UHPRYH0LQ�K�!BULJKW��
`HOVH ^
K�!BULJKW UHPRYH�K�!BULJKW� GDWD��`
`UHWXUQ IL[XS�K��
`
)LJXUH ����� 7KH�UHPRYH�RSHUDWLRQ�IRU�WKH //5%7UHH GDWD�VWUXFWXUH�
&RS\ULJKW��� ����� 'XQJ�;��1JX\HQ�DQG�-��6WDQOH\�:DUIRUG 5HYLVHG� 1RYHPEHU���� ����
Adjust left red link to right.
��� &KDSWHU��� %DODQFHG�7UHHV
�� UHPRYH �� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!YRLG //5%7UHH�7!��UHPRYH�7 FRQVW GDWD� ^
BURRW BURRW�!UHPRYH�BURRW� GDWD��LI �BURRW � QXOOSWU� ^
BURRW�!BFRORU %/$&.�`
`
�� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!1RGH�7! 1RGH�7!��UHPRYH�1RGH�7! K� 7 FRQVW GDWD� ^
LI �GDWD � K�!BGDWD� ^LI ��LV5HG�K�!BOHIW� �LV5HG�K�!BOHIW�!BOHIW�� ^
K PRYH5HG/HIW�K��`K�!BOHIW UHPRYH�K�!BOHIW� GDWD��
`HOVH ^
LI �LV5HG�K�!BOHIW�� ^K URWDWH5LJKW�K��
`LI ��GDWD K�!BGDWD� �K�!BULJKW QXOOSWU�� ^
GHOHWH K�UHWXUQ QXOOSWU�
`LI ��LV5HG�K�!BULJKW� �LV5HG�K�!BULJKW�!BOHIW�� ^
K PRYH5HG5LJKW�K��`LI �GDWD K�!BGDWD� ^
K�!BGDWD PLQ�K�!BULJKW��K�!BULJKW UHPRYH0LQ�K�!BULJKW��
`HOVH ^
K�!BULJKW UHPRYH�K�!BULJKW� GDWD��`
`UHWXUQ IL[XS�K��
`
)LJXUH ����� 7KH�UHPRYH�RSHUDWLRQ�IRU�WKH //5%7UHH GDWD�VWUXFWXUH�
&RS\ULJKW��� ����� 'XQJ�;��1JX\HQ�DQG�-��6WDQOH\�:DUIRUG 5HYLVHG� 1RYHPEHU���� ����
Found and can return immediately.
��� &KDSWHU��� %DODQFHG�7UHHV
�� UHPRYH �� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!YRLG //5%7UHH�7!��UHPRYH�7 FRQVW GDWD� ^
BURRW BURRW�!UHPRYH�BURRW� GDWD��LI �BURRW � QXOOSWU� ^
BURRW�!BFRORU %/$&.�`
`
�� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!1RGH�7! 1RGH�7!��UHPRYH�1RGH�7! K� 7 FRQVW GDWD� ^
LI �GDWD � K�!BGDWD� ^LI ��LV5HG�K�!BOHIW� �LV5HG�K�!BOHIW�!BOHIW�� ^
K PRYH5HG/HIW�K��`K�!BOHIW UHPRYH�K�!BOHIW� GDWD��
`HOVH ^
LI �LV5HG�K�!BOHIW�� ^K URWDWH5LJKW�K��
`LI ��GDWD K�!BGDWD� �K�!BULJKW QXOOSWU�� ^
GHOHWH K�UHWXUQ QXOOSWU�
`LI ��LV5HG�K�!BULJKW� �LV5HG�K�!BULJKW�!BOHIW�� ^
K PRYH5HG5LJKW�K��`LI �GDWD K�!BGDWD� ^
K�!BGDWD PLQ�K�!BULJKW��K�!BULJKW UHPRYH0LQ�K�!BULJKW��
`HOVH ^
K�!BULJKW UHPRYH�K�!BULJKW� GDWD��`
`UHWXUQ IL[XS�K��
`
)LJXUH ����� 7KH�UHPRYH�RSHUDWLRQ�IRU�WKH //5%7UHH GDWD�VWUXFWXUH�
&RS\ULJKW��� ����� 'XQJ�;��1JX\HQ�DQG�-��6WDQOH\�:DUIRUG 5HYLVHG� 1RYHPEHU���� ����
Possible adjustment with moveRedRight.
��� &KDSWHU��� %DODQFHG�7UHHV
�� UHPRYH �� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!YRLG //5%7UHH�7!��UHPRYH�7 FRQVW GDWD� ^
BURRW BURRW�!UHPRYH�BURRW� GDWD��LI �BURRW � QXOOSWU� ^
BURRW�!BFRORU %/$&.�`
`
�� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!1RGH�7! 1RGH�7!��UHPRYH�1RGH�7! K� 7 FRQVW GDWD� ^
LI �GDWD � K�!BGDWD� ^LI ��LV5HG�K�!BOHIW� �LV5HG�K�!BOHIW�!BOHIW�� ^
K PRYH5HG/HIW�K��`K�!BOHIW UHPRYH�K�!BOHIW� GDWD��
`HOVH ^
LI �LV5HG�K�!BOHIW�� ^K URWDWH5LJKW�K��
`LI ��GDWD K�!BGDWD� �K�!BULJKW QXOOSWU�� ^
GHOHWH K�UHWXUQ QXOOSWU�
`LI ��LV5HG�K�!BULJKW� �LV5HG�K�!BULJKW�!BOHIW�� ^
K PRYH5HG5LJKW�K��`LI �GDWD K�!BGDWD� ^
K�!BGDWD PLQ�K�!BULJKW��K�!BULJKW UHPRYH0LQ�K�!BULJKW��
`HOVH ^
K�!BULJKW UHPRYH�K�!BULJKW� GDWD��`
`UHWXUQ IL[XS�K��
`
)LJXUH ����� 7KH�UHPRYH�RSHUDWLRQ�IRU�WKH //5%7UHH GDWD�VWUXFWXUH�
&RS\ULJKW��� ����� 'XQJ�;��1JX\HQ�DQG�-��6WDQOH\�:DUIRUG 5HYLVHG� 1RYHPEHU���� ����
Found the node to remove.
��� &KDSWHU��� %DODQFHG�7UHHV
�� UHPRYH �� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!YRLG //5%7UHH�7!��UHPRYH�7 FRQVW GDWD� ^
BURRW BURRW�!UHPRYH�BURRW� GDWD��LI �BURRW � QXOOSWU� ^
BURRW�!BFRORU %/$&.�`
`
�� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!1RGH�7! 1RGH�7!��UHPRYH�1RGH�7! K� 7 FRQVW GDWD� ^
LI �GDWD � K�!BGDWD� ^LI ��LV5HG�K�!BOHIW� �LV5HG�K�!BOHIW�!BOHIW�� ^
K PRYH5HG/HIW�K��`K�!BOHIW UHPRYH�K�!BOHIW� GDWD��
`HOVH ^
LI �LV5HG�K�!BOHIW�� ^K URWDWH5LJKW�K��
`LI ��GDWD K�!BGDWD� �K�!BULJKW QXOOSWU�� ^
GHOHWH K�UHWXUQ QXOOSWU�
`LI ��LV5HG�K�!BULJKW� �LV5HG�K�!BULJKW�!BOHIW�� ^
K PRYH5HG5LJKW�K��`LI �GDWD K�!BGDWD� ^
K�!BGDWD PLQ�K�!BULJKW��K�!BULJKW UHPRYH0LQ�K�!BULJKW��
`HOVH ^
K�!BULJKW UHPRYH�K�!BULJKW� GDWD��`
`UHWXUQ IL[XS�K��
`
)LJXUH ����� 7KH�UHPRYH�RSHUDWLRQ�IRU�WKH //5%7UHH GDWD�VWUXFWXUH�
&RS\ULJKW��� ����� 'XQJ�;��1JX\HQ�DQG�-��6WDQOH\�:DUIRUG 5HYLVHG� 1RYHPEHU���� ����
Copy data from successor node.
��� &KDSWHU��� %DODQFHG�7UHHV
�� UHPRYH �� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!YRLG //5%7UHH�7!��UHPRYH�7 FRQVW GDWD� ^
BURRW BURRW�!UHPRYH�BURRW� GDWD��LI �BURRW � QXOOSWU� ^
BURRW�!BFRORU %/$&.�`
`
�� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!1RGH�7! 1RGH�7!��UHPRYH�1RGH�7! K� 7 FRQVW GDWD� ^
LI �GDWD � K�!BGDWD� ^LI ��LV5HG�K�!BOHIW� �LV5HG�K�!BOHIW�!BOHIW�� ^
K PRYH5HG/HIW�K��`K�!BOHIW UHPRYH�K�!BOHIW� GDWD��
`HOVH ^
LI �LV5HG�K�!BOHIW�� ^K URWDWH5LJKW�K��
`LI ��GDWD K�!BGDWD� �K�!BULJKW QXOOSWU�� ^
GHOHWH K�UHWXUQ QXOOSWU�
`LI ��LV5HG�K�!BULJKW� �LV5HG�K�!BULJKW�!BOHIW�� ^
K PRYH5HG5LJKW�K��`LI �GDWD K�!BGDWD� ^
K�!BGDWD PLQ�K�!BULJKW��K�!BULJKW UHPRYH0LQ�K�!BULJKW��
`HOVH ^
K�!BULJKW UHPRYH�K�!BULJKW� GDWD��`
`UHWXUQ IL[XS�K��
`
)LJXUH ����� 7KH�UHPRYH�RSHUDWLRQ�IRU�WKH //5%7UHH GDWD�VWUXFWXUH�
&RS\ULJKW��� ����� 'XQJ�;��1JX\HQ�DQG�-��6WDQOH\�:DUIRUG 5HYLVHG� 1RYHPEHU���� ����
Delete successor node.
��� &KDSWHU��� %DODQFHG�7UHHV
�� UHPRYH �� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!YRLG //5%7UHH�7!��UHPRYH�7 FRQVW GDWD� ^
BURRW BURRW�!UHPRYH�BURRW� GDWD��LI �BURRW � QXOOSWU� ^
BURRW�!BFRORU %/$&.�`
`
�� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!1RGH�7! 1RGH�7!��UHPRYH�1RGH�7! K� 7 FRQVW GDWD� ^
LI �GDWD � K�!BGDWD� ^LI ��LV5HG�K�!BOHIW� �LV5HG�K�!BOHIW�!BOHIW�� ^
K PRYH5HG/HIW�K��`K�!BOHIW UHPRYH�K�!BOHIW� GDWD��
`HOVH ^
LI �LV5HG�K�!BOHIW�� ^K URWDWH5LJKW�K��
`LI ��GDWD K�!BGDWD� �K�!BULJKW QXOOSWU�� ^
GHOHWH K�UHWXUQ QXOOSWU�
`LI ��LV5HG�K�!BULJKW� �LV5HG�K�!BULJKW�!BOHIW�� ^
K PRYH5HG5LJKW�K��`LI �GDWD K�!BGDWD� ^
K�!BGDWD PLQ�K�!BULJKW��K�!BULJKW UHPRYH0LQ�K�!BULJKW��
`HOVH ^
K�!BULJKW UHPRYH�K�!BULJKW� GDWD��`
`UHWXUQ IL[XS�K��
`
)LJXUH ����� 7KH�UHPRYH�RSHUDWLRQ�IRU�WKH //5%7UHH GDWD�VWUXFWXUH�
&RS\ULJKW��� ����� 'XQJ�;��1JX\HQ�DQG�-��6WDQOH\�:DUIRUG 5HYLVHG� 1RYHPEHU���� ����
Move down to the right.
��� &KDSWHU��� %DODQFHG�7UHHV
�� UHPRYH �� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!YRLG //5%7UHH�7!��UHPRYH�7 FRQVW GDWD� ^
BURRW BURRW�!UHPRYH�BURRW� GDWD��LI �BURRW � QXOOSWU� ^
BURRW�!BFRORU %/$&.�`
`
�� 3UH� 7KLV WUHH FRQWDLQV GDWD��� 3RVW� GDWD LV UHPRYHG IURP WKLV WUHH�WHPSODWH�FODVV 7!1RGH�7! 1RGH�7!��UHPRYH�1RGH�7! K� 7 FRQVW GDWD� ^
LI �GDWD � K�!BGDWD� ^LI ��LV5HG�K�!BOHIW� �LV5HG�K�!BOHIW�!BOHIW�� ^
K PRYH5HG/HIW�K��`K�!BOHIW UHPRYH�K�!BOHIW� GDWD��
`HOVH ^
LI �LV5HG�K�!BOHIW�� ^K URWDWH5LJKW�K��
`LI ��GDWD K�!BGDWD� �K�!BULJKW QXOOSWU�� ^
GHOHWH K�UHWXUQ QXOOSWU�
`LI ��LV5HG�K�!BULJKW� �LV5HG�K�!BULJKW�!BOHIW�� ^
K PRYH5HG5LJKW�K��`LI �GDWD K�!BGDWD� ^
K�!BGDWD PLQ�K�!BULJKW��K�!BULJKW UHPRYH0LQ�K�!BULJKW��
`HOVH ^
K�!BULJKW UHPRYH�K�!BULJKW� GDWD��`
`UHWXUQ IL[XS�K��
`
)LJXUH ����� 7KH�UHPRYH�RSHUDWLRQ�IRU�WKH //5%7UHH GDWD�VWUXFWXUH�
&RS\ULJKW��� ����� 'XQJ�;��1JX\HQ�DQG�-��6WDQOH\�:DUIRUG 5HYLVHG� 1RYHPEHU���� ����
Design Patterns for Data Structures LLRBTree
Removing 50 from an LLRBTree
h
(b) moveRedLeft(), only flips colors.
60
40 80
30 50 70 90
10
(a) Initial tree.
60
40 80
30 50 70 90
10
h 60
40 80
30 50 70 90
10
(c) moveRedRight(), h = flipColors(h).
h
h 60
40
8030
50
70 9010
(d) rotateRight(h).
h
h 60
40
8030
50
70 9010
h
(e) h = flipColors(h).
h 60
40
8030
50
70 9010
h
(f) remove(), twice.
h
h
h
(b) moveRedLeft(), only flips colors.
60
40 80
30 50 70 90
10
(a) Initial tree.
60
40 80
30 50 70 90
10
h 60
40 80
30 50 70 90
10
(c) moveRedRight(), h = flipColors(h).
h
h 60
40
8030
50
70 9010
(d) rotateRight(h).
h
h 60
40
8030
50
70 9010
h
(e) h = flipColors(h).
h 60
40
8030
50
70 9010
h
(f) remove(), twice.
h
h
(h) Fixup with rotateLeft().(g) delete h.
h 60
40
8030
70 9010
h
h
h
60
40
80
30 70
90
10
h 60
40
8030
70 9010
(i) Fixup with rotateRight().
h 60
40
8030
70 9010
(j) Fixup with flipColors().
LLRBTree
Chapter 11
13-6 Lecture Notes for Chapter 13: Red-Black Trees
2. If ´ is the root, then there’s a violation. Otherwise, OK.3. OK.4. If ´:p is red, there’s a violation: both ´ and ´:p are red.5. OK.
Remove the violation by calling RB-INSERT-FIXUP:
RB-INSERT-FIXUP.T; ´/
while ´:p:color == REDif ´:p == ´:p:p: left
y D ´:p:p:rightif y:color == RED
´:p:color D BLACK // case 1y:color D BLACK // case 1´:p:p:color D RED // case 1´ D ´:p:p // case 1
else if ´ == ´:p:right´ D ´:p // case 2LEFT-ROTATE.T; ´/ // case 2
´:p:color D BLACK // case 3´:p:p:color D RED // case 3RIGHT-ROTATE.T; ´:p:p/ // case 3
else (same as then clause with “right” and “left” exchanged)T:root:color D BLACK
Loop invariant:At the start of each iteration of the while loop,
a. ´ is red.b. There is at most one red-black violation:
! Property 2: ´ is a red root, or! Property 4: ´ and ´:p are both red.
[The book has a third part of the loop invariant, but we omit it for lecture.]
Initialization: We’ve already seen why the loop invariant holds initially.Termination: The loop terminates because ´:p is black. Hence, property 4 is OK.Only property 2 might be violated, and the last line fixes it.
Maintenance: We drop out when ´ is the root (since then ´:p is the sentinel T:nil,which is black). When we start the loop body, the only violation is of property 4.There are 6 cases, 3 of which are symmetric to the other 3. The cases are notmutually exclusive. We’ll consider cases in which ´:p is a left child.Let y be ´’s uncle (´:p’s sibling).
Case 1
Case 2
Case 3
Tuesday, August 7, 12
Insert fixup, general red-black tree
LLRBTreeRemove fixup, general red-black tree
Chapter 11
13-10 Lecture Notes for Chapter 13: Red-Black Trees
! x is either doubly black (if x:color D BLACK) or red & black (if x:color DRED).
! The attribute x:color is still either RED or BLACK. No new values for colorattribute.
! In other words, the extra blackness on a node is by virtue of x pointing to thenode.
Remove the violations by calling RB-DELETE-FIXUP:
RB-DELETE-FIXUP.T; x/
while x ¤ T:root and x:color == BLACKif x == x:p: left
w D x:p:rightif w:color == RED
w:color D BLACK // case 1x:p:color D RED // case 1LEFT-ROTATE.T; x:p/ // case 1w D x:p:right // case 1
if w: left:color == BLACK and w:right:color == BLACKw:color D RED // case 2x D x:p // case 2
else if w:right:color == BLACKw: left:color D BLACK // case 3w:color D RED // case 3RIGHT-ROTATE.T; w/ // case 3w D x:p:right // case 3
w:color D x:p:color // case 4x:p:color D BLACK // case 4w:right:color D BLACK // case 4LEFT-ROTATE.T; x:p/ // case 4x D T:root // case 4
else (same as then clause with “right” and “left” exchanged)x:color D BLACK
IdeaMove the extra black up the tree until! x points to a red & black node ) turn it into a black node,! x points to the root ) just remove the extra black, or! we can do certain rotations and recolorings and finish.
Within the while loop:! x always points to a nonroot doubly black node.! w is x’s sibling.! w cannot be T:nil, since that would violate property 5 at x:p.
There are 8 cases, 4 of which are symmetric to the other 4. As with insertion, thecases are not mutually exclusive. We’ll look at cases in which x is a left child.
Case 1
Case 2
Case 3
Case 4
Tuesday, August 7, 12