AVL Trees

16
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property : For every node, the heights of the left and right subtrees differ at most by one – Each node contains a value (-1, 1, 0) indicating which subtree is "heavier”

description

AVL Trees. AVL (Adel`son-Vel`skii and Landis) tree = A BST With the property : For every node, the heights of the left and right subtrees differ at most by one Each node contains a value (-1, 1, 0) indicating which subtree is "heavier”. Balance Factor. - PowerPoint PPT Presentation

Transcript of AVL Trees

Page 1: AVL Trees

AVL Trees

• AVL (Adel`son-Vel`skii and Landis) tree = – A BST– With the property: For every node, the heights

of the left and right subtrees differ at most by one

– Each node contains a value (-1, 1, 0) indicating which subtree is "heavier”

Page 2: AVL Trees

Balance Factor

– Each node is marked with a balance factor indicating which subtree is "heavier”

– Balance Factor: height (right subtree) minus height (left subtree)

– A balanced tree (-1, 1, 0)

1

1-1

0 0 1

0

Page 3: AVL Trees
Page 4: AVL Trees

AVL Implementation issues:

– Insert and Delete are modified. They restructure the tree to make it balanced (if necessary)

Page 5: AVL Trees

Fixing Imbalances

An imbalance is detected when the height difference between two subtrees of a node becomes greater than 1 or smaller than -1

There are two cases of imbalances:

2

-1

0

-2

1

0

2

1

0

-2

-1

0or

or

CASE 1 CASE 2

Imbalance caused by inserting node in left subtree of left child or right subtree of right child (i.e., insertion occurs on the “outside”)

Imbalance caused by inserting node in right subtree of left child or left subtree of right child (i.e., insertion occurs on the “inside”)

Page 6: AVL Trees

Fixing Imbalances (cont’d)

Fixing an imbalance is done by rotating the tree There are two types of rotation:

single rotation for CASE 1 imbalances

double rotation for CASE 2 imbalances consists of two single rotations

The rotations must always preserve the BST property

Page 7: AVL Trees

Fixing Imbalances: Case 1

6

4

2 6

4

2

node with imbalance

A B

C

D

A B C D

This is a single right rotation. A single left rotation is symmetric.

right rotatenode 4 about 6

Page 8: AVL Trees

Fixing Imbalances: Case 1 (cont’d)

left rotate node Q about PThis is a single left rotation

Page 9: AVL Trees

Fixing Imbalances: Case 2

6

2

4

6

4

2

node with imbalance

A

B C

D

A B C D

STEP 1: left rotate node 4 about 2

6

4

2

A B

C

D

STEP2: right rotate node 4 about 6

node with imbalance

Page 10: AVL Trees

Fixing Imbalances: Case 2 (cont’d)

STEP 1: Right rotate R about Q

STEP 2: Left rotate R about P Note that P can be part of a larger AVL tree; it can be a child of some other node in the tree

Page 11: AVL Trees

AVL Trees: Insert

1. Insert the node as in a BST2. Starting at the newly inserted node, travel

up the tree (towards the root), updating the balances along the way

The first node for which the balance factor becomes +/- 2 (if any) is the root P of a subtree that needs to be rebalanced

EXAMPLE: Insert the items

4, 2, 1, 5, 6, 7, 8, 18, 17, 16, 13, 12

into an initially empty AVL tree

Page 12: AVL Trees

AVL Trees: Insert, rebalance locally

– If a node is entered into the larger AVL tree and P becomes imbalanced, after restoring the balance of P, does extra work need to be done to the predecessor(s) of P?• Fortunately not. The new height of P (after the

rotation) is exactly the same as the original height of P prior to the insertion that caused P’s imbalance. Thus no further updating of heights on the path to the root is needed, and consequently no further rotations are needed • Does it mean that the tree can no grow in the height

at all?

Page 13: AVL Trees

AVL Trees: Delete

1. Delete the node as in a BST2. Starting at the parent of the deleted node,

travel up the tree (towards the root), updating the balances along the way

a. If the tree becomes unbalanced, decide which rotation needs to be performed, rotate the tree and update the balances

b. Keep traveling towards the root, checking the balances. You may need to rotate again

Page 14: AVL Trees

AVL Trees: Efficiency

• It can be shown that the worst case height of an AVL tree is at most 44% larger than the minimum possible for a BST (i.e. approximately 1.44lgn)

Page 15: AVL Trees

Time Complexity of Basic AVL Tree Operations

• Insert– Maximum possible number of rotations = 1

• Delete– Maximum possible number of rotations = lg(n)

• Worst case times– Search: O(lgn)– Insert: O(lgn)– Delete: O(lgn)

Page 16: AVL Trees

Other Methods

• AVL trees maintain balance of BSTs while they are being created via insertions of data

• An alternative approach is to have trees that readjust themselves when data is accessed, making often accessed data items move to the top of the tree (splay trees)