Data Structures in Java Lecture 10: AVL Trees. 10/12/2015 Daniel - - PowerPoint PPT Presentation

data structures in java
SMART_READER_LITE
LIVE PREVIEW

Data Structures in Java Lecture 10: AVL Trees. 10/12/2015 Daniel - - PowerPoint PPT Presentation

Data Structures in Java Lecture 10: AVL Trees. 10/12/2015 Daniel Bauer Balanced BSTs Balance condition: Guarantee that the BST is always close to a complete binary tree (every node has exactly two or zero children). Then the height of


slide-1
SLIDE 1

Data Structures in Java

Lecture 10: AVL Trees.

10/12/2015 Daniel Bauer

slide-2
SLIDE 2

Balanced BSTs

  • Balance condition: Guarantee that the BST is

always close to a complete binary tree (every node has exactly two or zero children).

  • Then the height of the tree will be O(log N) and all

BST operations will run in O(log N).

slide-3
SLIDE 3

AVL Tree Condition

  • An AVL Tree is a Binary Search Tree in which the

following balance condition holds after each

  • peration:
  • For every node, the height of the left and right

subtree differs by at most 1.

1 2 4 8 5 3 1 2 4 5

7

3 7 8

2 1 3 1 2 1 1 1 1 2 3 1

not an AVL tree

slide-4
SLIDE 4
  • Height of an AVL tree is at most 


~ 1.44 log(N+2)-1.328 = O(log N)

  • How to maintain the balance condition?
  • Rebalance the tree after each change (insertion
  • r deletion).
  • Rebalancing must be cheap.

AVL Trees

slide-5
SLIDE 5

“Outside” Imbalance

k2

x z

k1

y

k2

z x

k1

y

node k2 violates the balance condition left subtree of left child too high right subtree of right child too high

  • Solution: Single rotation
slide-6
SLIDE 6

“Inside” Imbalance

k2

y z

k1

x

k2

Y x

k1

z

node k2 violates the balance condition right subtree of left child too high left subtree of right child too high

  • Solution: Double rotation
slide-7
SLIDE 7

Single Rotation

x y

k1

Maintain BST property:

  • x is still left subtree of k1.
  • z is still right subtree of k2.
  • For all values v in y: k1 < v < k2


so y becomes new left subtree of k2.

k2

z

slide-8
SLIDE 8

Single Rotation

x y

k1

Maintain BST property:

  • x is still left subtree of k1.
  • z is still right subtree of k2.
  • For all values v in y: k1 < v < k2


so y becomes new left subtree of k2.

k2

z

slide-9
SLIDE 9

Single Rotation

x y

k1

Maintain BST property:

  • x is still left subtree of k1.
  • z is still right subtree of k2.
  • For all values v in y: k1 < v < k2


so y becomes new left subtree of k2.

k2

z

Modify 3 references:

  • k2.left = k1.right
  • k1.right = k2
  • parent(k2).left = k1 or 


parent(k2).right = k1 or 


slide-10
SLIDE 10

Maintaining Balance in an AVL Tree

  • Assume the tree is balanced.
  • After each insertion, find the lowest node k that violates

the balance condition (if any).

  • Perform rotation to re-balance the tree.
  • Rotation maintains original height of subtree under k

before the insertion. No further rotations are needed.

slide-11
SLIDE 11

Single Rotation Example

3

insert(3)

slide-12
SLIDE 12

Single Rotation Example

2 3

insert(3) insert(2)

slide-13
SLIDE 13

Single Rotation Example

1 2 3

insert(3) insert(2) insert(1)

3

rotate_left(3)

slide-14
SLIDE 14

Single Rotation Example

1 2 3

insert(3) insert(2) insert(1) rotate_left(3)

slide-15
SLIDE 15

Single Rotation Example

1 2 3 4

insert(3) insert(2) insert(1) rotate_left(3) insert(4)

slide-16
SLIDE 16

Single Rotation Example

1 2 3 4

insert(3) insert(2) insert(1) rotate_left(3) insert(4)

5

insert(5) rotate_right(3)

3

slide-17
SLIDE 17

Single Rotation Example

1 2 3 4

insert(3) insert(2) insert(1) rotate_left(3) insert(4)

5

insert(5) rotate_right(3)

slide-18
SLIDE 18

Single Rotation Example

1 2 3 4

insert(3) insert(2) insert(1) rotate_left(3) insert(4)

