avl trees

32
AVL Trees

Upload: nasim-alvarez

Post on 02-Jan-2016

26 views

Category:

Documents


1 download

DESCRIPTION

AVL Trees. Tree Height = 3 ( Length of longest Path). 0. K. G. 1. P. H. X. D. 2. 3. A. F. AVL Tree. First Balanced Binary Search Tree G.M. Adelson-Velskii / E.M.Landis 1962 BST w/ balance condition Ensures tree depth is O(log n). AVL Tree Definition. - PowerPoint PPT Presentation

TRANSCRIPT

AVL Trees

Tree Height = 3(Length of longest Path)

K

G P

D X

A F

H

3

1

2

0

AVL Tree

First Balanced Binary Search Tree G.M. Adelson-Velskii / E.M.Landis 1962 BST w/ balance condition Ensures tree depth is O(log n)

AVL Tree Definition

An AVL tree is a BST such that for any node in the tree:– the height of the left and right subtrees – differ at most by 1

– Height of a subtree with two nodes is 1– Height of a subtree with one node is 0– The height of an empty subtree is -1

L/R Sub-Tree Heights

K

G P

D X

A F

H0

01 0

2

-1-1 -1-1

-1 -1

-1

-1 -1

We will say sub-trees without a a root node have a height of 0.

0

1

Tree Balance Info Values(Height of L. Subtree - Height of R. Subtree)

K

G P

D X

A F

H

(Tree is balanced based on AVL def.)

0 0

00

1

0

-1

1

Insert Operation Only nodes in path from insertion point to root have their sub-trees

altered

– Insert a new node as with a BST

– Follow path from insertion point to root updating balance information

– At any node, the new balance may violate the AVL condition

– Re-balancing the deepest node

Re-balancing deepest node guarantees the entire tree satisfies AVL

4 AVL Insertion Violations

Insert left child of X’s left subtree LL

Insert right child of X’s right subtree RR

Insert left child of X’s right subtree LR

Insert right child of X’s left subtree RL

Example AVL Trees

K

G P

D X

A F

H

(Balance info above nodes)

0 0

00

1

0

-1

1

RR/LL Violations Fixed by Single Rotation

Insertion in Left Subtree of Left Child of X Insert in Right Subtree of Right Child of X

Switch parent (X) and child Maintain search order

Simple LL & RR Rotations

http://www.cgc.cs.jhu.edu/~jkloss/htmls/structures/avltree.html http://www.seanet.com/users/arsen/avltree.html BAD http://www.cse.iitk.ac.in/users/dsrkg/cs210/applets/AVLtree/avl.html

K

G

D K

G

D

LL & RR w/ Children

What happens when rotated nodes have children?

Example AVL Trees

K

G P

D X

A F

H

Try to Insert B

0 0

00

1

0

-1

1

AVL Insertion

K

G P

D X

A F

H

-1 0

01

2

0

-1

2

B 0

G and K are now unbalancedG is the deepest node

This is still a LL rotation beginning at node G

AVL Single Rotation

K

G P

D X

A F

H

B

Note if D node goes up and G goes down, G becomes D's right child. What happens to F?It becomes G's left child.

Completed Insertion with Rotation

K

D P

A X

B

G

0

11

2

0

1

3

H 0F

C++ Implementation of LL Single Rotation

// For AVL trees, this is a single LL rotation

TreePtrType RotateWithLeftChild(TreePtrType k2)

{ TreePtrType k1 = k2->left; k2->left = k1->right; k1->right = k2; return k1;}

C++ Implementation of RR Single Rotation

// For AVL trees, this is a single RR rotation

TreePtrType RotateWithRightChild(TreePtrType k1)

{ TreePtrType k2 = k1->right; k1->right = k2->left; k2->left = k1; return k2;}

RL/LR Violations Fixed by Double Rotation

Why not a single rotation?

Another Case

K

G P

D X

A F

H

What if we try to add E?

0 0

01

2

0

1

3

Harder AVL Insertion

K

G P

D X

A F

H

0 1

01

2

0

1

2

E 0

G and K are now unbalanced

AVL Double Rotation

Call the deepest node that is unbalanced X. Rotate X’s child with its grandchild. Rotate X and its new child.

AVL Double Rotation - First Rotation

K

G P

D X

A F

H

0 1

02

3

0

1

4

E 0

First rotate D and F

AVL Double Rotation - Second Rotation

K

G P

F X

D

A

H

0

02

3

0

1

4

E 0

Second rotate G and F

1

Completed Double Rotation

K

G

PF

XD

A H

1

0 0

2

0

1

3

E0

1

C++ Implementation of LR Double Rotation

// Double rotate binary tree node: first left child// with its right child; then parent with new left child// For AVL trees, this is a LR double rotation

TreePtrType DoubleRotateWithLeftChild(TreePtrType k3){ k3->left = RotateWithRightChild( k3->left ); return RotateWithLeftChild( k3 );}

C++ Implementation of RL Double Rotation

// Double rotate binary tree node: first right child// with its left child; then parent with new right child// For AVL trees, this is a RL double rotation

TreePtrType DoubleRotateWithRightChild(TreePtrType k1){ k1->right = RotateWithRightChild( k1->right ); return RotateWithRightChild( k1 );}

Keeping Track of Heights for AVL Trees

Store in each node: the difference in the heights of its left and right subtrees.

If we increase the height of the left subtree, then add one to the balance.

If we increase the height of the right subtree, then subtract one from the balance.

Determining Subtree to Rotate in AVL Insertions

When returning up the tree, the first node whose adjusted height is -2 or +2 requires rotation.

After rotation, the subtree is the same height as it was before, so no nodes further up towards the root will need to be updated.

Deletion from AVL Trees

Deletion is the same as deletion from a BST. May require rotations as with insertion.

Same type of rotations will balance the tree. Balance is checked after deletion beginning with root

First out of balance node is rebalanced Simplest rotation is selected (e.g., LL over LR)

Rebalancing cost/benefit Balanced binary trees with n nodes will have depth O(log2n).

AVL trees thus guarantee search time will be O(log 2 n).

Overhead involved in rebalancing as the AVL tree• justified when search operations exceed insertionsfaster searches compensate for slower insertions.

Empirical studies indicate that on the average, rebalancing is required for approximately 45 percent of the insertions. Roughly one-half of these rotations require are double rotations.