Lecture 16: Zen & the Art of O ( log n ) Search
description
Transcript of Lecture 16: Zen & the Art of O ( log n ) Search
LECTURE 16:ZEN & THE ART OF O(LOG n) SEARCH
CSC 213 – Large Scale Programming
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
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)
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)
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
AVL Tree Definition
Fancy type of BST O(log n) time
provided For this, needs more
info
6
92
41 8
5
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
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
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
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
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
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
Trinode Restructuring
CASE 1: Single rotation (e.g., 3 in a row)
b
a
cT0
T1
T2 T3
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
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
Trinode Restructuring
CASE 2: Double rotation (e.g., zig-zag)
T0
T1 T2
T3
b
a
c
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
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
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
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
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
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
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
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
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
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
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
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
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
?
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
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
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
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 ?
?
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
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 ?
?
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
?
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
?
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
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
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
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
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
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
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