Data Structures in Java
Lecture 10: AVL Trees.
10/12/2015 Daniel Bauer
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
Lecture 10: AVL Trees.
10/12/2015 Daniel Bauer
always close to a complete binary tree (every node has exactly two or zero children).
BST operations will run in O(log N).
following balance condition holds after each
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
~ 1.44 log(N+2)-1.328 = O(log N)
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
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
x y
k1
Maintain BST property:
so y becomes new left subtree of k2.
k2
z
x y
k1
Maintain BST property:
so y becomes new left subtree of k2.
k2
z
x y
k1
Maintain BST property:
so y becomes new left subtree of k2.
k2
z
Modify 3 references:
parent(k2).right = k1 or
the balance condition (if any).
before the insertion. No further rotations are needed.
3
insert(3)
2 3
insert(3) insert(2)
1 2 3
insert(3) insert(2) insert(1)
3
rotate_left(3)
1 2 3
insert(3) insert(2) insert(1) rotate_left(3)
1 2 3 4
insert(3) insert(2) insert(1) rotate_left(3) insert(4)
1 2 3 4
insert(3) insert(2) insert(1) rotate_left(3) insert(4)
5
insert(5) rotate_right(3)
3
1 2 3 4
insert(3) insert(2) insert(1) rotate_left(3) insert(4)
5
insert(5) rotate_right(3)
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
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)
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
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)
x y
k1 k2
z
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.
x
k1 k3
z
from z)
y
x
k1 k3
z
from z)
k2 yl yr
x
k2 k3
z
k1 yl yr
Maintain BST property:
so yl becomes new right subtree of k1.
so yr becomes new left subtree of k3.
x
k2 k3
z
k1 yl yr
Maintain BST property:
so yl becomes new right subtree of k1.
so yr becomes new left subtree of k3.
x
k2 k3
z
k1 yl yr
These are actually two single rotations: First at k1, then at k3.
x
k2 k3
z
k1 yl yr
These are actually two single rotations: First at k1, then at k3.
x
k2 k3
z
k1 yl yr
These are actually two single rotations: First at k1, then at k3.
Modify 5 references:
parent(k3).right = k2
x
k2 k3
z
k1 yl yr
1 2 3 4 5 6 7
1 2 3 4 5 6 7
insert(16)
16
1 2 3 4 5 6 7
insert(16)
16 15
7
insert(7) rotate(7)
1 2 3 4 5 6 7
insert(16)
16 15
7
insert(7) rotate(7)
z
k3 k1
x
k2 yl yr
1 2 3 4 5 6 7
insert(16)
16 15
insert(7) rotate(7)
14
6
insert(14) rotate(6)
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
1 2 3 4 5 6 7
insert(16)
16 15
insert(7) rotate(7)
14
insert(14) rotate(6)