D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees...

10
D. Christozov COS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete

Transcript of D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees...

Page 1: D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.

D. Christozov COS 221 Intro to CS II AVL Trees

1

AVL Trees: Balanced BST

Binary Search Trees Performance

Height Balanced Trees

Rotation

AVL: insert, delete

Page 2: D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.

D. Christozov COS 221 Intro to CS II AVL Trees

2

Binary Search Trees: Performance

The number of operations in function ‘search’ is equal to the length of the path from the root to the leaf.

Therefore the worst case of search for (unbalanced) BST is

O(n)

The worst case of balanced BST is

O(log2n)

Page 3: D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.

D. Christozov COS 221 Intro to CS II AVL Trees

3

Height-balanced trees

nn-1

A height-balanced binary tree has the property that for each node the difference in the heights of the right and

left child is no larger than one.

Full and Complete binary tree are height-balanced tree.

Page 4: D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.

D. Christozov COS 221 Intro to CS II AVL Trees

4

Height-balanced treesTheorem: The paths in height-balanced binary trees have logarithmic length.1. The largest number of nodes in a binary tree with n levels (of depth n) is 2n-1.2. Let us denote with Mn the minimal number of nodes in height-balanced tree

of depth n.3. M0 = 1M1 = 2Mn+1 = Mn-1 + Mn + 14. Similar to Fibonacci numbers: fn+1 = fn-1 + fn and therefore we can

approximate Mn

5. and n ≈ 1.44 log2 Mn

Page 5: D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.

D. Christozov COS 221 Intro to CS II AVL Trees

5

Rotation

Algorithms to maintain the Height-Balance Propertyin a Binary Search Tree is based on Rotation

X

Y Z

A

B

Let only for the node A the height-balanced tree property is violated and its depth is n+1. Let the right child B is heavier – its depth is n.The depth of X is n-2; and the depth of Y and Z are either n-1 or n-2.

We can assume that rotation to left will restore the height-balance property of the tree.

X Y

Z

A

B Left rotation is performed with two assignments:

A->right(B->left);

B->left(&A)

After Rotation:

Before Rotation:

Page 6: D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.

D. Christozov COS 221 Intro to CS II AVL Trees

6

Rotation

X

Y Z

A

B

X Y

Z

A

B

Left rotation

Abf = 2 Bbf = {0, 1, -1}

  Bbf 0 Bbf < 0

A n+1 n+1

B n n

X n-2 n-2

Y n-2 n-1

Z n-1 n-2

  Bbf 0 Bbf < 0

A n-1 n

B n n+1

X n-2 n-2

Y n-2 n-1

Z n-1 n-2

Heights before rotation Heights after rotation

Bbf =(n-2) - n =-2

Page 7: D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.

D. Christozov COS 221 Intro to CS II AVL Trees

7

AVL Tree

The bf = {-1, 0, 1} for every node in the tree

If bf > 1 and the bf of the root of the right-sub-tree is >=0 single left rotation restores the balance.

If bf > 1 and the bf of the root of the right-sub-tree is <0 to restore the balance double rotation is required:

1. single right rotation for the right sub-tree root; and

2. single left rotation for the current node.

for bf <-1 operation are symmetrical.

Value ={Key, Data}

int bf

left right

bf – balance factor

AVL tree node:

Adelson-Velskii and Landis AVL tree:1. The AVL tree is a Binary Search Tree2. The AVL tree is a Height-Balanced Tree

Page 8: D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.

D. Christozov COS 221 Intro to CS II AVL Trees

8

AVL tree: insertion

11:0

5:0

3:0

2:0 4:0

7:1

8:0

15:0

12:-1

10:0

18:0

16:0 20:0

9:0

11:?new

10:1

12:-2

Unbalanced:Double rotation

Insertion of the new node as right child increments the balance factor

if(left->balFac != oldbf && left->balFac) balFac--;

Update of balance factors follows the way back of the path of insertion.

Page 9: D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.

D. Christozov COS 221 Intro to CS II AVL Trees

9

AVL tree: insertion

1

12:-2

10:0

11:-1

12:-2

2

Double rotation:1: Rotation around the left child

Double rotation:2: Rotation around the unbalanced node.

Direction of rotation depends on the sign of the balance factor:positive left rotation; negative right rotation.

10:0

11:0

12:012:010:1

11:0

12:-2

Page 10: D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.

D. Christozov COS 221 Intro to CS II AVL Trees

10

AVL tree: removal1. Follows the algorithm for removal in a BST2. Updates the balance factors3. Restores balance, when needed

Let us remove 15

9:1

5:-1

7:1

8:0

15:1

18:-1

16:-1

251715.8:-1

15.9:0

11:-1

10:-1 12:0

11.5:0

3:1

2:0 4:-1

3.5

20:1

15.8:0

15.9:0

The parent’s balance factor is incremented: the left sub-tree has one level less

16:0

18:0

9:0Further update of balance factors follows the way back of the path of node removal.(the left-most descendent)

if(left->balFac != oldbf && left->balFac == 0) balFac--;

The left-most descendent of the right sub-tree is 15.8

It’s right child will become the left child of its parent.