cse 100: avl trees · balanced binary trees • deterministic balancing • change insert and...

43
CSE 100: AVL TREES

Upload: others

Post on 21-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

CSE 100: AVL TREES

Page 2: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

READING QUIZ – NO TALKING – NO NOTES

Q1: What is the definition of a balanced tree?

A.  All nodes either have two children or they are leaves

B.  The number of nodes to the left of the root is equal to the number of nodes to the right of the root

C.  The height of the tree is O(log2N), where N is the number of nodes in the tree

D.  The tree is a binary tree and it is completely full

Page 3: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

READING QUIZ – NO TALKING – NO NOTES

Q2: Which of the following is a benefit of randomized methods for balancing BSTs over deterministic methods?

A.  Balance is guaranteed B.  Operations are simpler to implement C.  Balance is not guaranteed

Page 4: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

READING QUIZ – NO TALKING – NO NOTES

Q3: True or false: AVL trees are globally rebalanced periodically.

A.  True B.  False

Page 5: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

READING QUIZ – NO TALKING – NO NOTES

Q4: What is a balance factor in an AVL tree?

A.  A number in a node that indicates the difference between the height of the left and right subtrees of that node

B.  The global amount that the tree is (or is not) out of balance

C.  The number of rotations that it will take to bring the tree back into balance

Page 6: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Balanced Binary Trees Recall: • Worst case for find in a BST is :

• Worst case for find in a Balanced BST is:

6

Page 7: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Balanced Binary Trees • Deterministic Balancing

•  Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees, red-black trees

• Randomized Balancing •  Use random numbers to determine the values of the nodes,

independent from the keys •  The resulting tree will probably be balanced (e.g., more like the

average case), but not guaranteed

TODAY

LATER

7

Are AVL trees new to you? A.  Yes B.  No

Page 8: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Height of a node Mark the height of each node in the tree.

42

10 67

53 90

What is the height of node with key 53? A.  Zero B.  One C.  Two D.  Three

8

Height of a node =

Depth of a node=

Page 9: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Local computation of height of a node

42

10 67

53 90

Height of a node= max{ Height of left child, Height of right child} +1

•  Goal of AVL trees: For each node, maintain the difference between height of left and right children to within +/-1

•  Each node maintains then a balance factor: •  Balance factor= height of the right child – height of left child

9

Page 10: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Which of the following is/are balanced trees? And thus can become AVL trees by adding the balance factors

42

10 67

53 90

87

42

10 67

53 90

87

11

42

10 67

53 90

A B C

D. A&C E. A&B&C

Annotate the trees with balance factors (for those that are balanced)

10

Page 11: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Finding an element in a AVL Tree is worst case O(logN)! How would you prove this (we won’t, but imagine we wanted to)? A.  Come up with a formula that shows that the height of the tallest AVL tree

with N nodes is never bigger than c*logN + k, for some c and k (assuming large N).

B.  Come up with a formula that shows that the number of nodes, N, in an AVL tree is never bigger than c*logN + k for some c and k (assuming large N)

C.  Come up with a formula that shows that the height of the tree never grows by more than log(N) when you insert a node.

D.  Run some empirical tests to show that for any size tree, the time to find an element is never more than some formula c*logN + k for some c and k (assuming large N)

11

Page 12: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

An AVL Tree is worst case O(logN) to find an element! (We might prove this later)

But how does the tree stay balanced as new nodes are inserted??

How would you prove this? A.  Come up with a formula that shows that the height of the tallest AVL tree

with N nodes is never bigger than c*logN + k, for some c and k (assuming large N).

The key to this proof is showing that the height stays “small”, no matter how legally “unbalanced” the tree is.

12

Page 13: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Inserting and rebalancing 30

15 70

10 20 60 85

5 50 65 80 90

40 55

1

-1

-1 0

0

-1

0 -1

0 0 0 0

0 0

Insert 3

13

Page 14: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Inserting and rebalancing 30

15 70

10 20 60 85

5 50 65 80 90

40 55

1

-1

-1 0

0

-1

0 -1

0 0 0 0

0 0

Insert 3

3

0 Propagate news up

14

Page 15: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Inserting and rebalancing 30

15 70

10 20 60 85

5 50 65 80 90

40 55

1

-1

-1 0

-1

-1

0 -1

0 0 0 0

0 0

Insert 3

3

0

We’re OK, Keep going up

15

Page 16: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Inserting and rebalancing 30

15 70

10 20 60 85

5 50 65 80 90

40 55

1

-1

-2 0

-1

-1

0 -1

0 0 0 0

0 0

Insert 3

3

0

Uh oh!

16

Page 17: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Rotations

17

X

y

C A

B

X

y

C A B

Page 18: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Inserting and rebalancing

30

15 70

10 20 60 85

5 50 65 80 90

40 55

1

-1

-2 0

-1

-1

0 -1

0 0 0 0

0 0

Insert 3

3

0

Rotate! Left or right?