5

insert(5) rotate_right(3)

6

insert(6) rotate_right(2)

2

slide-19
SLIDE 19

Single Rotation Example

1 2 3 4

insert(3) insert(2) insert(1) rotate_left(3) insert(4)

5

insert(5) rotate_right(3)

6

insert(6) rotate_right(2)

slide-20
SLIDE 20

Single Rotation Example

1 2 3 4

insert(3) insert(2) insert(1) rotate_left(3) insert(4)

5

insert(5) rotate_right(3)

6

insert(6) rotate_right(2)

7

insert(7) rotate_right(5)

5

slide-21
SLIDE 21

Single Rotation Example

1 2 3 4

insert(3) insert(2) insert(1) rotate_left(3) insert(4)

5

insert(5) rotate_right(3)

6

insert(6) rotate_right(2)

7

insert(7) rotate_right(5)

slide-22
SLIDE 22

Single Rotation does not work for “Inside” Imbalance

x y

k1 k2

z

slide-23
SLIDE 23

Single Rotation does not work for “Inside” Imbalance

x y

k1 k2

z

Result is not an AVL tree. Now k1 is violates the balance condition. 
 Problem: Tree y cannot move and it is too high.

slide-24
SLIDE 24

Double Rotation (1)

x

k1 k3

z

  • y is non-empty (imbalance due to insertion into y or deletion

from z)

  • so we can view y as a root and two sub-trees.

y

slide-25
SLIDE 25

Double Rotation (1)

x

k1 k3

z

  • y is non-empty (imbalance due to insertion into y or deletion

from z)

  • so we can view y as a root and two sub-trees.

k2 yl yr

  • either yl or yr is two levels deeper than z (or both are empty).
slide-26
SLIDE 26

Double Rotation (2)

x

k2 k3

z

k1 yl yr

Maintain BST property:

  • x is still left subtree of k1.
  • z is still right subtree of k3.
  • For all values v in yl: k1 < v < k2


so yl becomes new right subtree of k1.

  • For all values w in yr: k2 < w < k3


so yr becomes new left subtree of k3.

slide-27
SLIDE 27

Double Rotation (2)

x

k2 k3

z

k1 yl yr

Maintain BST property:

  • x is still left subtree of k1.
  • z is still right subtree of k3.
  • For all values v in yl: k1 < v < k2


so yl becomes new right subtree of k1.

  • For all values w in yr: k2 < w < k3


so yr becomes new left subtree of k3.

slide-28
SLIDE 28

Double Rotation (2)

x

k2 k3

z

k1 yl yr

These are actually two single rotations: First at k1, then at k3.

slide-29
SLIDE 29

Double Rotation (2)

x

k2 k3

z

k1 yl yr

These are actually two single rotations: First at k1, then at k3.

slide-30
SLIDE 30

Double Rotation (2)

x

k2 k3

z

k1 yl yr

These are actually two single rotations: First at k1, then at k3.

slide-31
SLIDE 31

Double Rotation (3)

Modify 5 references:

  • parent(k3).left = k2 or 


parent(k3).right = k2

  • k2.left = k1
  • k2.right = k3
  • k1.right = root(yl)
  • k3.left = root(yr)

x

k2 k3

z

k1 yl yr

slide-32
SLIDE 32

Double Rotation Example

1 2 3 4 5 6 7

slide-33
SLIDE 33

Double Rotation Example

1 2 3 4 5 6 7

insert(16)

16

slide-34
SLIDE 34

Double Rotation Example

1 2 3 4 5 6 7

insert(16)

16 15

7

insert(7) rotate(7)

slide-35
SLIDE 35

Double Rotation Example

1 2 3 4 5 6 7

insert(16)

16 15

7

insert(7) rotate(7)

z

k3 k1

x

k2 yl yr

slide-36
SLIDE 36

Double Rotation Example

1 2 3 4 5 6 7

insert(16)

16 15

insert(7) rotate(7)

14

6

insert(14) rotate(6)

slide-37
SLIDE 37

Double Rotation Example

1 2 3 4 5 6 7

insert(16)

16 15

insert(7) rotate(7)

14

6

insert(14) rotate(6)

z

k3 k1

x

k2 yl yr

slide-38
SLIDE 38

Double Rotation Example

1 2 3 4 5 6 7

insert(16)

16 15

insert(7) rotate(7)

14

insert(14) rotate(6)