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)
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Transcript of 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)

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

AVL Properties

• First, its a binary search tree...

• L <= P and P <= R

P

L R

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

BTW, Binary Heap Properties

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

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

P

L R

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

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

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

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

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

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

AVL Properties

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

1

3

715

31

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

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

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

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

Question?

• Is this an AVL Tree?

3

5 17 32 79

7712 35 56

29 70

50

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

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

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

Question?

• No

3 9

5

15 21

17

49

46

52 54

53

58 68

60 71 79

7712 35 56

29 70

50

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

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

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

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

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

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

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.

Imbalances

• Left-Left (Single Rotation)

• Right-Right(Single Rotation)

• Left-Right (Double Rotation)

• Right-Left(Double Rotation)

Illustration

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