cut

18

Which of following is needed to rebalance? A.  Rotate 5 left B.  Rotate 10 left C.  Rotate 5 right D.  Rotate 10 right

Page 19: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Inserting and rebalancing 30

15 70

10 20 60 85

5 50 65 80 90

40 55

1

-1

-2 0

-1

-1

0 -1

0 0 0 0

0 0

Insert 3

3

0

lift

19

Page 20: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Inserting and rebalancing 30

15 70

10

20 60 85 5

50 65 80 90

40 55

1

-1

0

0 0

-1

0 -1

0 0 0 0

0 0

Insert 3

3

0

Adjust and reattach

20

Page 21: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

We just did a single rotation of 5 around 10 A.k.a. a single right rotation

30

15 70

10

20 60 85 5

50 65 80 90

40 55

1

-1

0

0 0

-1

0 -1

0 0 0 0

0 0

Insert 3

3

0

Done!

21

Page 22: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Single rotation practice

30

15 70

20 60

0

1

0

-1

0

What could you insert into this AVL tree that would result in a single right rotation? A.  71 B.  10 C.  50 D.  66

22

Page 23: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Single rotation practice

30

15 70

20 60

0

1

0

-1

0

Insert 50. Draw the resulting AVL tree. (Don’t peek)

23

Page 24: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Single rotation practice

30

15 70

20 60

0

1

0

-2

-1

After insertion

50

0

24

Page 25: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Single rotation practice

30

15

70 20

60

0

1

0 0

0

After rotation

50

0

25

Page 26: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Single rotation is not enough

30

15 70

20 60

0

1

0

-1

0

What happens if we insert 66?

26

Page 27: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Single rotation is not enough

30

15 70

20 60

0

1

0

-2

1

66

0

cut

27

Page 28: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Is this a valid rotation?

30

15 70

20 60

0

1

0

-2

1

66

0

lift

70

60

0

0

66

0

A.  Yes B.  No

28

Page 29: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Invalid rotation!

30

15

20

0

1

0

70

60

-2

1

66

0

UH OH!

29

Page 30: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Double rotation to the rescue

30

15 70

20 60

0

1

0

-2

1

Single rotations only work when involved nodes are “in a line” So we will first rotate left around 60, then we can rotate right around 70.

66

0

30

Page 31: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Double rotation to the rescue

30

15 70

20 60

0

1

0

-2

1

Single rotations only work when involved nodes are “in a line” So we will first rotate left around 60, then we can rotate right around 70.

66

0

cut

31

Page 32: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Double rotation to the rescue

30

15 70

20 60

0

1

0

-2

1

Single rotations only work when involved nodes are “in a line” So we will first rotate left around 60, then we can rotate right around 70.

66

0 lift

32

Page 33: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Double rotation to the rescue

30

15 70

20

60

0

1

0

-2

-1

Single rotations only work when involved nodes are “in a line” So we will first rotate left around 60, then we can rotate right around 70.

66

0 adjust

33

Page 34: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Double rotation to the rescue

30

15 70

20

60

0

1

0

-2

-1

Single rotations only work when involved nodes are “in a line” So we will first rotate left around 60, then we can rotate right around 70.

66

0 reattach

34

Page 35: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Double rotation to the rescue

30

15 70

20

60

0

1

0

-2

-1

Single rotations only work when involved nodes are “in a line” So we will first rotate left around 60, then we can rotate right around 70.

66

0

Where in the tree above should I cut to start this rotation?

A

B

C

35

Page 36: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Double rotation to the rescue

30

15

70 20 60

0

1

0

0

0

Single rotations only work when involved nodes are “in a line” So we will first rotate left around 60, then we can rotate right around 70.

66

0

36

Page 37: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

It’s sometimes even more complicated

30

15

70 60

0

1 0

0

Insert 64… do we need a double or a single rotation? A.  Double B.  Single C.  No rotation needed

66

0

37

Page 38: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Where is the tree out of balance?

30

15

70 60

66

64

A.  Node 30 B.  Node 66 C.  Node 60 D.  Node 64

38

Page 39: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Rotate right around 66 to make a straight line

30

15

70 60

+2

1 -1

0

66

0

64

0

39

Page 40: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Rotate right around 66 to make a straight line

30

15

70 60

1

66

64

40

Page 41: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Rotate right around 66 to make a straight line

30

15

70

60

1

66

64

UH OH! Where do we put 64?? Are we stuck?

41

Page 42: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Rotate right around 66 to make a straight line

30

15

70

60

0

1 0

0

66

0

64

0

Will 64 always reattach there?

A.  No, sometimes this doesn’t work B.  Yes, this will always work

42

Page 43: CSE 100: AVL TREES · Balanced Binary Trees • Deterministic Balancing • Change insert and delete operations to ensure that the tree always stays “balanced”, example AVL trees,

Finishing the rotation to balance the tree

30

15 70

60 -1

0

0

0

66

0

64

0

43