AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut...

61
AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from http://cseweb.ucsd.edu/~kube/cls/100/Lectures/lec4.avl/lec4.pdf

Transcript of AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut...

Page 1: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

AVL Trees Heaps

And ComplexityD. Thiebaut

CSC212 — Fall 2014

Some material taken from http://cseweb.ucsd.edu/~kube/cls/100/Lectures/lec4.avl/lec4.pdf

Page 2: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Complexity Of BST Operations

or

"Why Should We Use BST

Data Structures"

Page 3: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

What We're After:• What's the worst amount of work we can expect

• when we insert?

• when we delete?

• when we search, successfully or unsuccessfully?

• What's the average amount of work we can expect?

• We'd like to know about the best case, but in general it doesn't occur that often.

Page 4: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Which Two-Operations In BSTs are Similar?

• Search/Find {

• Insert

• Delete

• Successful

• Unsuccessful

Page 5: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Worst Case for Inserts or Unsuccessful Searches

• Definition: The depth of a node x, d(x), is the # of nodes from the root to that node. d(x) = 0-based level of the node plus 1.

• What is the worst possible number of nodes visited for searching or inserting in a BST?

• So, what is the worst-case complexity for insert or unsuccessful operations?

Page 6: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Operation Outcome Worst Case !Complexity

Average!Complexity

Best!Complexity

Insertion X O(N)

SearchSuccessful

Unsuccessful O(N)

Deletion X

Page 7: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Best-Case complexity for search?

Page 8: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Operation Outcome Worst Case !Complexity

Average!Complexity

Best!Complexity

Insertion X O(N)

SearchSuccessful O(1)

Unsuccessful O(N) O(1)

Deletion X

Page 9: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Average Case for Successful Searches

Page 10: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Need More Definitions• BST of N nodes

• keyi = key residing in node xi, i=1, 2… N

• d(xi) = depth of node xi

• pi = probability of searching for keyi

What is the average number of nodes visited

when searching a BST of N nodes?

Page 11: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

So, we need to know pi…

Davg(N) =NX

i=1

dipi

Page 12: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

• if all the keys are equally likely, then all the pi are identical.

• if the pi are not identical, then some keys are more likely than others, and we can take advantage of this self-adjusting trees (Section 6.8)

Page 13: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Equally Likely Keys

• equally likely keys: pi = 1/N

• Davg(N) depends on tree shape!

Davg(N) = 1/NNX

i=1

di = total node depth

Page 14: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Unbalanced Tree

1/N

log(N+1)X

i=1

di

pi

= 1/N(1 + 2 + 3 + ... + N)

= ?

Page 15: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Fully Balanced Tree1x1 = 1x2(1-1)

2x2 = 2x2(2-1)

3x4 = 3x2(3-1)

ix2(i-1)

1/N

log(N+1)X

i=1

i2(i�1) < log(N + 1) = O(log N)

Page 16: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

O(N)

O( log N )?Successful Search:

Unbalanced Trees !Balanced Trees{

Page 17: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

But… Are average BSTs tall or

fat?

Page 18: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

What about Successful Search in a Random BST?

Page 19: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

What about Successful Search in a Random BST?

• We need to look at all possible shapes the BST of N nodes can have and compute the average number of probes required for each successful search, and average over all possible shapes of the BST…

• Huge amount of combinations!

Different BSTs of 3 nodes

and lengths of different paths for each

1+2+3 =6

1+2+3 =6

1+2+3 =6

1+2+2 =5

1+2+3 =6

Page 20: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Davg(N) ⇡ 1.386 log2 N

Average # of Probes for Successful Searches in a "random" BST

of N nodes:

Page 21: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Operation Outcome Worst Case !Complexity

Average!Complexity

Best!Complexity

Insertion X O(N) O(log N) O(1)

SearchSuccessful O(N) O(log N) O(1)

Unsuccessful O(N) O(log N) O(1)

Deletion X O(N) O(log N) O(1)

We don't really care

That's what we expect

Unlikely to happen

Page 22: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

AVL-Trees• Named for Adelson-Velskii and Landis

• 1962

• Important property: For any node X in the tree, the heights of the left and right subtrees of X differ by at most 1

-1

0

+1

0 -1

0

0

0 0

+1

0

Page 23: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

AVL

Page 24: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

AVL Trees Rely on Rotations

Q

PQ

P

Rotation Left

Rotation Right

Page 25: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Q

P

h

h

h

add 'Z'

Page 26: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Q!0

P+1

h

h

h

Q+1

P!+2

h

h

h+1

z

h+2

Page 27: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Q!0

P+1

h

h

h

Q+1

P!+2

h

h

h+1

Q0

P!0

hhh+1

Rotation Left

Page 28: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

AVL Time Complexity

Operation Worst Case !Complexity

Average!Complexity

Insertion O(log N) O(log N)

Search O(log N) O(log N)

Deletion O(log N) O(log N)

Page 29: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Java

• Not that many "pure" trees

• javax.swing.tree

• javax.swing.tree TreeModel

• javax.swing.tree TreeNode

Page 30: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Heaps

Page 31: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

A heap is:

• A fully balanced binary tree, will all leaves on the left-most inner nodes

• The key of a parent is larger than or equal to the key of its children

30

20 10

920 15

30

30

20

30

20 10

Page 32: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Typically used for…• Priority queues: get the next element with the

highest priority

First-In… Highest-Priority Out

Page 33: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Properties• The largest element is in the root, always

• The heap folds nicely…

30

20 15

920 15 10

What can you say of the

keys other than the root?

Page 34: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Properties• The largest element is in the root, always

• The heap folds nicely…

30

20

15

9

20

15

10

Page 35: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Properties• The largest element is in the root, always

• The heap folds nicely…

30

20

15

9

20

15

10

Page 36: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Properties• The largest element is in the root, always

• The heap folds nicely…

30

20

15

9

20

15

2030

15

1520

91010

Page 37: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Properties• The largest element is in the root, always

• The heap folds nicely…

30

20

15

9

20

15

2030

15

1520

91010

1 2 3 4 5 6 7 8 9

i !!2*i 2*i+1

Page 38: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

insertion of a new key

30

20

15

9

20

15

2030

15

1520

91010

1 2 3 4 5 6 7 8 9

i !!2*i 2*i+1

5 key

Page 39: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

insertion of a new key

30

20

15

9

20

15

2030

15

1520

9

51010

1 2 3 4 5 6 7 8 9

i !!2*i 2*i+1

5

Page 40: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

insertion of a new key

30

20

15

9

20

15

2030

15

1520

9

51010

1 2 3 4 5 6 7 8 9

i !!2*i 2*i+1

5

heapify up

Page 41: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

insertion of a new key

30

20

15

9

20

15

2030

15

1520

9

51010

1 2 3 4 5 6 7 8 9

i !!2*i 2*i+1

5

Page 42: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

insertion of a new key

30

20

15

9

20

15

2030

15

1520

9

51010

1 2 3 4 5 6 7 8 9

i !!2*i 2*i+1

5

40 key

Page 43: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

insertion of a new key

30

20

15

9

20

15

2030

15

1520

9

51010

1 2 3 4 5 6 7 8 9

i !!2*i 2*i+1

5

40

Page 44: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

insertion of a new key

30

20

15

9

20

15

2030

15

1520

9

510

40

10

1 2 3 4 5 6 7 8 9

i !!2*i 2*i+1

5

40

Page 45: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

insertion of a new key

30

20

15

9

20

15

2030

15

1520

9

510

40

10

1 2 3 4 5 6 7 8 9

i !!!2*i 2*i+1

5

40

heapify up

Page 46: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

insertion of a new key

30

20

15

9

40

15

2030

15

1540

9

510

20

10

1 2 3 4 5 6 7 8 9

i !2*i 2*i+1

5

20

heapify up

Page 47: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

insertion of a new key

30

40

15

9

20

15

4030

15

1520

9

510

20

10

1 2 3 4 5 6 7 8 9

i 2*i 2*i+1

5

20

heapify up

Page 48: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

insertion of a new key

40

30

15

9

20

15

3040

15

1520

9

510

20

10

1 2 3 4 5 6 7 8 9

i 2*i 2*i+1

5

20

heapify up

Page 49: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

insertion of a new key

40

30

15

9

20

15

3040

15

1520

9

510

20

10

1 2 3 4 5 6 7 8 9

i 2*i 2*i+1

5

20

Page 50: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Algorithm

heapifyUp( node ) begin while ( node has a parent ) and ( node.key > parent.key ) begin swap( node and its parent ) // node is now in its parent's original position end end

Page 51: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Deleting the Root

40

30

15

9

20

15

3040

10

1520

9

515

10

10

1 2 3 4 5 6 7 8 9

i 2*i 2*i+1

5

10

Page 52: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Deleting the Root40

30

15

9

20

15

3040

15

1520

9

510

10

10

1 2 3 4 5 6 7 8 9

i 2*i 2*i+1

5

10

40

Page 53: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

40

Deleting the Root

30

15

9

20

15

3010

15

1520

9

510

XX

10

1 2 3 4 5 6 7 8 9

i 2*i 2*i+1

5

10

Page 54: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Deleting the Root

10

15

9

20

15

1030

15

1520

9

510

XX

10

1 2 3 4 5 6 7 8 9

i 2*i 2*i+1

5

30heapify down

Page 55: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Deleting the Root

10

15

9

20

15

1030

15

1520

9

510

XX

10

1 2 3 4 5 6 7 8 9

i 2*i 2*i+1

5

30

heapify down

Page 56: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Deleting the Root

20

15

9

10

15

2030

15

1510

9

510

XX

10

1 2 3 4 5 6 7 8 9

i 2*i 2*i+1

5

30

heapify down

Page 57: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Deleting the Root

20

15

9

10

15

2030

15

1510

9

510

XX

10

1 2 3 4 5 6 7 8 9

i 2*i 2*i+1

5

30

heapify down

Page 58: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Deleting the Root

20

15

9

10

15

2030

15

1510

9

510

XX

10

1 2 3 4 5 6 7 8 9

5

30

Page 59: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

Deleting the Root

20

15

9

10

15

2030

15

1510

9

510

XX

10

1 2 3 4 5 6 7 8 9

5

30

Page 60: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

AlgorithmheapifyDown( node ) begin if node is a leaf then return! maxNode = node's child with highest key if maxNode.key > node.key then begin swap( node and maxNode ) // now node is in place of maxNode's original position heapifyDown( node ) // recurse down end end

Page 61: AVL Trees Heaps And Complexity - Clark Science Center · AVL Trees Heaps And Complexity D. Thiebaut CSC212 — Fall 2014 Some material taken from kube/cls/100/Lectures/lec4.avl/lec4.pdf

OperationWorst!Time!

Complexity

Average!Time!

Complexity

Best!Time!

Complexity

Insert!key O(log N) O(log N) O(log N)

delete!root O(log N) O(log N) O(log N)