21
Tree Balancing: AVL Trees Dr. Yingwu Zhu

Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

Embed Size (px)

Citation preview

Page 1: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

Tree Balancing: AVL Trees

Dr. Yingwu Zhu

Page 2: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

Recall in BST The insertion order of items determine

the shape of BST Balanced: search T(n)=O(logN) Unbalanced: T(n) = O(n)

Key issue: A need to keep a BST balanced! Introduce AVL trees, by Russian

mathematican

Page 3: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

AVL Tree Definition First, a BST Second, height-balance property:

balance factor of each node is 0, 1, or -1 Question: what is balance factor?

BF = Height of the left subtree – height of the right subtree

Height: # of levels in a subtree/tree

Page 4: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

Determine balance factor

A

B D

GE

HF

C

Page 5: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

ADT: AVL Trees

Data structure to implement

Balance factor Data

Left Right

Page 6: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

ADT: AVL Trees

Basic operations Constructor, search, travesal, empty Insert: keep balanced! Delete: keep balanced! See P842 for class template Similar to BST

Page 7: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

Example

RI

PA

Insert “DE”, what happens? Need rebalancing?

Page 8: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

Basic Rebalancing Rotation Single rotation:

Right rotation: the inserted item is on the left subtree of left child of the nearest ancestor with BF of 2

Left rotation: the inserted item is on the right subtree of right child of the nearest ancestor with BF of -2

Double rotation Left-right rotation: the inserted item is on the right subtree of

left child of the nearest ancestor with BF of 2

Right-left rotation: the inserted item is on the left subtree of right child of the nearest ancestor with BF of -2

Page 9: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

How to perform rotations Rotations are carried out by resetting

links

Two steps: Determine which rotation Perform the rotation

Page 10: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

Right Rotation Key: identify the nearest ancestor of

inserted item with BF +2 A: the nearest ancestor. B: left child Step1: reset the link from parent of A to B Step2: set the left link of A equal to the

right link of B Step3: set the right link of B to A Examples

Page 11: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

A

B

L(A)

L(B) R(B)

new

h

Page 12: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

Exercise #1

10

8

96

20How about insert 4

Page 13: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

Left Rotation

Step1: reset the link from parent of A to B

Step2: set the right link of A to the left link of B

Step3: set the left link of B to A Examples

Page 14: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

Example

A

B

L(A)

L(B) R(B)

new

h

Page 15: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

Exercise #2

7

5 15

10 20

18What if insert

Page 16: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

Exercise #3

12

8 16

4 10 14

2 6

1How about insert

Page 17: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

Double Rotations

Left-right rotation Right-left rotation How to perform?

1. Rotate child and grandchild nodes of the ancestor

2. Rotate the ancestor and its new child node

Page 18: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

Basic Rebalancing Rotation

Double rotation Left-right rotation: the inserted

item is on the right subtree of left child of the nearest ancestor with BF of 2

Right-left rotation: the inserted item is on the left subtree of right child of the nearest ancestor with BF of -2

Page 19: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

Exercise #4

4

2 6

1 3 5

16

7

15What if insert

Page 20: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:
Page 21: Tree Balancing: AVL Trees Dr. Yingwu Zhu. Recall in BST The insertion order of items determine the shape of BST Balanced: search T(n)=O(logN) Unbalanced:

Summary on rebalancing

The key is you need to identify the nearest ancestor of inserted item

Determine the rotation by definition

Perform the rotation