Top Banner
AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)
30

AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

AVL Trees

Balanced Binary Search Trees(not covered in book, but related to

pp 165-169)

Page 2: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

AVL Trees

• First-invented self-balancing binary search tree

• Named after its two inventors, 1. G.M. Adelson-Velsky and

2. E.M. Landis, – published it in their 1962 paper "An algorithm

for the organization of information."

Page 3: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

AVL Properties

• First, its a binary search tree...

• L <= P and P <= R

P

L R

Page 4: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Question?

• Is this a binary tree search tree?

3 9

5

15 21

17

28 34

32

43 51

46

52 54

53

58 68

60 71 79

7712 35 56

29 70

50

Page 5: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

BTW, Binary Heap Properties

• Min Heap• P <= L and P <= R

• Max Heap• P >= L and P >= R

P

L R

Page 6: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

AVL Properties

• An AVL tree is a balanced binary tree

• To understand balance we need to understand the notion of Tree Height

55

32 71

64 86 Height 0

Height 1

Height 2

Page 7: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

AVL Properties

• By default, nodes with no children have a height of Height of 0.

55

32 71

64 86 Height 0

Height 1

Height 2

Height 0

Height 0

Page 8: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

AVL Properties

• But, we must also understand the concept of Sub-trees

55

32 71

64 86

Height 0 Height 1

Height 2

Height 0 Height 0

sub-tree L has a height of 0 sub-tree R has a

height of 1

Height = max(L.height, R.height) + 1

Page 9: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

AVL Properties

• Also empty sub-trees have a Height of -1

44

58

91

Height = 2 = max(0, 1) + 1

Height = 0 = max(-1,-1) + 1

Height = max(L.height, R.height) + 1

Height = 1 = max(-1, 0) + 1

Page 10: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

AVL Properties

• Anyway, the AVL Balance Property is as follows...

• For ALL nodes, the Height of the Left and Right Sub-trees can only differ by 1.

P A Node

L R

1.. heightRheightL

Page 11: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

AVL Properties

• Wouldn’t this be a better Balance property?

• For ALL nodes, the Height of the Left and Right Sub-trees must be equal!

P A Node

L R

0.. heightRheightL

Page 12: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

AVL Properties

• For ALL nodes, the Height of the Left and Right Sub-trees must be equal!

1

3

715

31

Page 13: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

AVL Properties

• For ALL nodes, the Height of the Left and Right Sub-trees must be equal!

• Strict Balance Conditions are too restrictive. 1

3

715

31

Page 14: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Question?

• Is this an AVL Tree?

3 9

5

15 21

17

28 34

32

43 51

46

52 54

53

58 68

60 71 79

7712 35 56

29 70

50

Page 15: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Question?

• Is this an AVL Tree?

3 9

5

15 21

17

30 34

32

43 49

46

52 54

53

58 68

60 71 79

7712 35 56

29 70

50

Page 16: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Question?

• Is this an AVL Tree?

3 9

5

15 21

17

30 34

32

43 49

46

52 54

53

58 68

60 71 79

7712 35 56

29 70

50

1 1 1 1 1 1

122 2

3

4

3

Page 17: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Question?

• Is this an AVL Tree?

3

5 17 32 79

7712 35 56

29 70

50

Page 18: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Question?

• Is this an AVL Tree?

3

5 17 32 79

7712 35 56

29 70

50

1 0 0 0

102 1

3

4

2

0

Page 19: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Question?

• Is this an AVL Tree?

3

5 17 32 79

7712 35 56

29 70

50

1 0 0 0

102 1

3

4

2

00

+1

+1 +1

+1

+1

-1

-10

00 0

Page 20: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Question?

• Is this an AVL Tree?

3 9

5

15 21

17

49

46

52 54

53

58 68

60 71 79

7712 35 56

29 70

50

Page 21: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Question?

• No

3 9

5

15 21

17

49

46

52 54

53

58 68

60 71 79

7712 35 56

29 70

50

Page 22: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Question?

• Did this fix the problem?

3 9

5

15 21

17 49

46

52 54

53

58 68

60 71 79

7712

35

56

29 70

50

Page 23: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Question?• Is this an AVL Tree?

3 9

5

15 21

17

28 34

32

43 51

46

52 54

53

58 68

60 71 79

7712 35 56

29 70

50

69

Page 24: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Question?• Is this an AVL Tree?

3 9

5

15 21

17

28 34

32

43 51

46

52 54

53

58 68

60 71 79

7712 35 56

29 70

50

690

-1

-1

-1

+2

Page 25: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Question?• Is this an AVL Tree?

3 9

5

15 21

17

28 34

32

43 51

46

52 54

53

58 68

60 71 79

7712 35 56

29 70

50

690

-1

-1

-1

+2

Page 26: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Question?• Is this an AVL Tree?

3 9

5

15 21

17

28 34

32

43 51

46

52 54

53

58 68

60

71 79

7712 35 56

29

70

50

690

-1

-1

-1

+2

Page 27: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Question?• Is this an AVL Tree?

3 9

5

15 21

17

28 34

32

43 51

46

52 54

53 58 68

60

71 79

77

12 35 56

29

70

50

69

0

Page 28: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Correcting Imbalance

1. After every insertion

2. Check to see if an imbalance was created.• All you have to do backtrack up the tree

3. If you find an imbalance, correct it.

4. As long as the original tree is an AVL tree, there are only 4 types of imbalances that can occur.

Page 29: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Imbalances

• Left-Left (Single Rotation)

• Right-Right(Single Rotation)

• Left-Right (Double Rotation)

• Right-Left(Double Rotation)

Page 30: AVL Trees Balanced Binary Search Trees (not covered in book, but related to pp 165-169)

Illustration

• http://www.eli.sdsu.edu/courses/fall96/cs660/notes/avl/avl.html