chapter 15 - school of computing and information...
TRANSCRIPT
![Page 1: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/1.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 147
Chapter 15
Stacks and Queues
![Page 2: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/2.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 148
How the stack routines work: empty stack, push(A) , push(B) , pop
tos (0)
tos (1)
tos (0)
tos (-1)
A A A
B
![Page 3: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/3.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 149
Basic array implementation of the queue
front
front
front
front
front
back
back
back
back
back
A
A B
B
size = 0
size = 1
size = 2
size = 1
size = 0
makeEmpty( )
enqueue(A)
enqueue(B)
dequeue( )
dequeue( )
![Page 4: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/4.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 150
Array implementation of the queue with wraparound
front
front
front
front
front
back
back
back
back
back
F
F
E
size = 3
size = 4
size = 3
size = 2
size = 1
After 3 enqueues
enqueue(F)
dequeue( )
dequeue( )
dequeue( )
F
F
D E
C D E
C D E
![Page 5: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/5.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 151
Linked list implementation of the stack
ABCD
topOfStack
![Page 6: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/6.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 152
Linked list implementation of the queue
A B C D
front back
![Page 7: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/7.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 153
enqueue operation for linked-list-based implementation
back
X
back
...
...
Before
After
![Page 8: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/8.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 154
Chapter 16
Linked Lists
![Page 9: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/9.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 155
Basic linked list
A B C D
frontOfList
![Page 10: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/10.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 156
Insertion into a linked list: create new node (tmp ), copy in x , set tmp ’s next reference, set current ’s next reference
current
... ...
x
A B
tmp
![Page 11: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/11.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 157
Deletion from a linked list
current
... ...XA B
![Page 12: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/12.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 158
Using a header node for the linked list
A B C
header
![Page 13: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/13.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 159
Empty list when header node is used
header
![Page 14: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/14.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 160
Doubly linked list
head tail
A B
![Page 15: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/15.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 161
Empty doubly linked list
head tail
![Page 16: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/16.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 162
Insertion into a doubly linked list by getting new node and then changing references in order indicated
... ...A B
Xa bc d
![Page 17: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/17.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 163
Circular doubly linked list
first
A B C D
![Page 18: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/18.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 164
Chapter 17
Trees
![Page 19: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/19.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 165
A tree
A
B C D E
F G H I J
K
![Page 20: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/20.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 166
Tree viewed recursively
...
root
T1 T2 T3 Tk
![Page 21: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/21.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 167
First child/next sibling representation of tree in Figure 17.1
A
B C D E
F G H I J
K
![Page 22: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/22.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 168
Unix directory
mark*
books* courses*
ecp*dsaa* ipps*
ch1 ch2 ch1 ch2 ch2ch1
cop3223* cop3530*
syl syl
.login
![Page 23: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/23.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 169
mark books dsaa ch1 ch2 ecp ch1 ch2 ipps ch1 ch2 courses cop3223 syl cop3530 syl .login
The directory listing for tree in Figure 17.4
![Page 24: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/24.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 170
Unix directory with file sizes
mark*(1)
books*(1) courses*(1)
ecp*(1)dsaa*(1) ipps*(1)
ch1(9) ch2(7) ch1(4) ch2(6) ch2(8)ch1(3)
cop3223*(1) cop3530*(1)
syl(2) syl(3)
.login(2)
![Page 25: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/25.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 171
ch1 9 ch2 7 dsaa 17 ch1 4 ch2 6 ecp 11 ch1 3 ch2 8 ipps 12 books 41 syl 2 cop3223 3 syl 3 cop3530 4 courses 8 .login 2mark 52
Trace of the size method
![Page 26: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/26.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 172
Uses of binary trees: left is an expression tree and right is a Huffman coding tree
+
a *
- d
a
d
b cb c
![Page 27: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/27.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 173
Result of a naive merge operation
t1.root t2.root
rootX
![Page 28: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/28.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 174
Aliasing problems in the merge operation; T1 is also the current object
T2.root
rootX
oldRootoldT1.Root
T1.root
![Page 29: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/29.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 175
Recursive view used to calculate the size of a tree: ST = SL + SR + 1
SL SR
![Page 30: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/30.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 176
Recursive view of node height calculation: HT = max( HL+1, HR +1 )
HL
HL+1
HR
HR +1
![Page 31: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/31.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 177
Preorder, postorder, and inorder visitation routes
1
2 3
4 6
75
7
1 6
3 5
42
2
1 5
3 7
64
![Page 32: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/32.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 178
Stack states during postorder traversal
a 0b 0a 1
b 1a 1
d 0b 2a 1
d 1b 2a 1
d 2b 2a 1
b 2a 1
d
a 1
b
c 0a 2
e 0c 1a 2
e 1c 1a 2
e 2c 1a 2
c 1a 2
e
c 2a 2 a 2
c a
a
b c
ed
![Page 33: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/33.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 179
Chapter 18
Binary Search Trees
![Page 34: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/34.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 180
Two binary trees (only the left tree is a search tree)
2 9
1 5
3
2 9
1 5
3 8
7 7
![Page 35: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/35.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 181
Binary search trees before and after inserting 6
2 9
1 5
3
7
2 9
1 5
3 6
7
![Page 36: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/36.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 182
Deletion of node 5 with one child, before and after
7
2 9
1 5
3
7
2 9
1
3
![Page 37: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/37.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 183
Deletion of node 2 with two children, before and after
7
2 9
1 5
3
7
3 9
1
4
5
4
![Page 38: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/38.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 184
Using the size data field to implement findKth
X X X
SL SL SL SRSRSR
K < SL + 1 K == SL + 1 K > SL + 1
![Page 39: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/39.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 185
Balanced tree on the left has a depth of log N; unbalanced tree on the right has a depth of N–1
![Page 40: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/40.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 186
Binary search trees that can result from inserting a permu-tation 1, 2, and 3; the balanced tree in the middle is twice as likely as any other
33
12
21
32
3
1
3
2
211
![Page 41: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/41.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 187
Two binary search trees: the left tree is an AVL tree, but the right tree is not (unbalanced nodes are darkened)
12
8 16
4 10 14
2 6
12
8 16
4 10 14
2 6
1
![Page 42: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/42.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 188
Minimum tree of height H
H–1
H
H–2SH–1 SH–2
![Page 43: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/43.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 189
Single rotation to fix case 1
k2
k1
k1
k2
A
B
C
A B C
![Page 44: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/44.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 190
Single rotation fixes AVL tree after insertion of 1
8 16
4 10 14
2 6
4 16
2 8 14
1 6 10
k2
k1
A B
C
1
A
B C
k2
k1
1212
![Page 45: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/45.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 191
Symmetric single rotation to fix case 4
k2
k1
k1
k2
A B C
A
B
C
![Page 46: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/46.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 192
Single rotation does not fix case 2
k2
k1 k2
P
Q
R P
Q
R
k1
![Page 47: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/47.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 193
Left-right double rotation to fix case 2
k3
k1
k2
k1 k3
AB C
D
AB C
D
k2
![Page 48: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/48.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 194
Double rotation fixes AVL tree after insertion of 5
8 16
4 10 14
2 6
6 16
4 8 14
2 105
k3
k1
A
D
5
A C D
k3
k2
C
k2 B
k1
12
B
12
![Page 49: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/49.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 195
Left-right double rotation to fix case 3
k1
k3
k2
A
B CD A
B CD
k3k1
k2
![Page 50: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/50.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 196
A red-black tree is a binary search tree with the following ordering properties:
1. Every node is colored either red or black.2. The root is black.3. If a node is red, its children must be black.4. Every path from a node to a null reference must contain the same number
of black nodes.
Red-black tree properties
![Page 51: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/51.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 197
Example of a red-black tree; insertion sequence is 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55)
15
10 20
70
60 85
65 80 90
40 55
30
5 50
![Page 52: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/52.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 198
If S is black, then a single rotation between the parent and grandparent, with appropriate color changes, restores property 3 if X is an outside grandchild
B
C D E
A
A B C
D E
SP
X
G
X G
S
P
![Page 53: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/53.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 199
If S is black, then a double rotation involving X, the parent, and the grandparent, with appropriate color changes, restores property 3 if X is an inside grandchild
P S
XA D E A B C
B C D E
G X
P G
S
![Page 54: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/54.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 200
If S is red, then a single rotation between the parent and grandparent, with appropriate color changes, restores property 3 between X and P
P S
X
B
C D E
A
A B C
D E
G
X G
P
S
![Page 55: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/55.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 201
Color flip; only if X’s parent is red do we continue with a rotation
C1 C2
X
C1 C2
X
![Page 56: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/56.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 202
Color flip at 50 induces a violation; because it is outside, a single rotation fixes it
15
10 20
70
60 85
65 80 90
40 55
30
5 50
![Page 57: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/57.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 203
Result of single rotation that fixes violation at node 50
15
10 20
60
50 70
55 65 85
80 90
30
5 40
![Page 58: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/58.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 204
Insertion of 45 as a red node
15
10 20
60
50 70
55 65 85
80 9045
30
5 40
![Page 59: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/59.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 205
Deletion: X has two black children, and both of its sibling’s children are black; do a color flip
X T X T
P P
![Page 60: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/60.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 206
Deletion: X has two black children, and the outer child of its sibling is red; do a single rotation
P T
X T P R
R X
![Page 61: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/61.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 207
Deletion: X has two black children, and the inner child of its sibling is red; do a double rotation
P R
X T P T
R X
![Page 62: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/62.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 208
X is black and at least one child is red; if we fall through to next level and land on a red child, everything is good; if not, we rotate a sibling and parent
B C B C B
TX P
TX' P C
X'
![Page 63: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/63.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 209
The level of a node is
• One if the node is a leaf• The level of its parent, if the node is red• One less than the level of its parent, if the node is black
1. Horizontal links are right pointers (because only right children may be red).2. There may not be two consecutive horizontal links (because there cannot be
consecutive red nodes).3. Nodes at level 2 or higher must have two children.4. If a node does not have a right horizontal link, then its two children are at the
same level.
AA-tree properties
![Page 64: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/64.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 210
AA-tree resulting from insertion of 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55, 35
5 10
15
20 35 40 55 65 80 90
30 70
60 8550
![Page 65: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/65.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 211
skew is a simple rotation between X and P
A B C
P X
A B C
P X
![Page 66: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/66.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 212
split is a simple rotation between X and R; note that R’s level increases
A B
X R
A B
XG G
R
![Page 67: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/67.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 213
After inserting 45 into sample tree; consecutive horizontal links are introduced starting at 35
After split at 35; introduces a left horizontal link at 50
After skew at 50; introduces consecutive horizontal nodes starting at 40
5 10 20 35 40 55 65 80 9045
30 70
50 6015 85
5 10 20 35 55 65 80 90
50
45
85604015
30 70
5 10 20 35 55 65 80 90
30
45
70
60 8515 40 50
![Page 68: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/68.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 214
After split at 40; 50 is now on the same level as 70, thus inducing an illegal left horizontal link
After skew at 70; this introduces consecutive horizontal links at 30
After split at 30; insertion is complete
5 10 20 35 55 65 80 9045
50
15 40 60 85
30 70
5 10 20 35 55 65 80 9045
15 40 60 85
30 50 70
5 10 20 35 55 65 80 9045
15 40 60 85
30 70
50
![Page 69: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/69.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 215
When 1 is deleted, all nodes become level 1, introducing horizontal left links
3 4 6 71
2 5
![Page 70: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/70.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 216
Five-ary tree of 31 nodes has only three levels
![Page 71: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/71.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 217
B-tree of order 5
41 66 87
92 9772 78 8348 51 548 18 26 35
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
31
32
35
36
37
38
39
41
42
44
46
48
49
50
51
52
53
54
56
58
83
84
85
78
79
81
72
73
74
76
66
68
69
70
97
98
99
92
93
95
87
89
90
59
![Page 72: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/72.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 218
A B-tree of order M is an M-ary tree with the following properties:
1. The data items are stored at leaves.2. The nonleaf nodes store up to keys to guide the searching; key i repre-
sents the smallest key in subtree .3. The root is either a leaf or has between 2 and M children.4. All nonleaf nodes (except the root) have between and M children.5. All leaves are at the same depth and have between and L children,
for some L.
B-tree properties
M 1–i 1+
M 2⁄L 2⁄
![Page 73: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/73.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 219
B-tree after insertion of 57 into tree in Figure 18.70
41 66 87
92 9772 78 8348 51 548 18 26 35
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
31
32
35
36
37
38
39
41
42
44
46
48
49
50
51
52
53
54
56
57
83
84
85
78
79
81
72
73
74
76
66
68
69
70
97
98
99
92
93
95
87
89
90
58
59
![Page 74: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/74.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 220
Insertion of 55 in B-tree in Figure 18.71 causes a split into two leaves
41 66 87
92 9772 78 8348 51 54 578 18 26 35
246
810121416
18202224
2628303132
3536373839
41424446
484950
515253
545556
838485
787981
72737476
66686970
979899
929395
878990
575859
![Page 75: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/75.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 221
Insertion of 40 in B-tree in Figure 18.72 causes a split into two leaves and then a split of the parent node
26 41 66 87
92 9772 78 8348 51 54 5735 38
2628303132
353637
383940
41424446
484950
515253
545556
838485
787981
72737476
66686970
979899
929395
878990
575859
8 18
246
810121416
18202224
![Page 76: Chapter 15 - School of Computing and Information Sciencesweiss/dsj/transparencies/Transparencys15-18.pdf · Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a](https://reader030.vdocument.in/reader030/viewer/2022040420/5e0887ef31e52c16c3400780/html5/thumbnails/76.jpg)
Copyright 1998 by Addison-Wesley Publishing Company 222
B-tree after deletion of 99 from Figure 18.73
26 41 66 83
87 9272 7848 51 54 5735 38
26
28
30
31
32
35
36
37
38
39
40
41
42
44
46
48
49
50
51
52
53
54
55
56
78
79
81
72
73
74
76
66
68
69
70
92
93
95
97
98
87
89
90
83
84
85
57
58
59
8 18
2
4
6
8
10
12
14
16
18
20
22
24