AVL TREES

Post on 25-Feb-2016

54 views 0 download

description

AVL TREES. COP 3502. 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. 7. 3. AVL Trees. - PowerPoint PPT Presentation

Transcript of AVL TREES

AVL TREES

COP 3502

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

82

1 4 37

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)

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

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

82

1

5

82

1 4

3

7

Height = 2

H = 0 H = -1

Height = 3

H = 2 H = 1

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

82

1

5

82

4

3

7

BF = 1

H = 1 H = 0

BF = 2

H = 1H = -1

Non-AVL Trees

C

B

A

C

A

B

30

15 50

4 27

20

30

15

50

4

27

20

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

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

C

BA

C

A

B

A

BC

A

C

B

A

B

CA

B

C

CB

A

A

B

C

A

BC

A

B

C

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

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 insertRestore 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.

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

75

H = 1H = -1

H = 0H = -1 5

7

9A

B

C

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.

H = 2H = 0

30

15 50

4 27

20H = 0 H = -1

H = 0 H = 1

A

B

C 30

15

50

4

27

20

A

B

C

3015

504

27

20

A

B

C

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

H = 1 H = 3

32

16 48

8 24 40 56

36 44 52 60

46

A

B

C

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

32

16

488 24

40

56

36

44

52 6046

A

B

C

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

32

16

48

8 24

40

563644

52 6046

A

B

C

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

H = 2

32

16 48

8 24 40 56

36 44 52 60

58 62

61

A

B

C

4H = 0

BF = 2

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

32

16 48

8 24 40

5636 44

52

60

58

62

61

A

B

C4

AVL Tree example

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

30

20

3110 25

36

43

4 18

15

29

AVL Tree example - ANSWER

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

30

20

3110 25

36

43

4 18

15

29

H = 3 H = 1

A

B

CImbalanced, Balance Factor = 2

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