lecture 16: zen & the art of o ( log n ) search
DESCRIPTION
CSC 213 – Large Scale Programming. Lecture 16: Zen & the Art of O ( log n ) Search. Today’s Goals. Review Map & Dictionary implementations What do they do well? When would they be used? Why do they suck so much another is needed? Discuss how BSTs can achieve Zen-like balance - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/1.jpg)
LECTURE 16:ZEN & THE ART OF O(LOG n) SEARCH
CSC 213 – Large Scale Programming
![Page 2: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/2.jpg)
Today’s Goals
Review Map & Dictionary implementations What do they do well? When would they be
used? Why do they suck so much another is
needed? Discuss how BSTs can achieve Zen-like
balance Nodes will need more data, what fields do
we add? How this will modify approach to add &
remove data? What nodes get restructured and how to
know? Are there any neat hacks to coding
restructures? How traces help us write this tricky code
easily
![Page 3: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/3.jpg)
Map & Dictionary ADT
Implementation
Searching
Adding Removing
Ordered List O(log n) O(n) O(n)Unordered List
O(n) O(n)/O(1) O(n)
Hash O(n) O(n) O(n) if lucky/good
O(1) O(1) O(1)
BST O(n) O(n) O(n) if balanced
O(log n) O(log n) O(log n)
![Page 4: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/4.jpg)
Map & Dictionary ADT
Implementation
Searching
Adding Removing
Ordered List O(log n) O(n) O(n)Unordered List
O(n) O(n)/O(1) O(n)
Hash O(n) O(n) O(n) if lucky/good
O(1) O(1) O(1)
BST O(n) O(n) O(n) if balanced
O(log n) O(log n) O(log n)
![Page 5: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/5.jpg)
Binary Search Trees
Maintain specific order Lower keys - left
subtree Right subtree for higher Equal keys not
specified, just be consistent
Fastest when complete Normally we see worst
case How to force this to
balance?
6
92
41 108
![Page 6: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/6.jpg)
AVL Tree Definition
Fancy type of BST O(log n) time
provided For this, needs more
info
6
92
41 8
5
![Page 7: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/7.jpg)
AVL Tree Definition
Fancy type of BST O(log n) time
provided For this, needs more
info
6
92
41 8
5
Node heights are shown in blue
1
21 1
23
4
![Page 8: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/8.jpg)
AVL Tree Definition
Fancy type of BST O(log n) time
provided For this, needs more
info Keep tree balanced
by… Checking heights of
kids Only let differ by 0 or
1
6
92
41 8
5
Node heights are shown in blue
1
1 1
3
4
2
2
![Page 9: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/9.jpg)
AVL Tree Definition
Fancy type of BST O(log n) time
provided For this, needs more
info Keep tree balanced
by… Checking heights of
kids Only let differ by 0 or
1
Node heights are shown in blue
6
92
41 8
51
1 1
3
4
2
2
![Page 10: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/10.jpg)
AVL Tree Definition
Fancy type of BST O(log n) time
provided For this, needs more
info Keep tree balanced
by… Checking heights of
kids Only let differ by 0 or
1 Fix larger
differences by Shifting nodes in the BST
Help it maintain balance
Node heights are shown in blue
6
92
41 8
51
1 1
3
4
2
2
![Page 11: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/11.jpg)
AVL Tree Definition
Fancy type of BST O(log n) time
provided For this, needs more
info Keep tree balanced
by… Checking heights of
kids Only let differ by 0 or
1 Fix larger
differences by Shifting nodes in the BST
Help it maintain balance
Trinode Restructuring
Node heights are shown in blue
6
92
41 8
51
1 1
3
4
2
2
![Page 12: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/12.jpg)
Trinode Restructuring
Insertion & removal can unbalance tree Restore tree’s tao using trinode
restructuring When node’s children’s height differ
more than 1 Node, taller child, & tallest grandchild
used for this Grandchild used must be taller child of
taller child Median node subtree's root after
restructure Restructure makes the other 2 nodes its
children Also redistributes other child & grandkids
![Page 13: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/13.jpg)
Trinode Restructuring
CASE 1: Single rotation (e.g., 3 in a row)
b
a
cT0
T1
T2 T3
![Page 14: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/14.jpg)
Trinode Restructuring
CASE 1: Single rotation (e.g., 3 in a row) Move the taller child to subtree root Parent & grandchild become kids of the
taller child
b
a
cT0
T1
T2 T3
b
a c
![Page 15: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/15.jpg)
Trinode Restructuring
CASE 1: Single rotation (e.g., 3 in a row) Move the taller child to subtree root Parent & grandchild become kids of the
taller child Does not (CANNOT) change order of nodes
in tree b
a
cT0
T1
T2 T3
b
a c
T0 T1 T2 T3
![Page 16: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/16.jpg)
Trinode Restructuring
CASE 2: Double rotation (e.g., zig-zag)
T0
T1 T2
T3
b
a
c
![Page 17: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/17.jpg)
Trinode Restructuring
CASE 2: Double rotation (e.g., zig-zag) Start when taller child & grandchild in
opposition Grandchild becomes root once this
completesc
a bT0
T1 T2
T3
b
a
c
![Page 18: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/18.jpg)
Trinode Restructuring
CASE 2: Double rotation (e.g., zig-zag) Start when taller child & grandchild in
opposition Grandchild becomes root once this
completes Does not (CANNOT) change order of nodes
in tree
T0 T1 T2 T3
c
a bT0
T1 T2
T3
b
a
c
![Page 19: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/19.jpg)
AVL Tree Insertion
Normal BST insertion starts the process Once added must then check tree for
balance insert(5)
1
7
92
41 81 1
6
2
3 2
4
![Page 20: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/20.jpg)
AVL Tree Insertion
Normal BST insertion starts the process Once added must then check tree for
balance insert(5)
1
7
92
41 81 1
6
2
3 2
4
![Page 21: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/21.jpg)
AVL Tree Insertion
Normal BST insertion starts the process Once added must then check tree for
balance insert(5)
?
7
92
41 81 1
6?
?
? 2
?
5
![Page 22: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/22.jpg)
Insertion in an AVL Tree
From inserted node, walk up to root At worst, stop after root has been
processed With each node, check if children are
balanced Restructure to restore balance among the
children Each insertion performs at most 1
restructuring Unbalanced node on path from root to
inserted node Most cases do not require a trinode
restructuring
![Page 23: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/23.jpg)
AVL Tree Insertion
Normal BST insertion starts the process Once added must then check tree for
balance insert(5)
?
7
92
41 81 1
61
?
? 2
?
5
![Page 24: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/24.jpg)
AVL Tree Insertion
Normal BST insertion starts the process Once added must then check tree for
balance insert(5)
?
7
92
41 81 1
61
?
? 2
?
5
![Page 25: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/25.jpg)
AVL Tree Insertion
Normal BST insertion starts the process Once added must then check tree for
balance insert(5)
2
7
92
41 81 1
61
?
? 2
?
5
![Page 26: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/26.jpg)
AVL Tree Insertion
Normal BST insertion starts the process Once added must then check tree for
balance insert(5)
2
7
92
41 81 1
61
?
? 2
?
5
![Page 27: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/27.jpg)
AVL Tree Insertion
Normal BST insertion starts the process Once added must then check tree for
balance insert(5)
2
7
92
41 81 1
61
?
? 2
?
5
Oops
![Page 28: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/28.jpg)
9
Trinode Restructuring
Uses the node, taller child, tallest grandchild Must be on insertion path, so used for
restructuring insert(5)
2
7
2
41 81 1
61
?
?
?
5
2
![Page 29: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/29.jpg)
9
4
Trinode Restructuring
Uses the node, taller child, tallest grandchild Must be on insertion path, so used for
restructuring insert(5)
?
7
2
51 81 1
6?
?
? 2
?
![Page 30: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/30.jpg)
9
4
After The Restructuring
Balance restored to tree; tao returns to normal Will need to complete walk updating
heights insert(5)
1
7
2
51 81 1
61
2
3 2
4
![Page 31: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/31.jpg)
9
4
Removing From AVL Tree
Normal BST removal starts AVL tree removal Removal of node may also cause tao to
drop remove(9)
1
7
2
51 81 1
61
2
3 2
4
![Page 32: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/32.jpg)
9
4
Removing From AVL Tree
Normal BST removal starts AVL tree removal Removal of node may also cause tao to
drop remove(9)
1
7
2
51 81 1
61
2
3 2
4
![Page 33: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/33.jpg)
Removing From AVL Tree
Normal BST removal starts AVL tree removal Removal of node may also cause tao to
drop remove(9) 8
41
7
2
511
61
2
3 ?
?
![Page 34: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/34.jpg)
Removing From AVL Tree
Again walk up tree checking for balance Examine nodes along path only
There will be no change for nodes not on path
Multiple restructuring operations may be needed However, may not need any restructuring Only 1 restructuring could be needed
Use the node, taller child & tallest grandchild May not be on the path from start to root Works exactly like restructuring during
insert
![Page 35: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/35.jpg)
Removing From AVL Tree
Normal BST removal starts AVL tree removal Removal of node may also cause tao to
drop remove(9) 8
41
7
2
511
61
2
3 ?
?
![Page 36: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/36.jpg)
Removing From AVL Tree
Normal BST removal starts AVL tree removal Removal of node may also cause tao to
drop remove(9) 8
41
7
2
511
61
2
3 1
?
![Page 37: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/37.jpg)
Trinode Restructuring
Uses the node, taller child, tallest grandchild Nodes may not be on path used during the
removal remove(9) 8
41
7
2
511
61
2
3 1
?
![Page 38: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/38.jpg)
After the Restructure
Cannot stop till we reach the top of the tree May need to perform 0, 1, or multiple
restructures remove(9) 7
5
2
411 1
2 2
3
16
18
![Page 39: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/39.jpg)
After the Restructure
Cannot stop till we reach the top of the tree May need to perform 0, 1, or multiple
restructures remove(9) 7
5
2
411 1
2 2
3
16
18
![Page 40: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/40.jpg)
After the Restructure
Cannot stop till we reach the top of the tree May need to perform 0, 1, or multiple
restructures remove(9) 7
5
2
411 1
2 2
3
16
18
![Page 41: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/41.jpg)
Restructuring for Dummies
Store the 7+1 nodes in local variables Record subtree’s parent in the “+1”
variable All of left, right, & parent set using
pattern Median node is root; subtrees maintain
order
![Page 42: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/42.jpg)
Restructuring for Dummies
Store the 7+1 nodes in local variables Record subtree’s parent in the “+1”
variable All of left, right, & parent set using
pattern Median node is root; subtrees maintain
order b
a
cT0
T1
T2 T3
b
a c
T0 T1 T2 T3
![Page 43: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/43.jpg)
Restructuring for Dummies
Store the 7+1 nodes in local variables Record subtree’s parent in the “+1”
variable All of left, right, & parent set using
pattern Median node is root; subtrees maintain
orderT0
T1 T2
T3
b
a
c
c
a b
T0 T1 T2 T3
![Page 44: Lecture 16: Zen & the Art of O ( log n ) Search](https://reader036.vdocuments.net/reader036/viewer/2022062315/568164df550346895dd73f87/html5/thumbnails/44.jpg)
For Next Lecture
Weekly assignment to demonstrate knowledge Due at regular time Tuesday
Program#1 tests due Monday at midnight Remember to submit updated design & test
cases Quiz on Monday on trees, BSTs, & AVL
trees Please study up on all the material covered Use your templates: they can greatly
simplify process Do not forget that pictures worth 1000
words