17
CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O(log n) Search

CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

Embed Size (px)

Citation preview

Page 1: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

CSC 213 –Large Scale

Programming

Lecture 18:

Zen & the Art of O(log n) Search

Page 2: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

Today’s Goal

Discuss AVL Trees Relationship to BinaryTree and BST What an AVL Tree looks like and how they work Effects on big-Oh notation AVL Tree Limitations

Page 3: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

Dictionary ADT

Dictionary ADT maps key to 1 or more valuesUsed wherever search is (e.g., everywhere)Implementation using Sequence takes O(n) timeWith good batch of hash, could get O(1) time

But could still end up with O(n) timeIf data are random, BST takes O(log n) time

But for ordered data, BST still takes O(n) time

There must be a better way!

Page 4: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

Binary Search Trees

May not be complete But faster when they are

Use different ordering Lower keys in left

subtree Higher keys in right

subtree Equal keys not specified,

but must be consistent

6

92

41 10

Page 5: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

AVL Tree Definition

Another type of BST Algorithm guarantees O(log n) time Does not allow tree to

become linked list Keeps tree in balance

AVL Tree balanced when each Node’s children differ in height by at most 1 Maintains balance through

trinode restructuring

Node heights are shown in red

6

92

41 81

1

1

5

2

3 2

4

Page 6: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

Trinode Restructuring

Insertion & removal can unbalance tree Trinode restructuring restores tree’s tao

Used when node’s children’s height differ Takes node, taller child, & taller grandchild Grandchild must be taller of taller child’s children

Move median node to root of subtree Other 2 nodes in restructuring become its children

Page 7: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

Trinode Restructuring

Case 1: Single rotation (e.g., 3 in a row) Move child of 3 to root of subtree Subtrees become grandchildren of node No change in the order of subtrees

b

a

c

T0

T1

T2 T3

b

a c

T0 T1 T2 T3

Page 8: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

Trinode Restructuring

Case 2: Double rotation Taller child & grandchild go opposite ways Move grandchild up to the root Subtrees become grandchildren of node, but

maintain order

c

b

a

T0

T1 T2

T3

b

ca

T0 T1 T2 T3

Page 9: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

1

Insertion in an AVL Tree

Begins like normal BST insertion Example: insert(5)

7

92

41 81

2

1

6

2

3 2

4

51

Page 10: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

Insertion in an AVL Tree

Travel up tree checking nodes for balance Must increase tao of unbalanced nodes

7

92

41 8

6

5

Page 11: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

Insertion in an AVL Tree

Must walk up tree starting at inserted Node Stop once we hit the root node Update height for each Node along path Check if Node’s children are balanced Perform rotations when balance needs restoration

Not all insertions require rebalancing Will need at most 1 insertion per Node But some insertions need multiple rebalancings

Page 12: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

1

Removal in an AVL Tree

Start with normal BST removal But removal may cause drop in the TAO

Example: remove(7)7

92

51 8

64

1

1

12

3 2

4

1

8

Page 13: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

1

Removal in an AVL Tree

Start with normal BST removal But removal may cause drop in the TAO

Example: remove(7)7

92

51

64

1

1

2

3

4

1

8

Page 14: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

Removal in an AVL Tree

Start with normal BST removal But removal may cause drop in the TAO

Example: remove(7)

7

82

41 6

5

9

Page 15: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

Removal in an AVL Tree

Again walk up tree checking for balance Update heights as we go

Only examines Nodes along path Height of nodes not on path cannot be changed

May need multiple restructuring operations Uses same restructuring as insert

Use unbalanced node’s taller child & taller grandchild Does not matter if child & grandchild on path

Page 16: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

Restructuring for Dummies

Store the 7+1 Nodes in local variables Plus one records parent Node of this subtree

Set all left, right, & parent from pattern Median node is root; subtrees maintain order

c

b

a

T0

T1 T2

T3

b

ca

T0 T1 T2 T3

b

ca

T0 T1 T2 T3

b

a

c

T0

T1

T2 T3

Page 17: CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search

In the Next Lecture

Discuss even faster ways of searching No difference in big-Oh notation, however

But provide significant speedup in real-life Learn new ways of amortizing costs Discover proper use of the term “splay”

When and why we splay a tree What it means to splay Any other excuses I can think of to say splay