avl trees - university of central florida

21
AVL TREES COP 3502

Upload: others

Post on 03-Feb-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AVL TREES - University of Central Florida

AVL

TREES

COP 3502

Page 2: AVL TREES - University of Central Florida

AVL Trees

We know that the search time for a node in a balanced binary search tree is O(log n)

We’re dividing our search space in half each time we search the left or right branch.

5

8 2

1 4 3 7

Page 3: AVL TREES - University of Central Florida

AVL Trees

But if trees get out of balance, or have deep search paths Their search performance deteriorates

In the worst case instead of having an O(log n) search time

The search time is O(n)

Page 4: AVL TREES - University of Central Florida

So what we want is a tree that stays relatively balanced so that we can maintain the O(log n) search time, BUT doesn’t require too much work in maintaining the

balance so that we can still have O(log n) insertion time.

2 Russian mathematicians, Adelson-Velski and Landis, created this type of almost balanced trees – known as AVL trees

AVL Trees

Page 5: AVL TREES - University of Central Florida

AVL Trees

The AVL tree property is that for any node N, the height of N’s left and right subtrees must be equal or differ by 1.

height of a binary tree: the length of the longest path from the root to a leaf. (the height of an empty tree is -1) (the height of a leaf is 0)

5

8 2

1

5

8 2

1 4

3

7

Page 6: AVL TREES - University of Central Florida

AVL Trees

The AVL tree property is that for any node N, the height of N’s left and right subtrees must be equal or differ by 1.

The Balance Factor is the difference in heights of the left and right subtrees at any node.

height of a binary tree: the length of the longest path from the root to a leaf. (the height of an empty tree is -1) (the height of a leaf is 0)

5

8 2

1

5

8 2

4

3

7

Page 7: AVL TREES - University of Central Florida

Non-AVL Trees

C

B

A

C

A

B

30

15 50

4 27

20

30

15

50

4

27

20

Page 8: AVL TREES - University of Central Florida

AVL Trees

Now that we know what an AVL tree is, now the question is how do we maintain this AVL tree

property when new nodes are inserted or deleted?

When an imbalance is introduced to a tree, it is localized to 3 nodes and their 4 subtrees. Denote the 3 nodes as A, B, C in their inorder listing.

Here are the 4 possibilites of the imbalances that could occur:

C

B

A

C

A

B

A

B

C

A

C

B

Page 9: AVL TREES - University of Central Florida

AVL Trees

All 4 imbalance cases can be solved by converting to the following tree:

C

B

A

C

A

B

A

B

C

A

C

B

A

B

C

Page 10: AVL TREES - University of Central Florida

C

B

A

C

A

B

A

B

C

A

C

B

A

B

C A

B

C

C

B

A

A

B

C

A

B

C

A

B

C

Case 1: Case 2: Case 3: Case 4: Single Rotation Double Rotation Double Rotation Single Rotation

Page 11: AVL TREES - University of Central Florida

AVL Tree Insert

So now the question is, how can we use these rotations to actually perform an insert on an AVL tree? Here are basic steps:

Do a normal binary search tree insert

Restore the tree based on this new leaf node, steps for restoration: 1) Calculate the heights of the left and right subtrees, use this to set the

potentially new height of the node.

2) If they are within one of each other, recursively restore the parent node.

3) If not, then perform the appropriate rotations on that particular node, THEN recursively restore the heights of the parent node.

Note: No recursive call is made if the node in question is the root node and has no parents.

Note: one rebalancing will always do the trick, though we must make the recursive calls to move up the tree so that the heights stored at each node are properly recalculated.

Page 12: AVL TREES - University of Central Florida

AVL Tree Insert Examples The most simple insert into an AVL Tree that causes a

rebalance is inserting a 3rd node into an AVL tree that creates a tree of height 2. In this example, consider inserting the value 5:

9

7

5 5

7

9 A

B

C

Page 13: AVL TREES - University of Central Florida

AVL Tree Insert Examples In this example, consider inserting the value 20:

In this situation, the nodes 27 and 15 are balanced and we don’t discover an imbalance until we trace up to 30. At this point, we label the nodes A, B and C based on our trace up the tree. The three values we passed were 27, 15 and 30, respectively. Thus, our labels are A = 15, B = 27, and C = 30.

30

15 50

4 27

20

A

B

C 30

15

50

4

27

20

A

B

C

30 15

50 4

27

20

A

B

C

Page 14: AVL TREES - University of Central Florida

AVL Tree Insert Examples In this example, consider inserting the value 46:

32

16 48

8 24 40 56

36 44 52 60

46

A

B

C

Page 15: AVL TREES - University of Central Florida

AVL Tree Insert Examples In this example, consider inserting the value 46:

32

16

48 8 24

40

56

36

44

52 60 46

A

B

C

Page 16: AVL TREES - University of Central Florida

AVL Tree Insert Examples In this example, consider inserting the value 46:

32

16

48

8 24

40

56 36 44

52 60 46

A

B

C

Page 17: AVL TREES - University of Central Florida

AVL Tree Insert Examples In this example, consider inserting the value 61:

32

16 48

8 24 40 56

36 44 52 60

58 62

61

A

B

C

4

Page 18: AVL TREES - University of Central Florida

AVL Tree Insert Examples In this example, consider inserting the value 61:

32

16 48

8 24 40

56 36 44

52

60

58

62

61

A

B

C 4

Page 19: AVL TREES - University of Central Florida

AVL Tree example

Show the resulting tree after inserting 15 into the tree below:

30

20

31 10 25

36

43

4 18

15

29

Page 20: AVL TREES - University of Central Florida

AVL Tree example - ANSWER

Show the resulting tree after inserting 15 into the tree below:

30

20

31 10 25

36

43

4 18

15

29

A

B

C

Page 21: AVL TREES - University of Central Florida

AVL Tree example - ANSWER

Show the resulting tree after inserting 15 into the tree below:

30

20

31

10

25 36

43

4 18

15 29

A

B

C