Implementation of Dictionaries Using AVL Tree...

Preview:

Citation preview

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

Implementation of Dictionaries Using AVL Tree

Kanimozhi BalaramanIndiana State UniversityTerre Haute IN, USA

kbalaraman@cs.indstate.edu

December 7, 2011

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

CONTENTS

1 AVL tree - Introduction

2 History

3 Statement of Problem

4 Algorithm

5 AVL Tree Properties

6 Height of an AVL tree

7 Rotation

8 Operations

9 Time Complexity

10 References

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

INTRODUCTION

Tree is a way of representing the hierarchical nature of astructure in a graphical form.

Trees are connected graphs that contain no loops.Three basic operations performed using trees

1 Insert2 Search3 Delete

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

INTRODUCTION

Tree is a way of representing the hierarchical nature of astructure in a graphical form.

Trees are connected graphs that contain no loops.Three basic operations performed using trees

1 Insert2 Search3 Delete

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

HISTORY

AVL trees are invented by two Russian mathematiciansAdel’son-Vel’skii and Landis in 1962.

AVL tree was published in the paper named ”An algorithmfor the organisation information”.

AVL tree is a balanced binary search tree which employeesrotation to maintain balance.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

HISTORY

AVL trees are invented by two Russian mathematiciansAdel’son-Vel’skii and Landis in 1962.

AVL tree was published in the paper named ”An algorithmfor the organisation information”.

AVL tree is a balanced binary search tree which employeesrotation to maintain balance.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

HISTORY

AVL trees are invented by two Russian mathematiciansAdel’son-Vel’skii and Landis in 1962.

AVL tree was published in the paper named ”An algorithmfor the organisation information”.

AVL tree is a balanced binary search tree which employeesrotation to maintain balance.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

HISTORY

AVL trees are invented by two Russian mathematiciansAdel’son-Vel’skii and Landis in 1962.

AVL tree was published in the paper named ”An algorithmfor the organisation information”.

AVL tree is a balanced binary search tree which employeesrotation to maintain balance.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

STATEMENT OF PROBLEM

Binary Search tree can become badly unbalanced. So abalanced Binary Search tree like AVL tree can be used.

AVL trees are always balanced.

In worst case Binary Search tree takes O(n) for insert,search and delete operations.

AVL tree takes O(log n) for insert, search and deleteoperations in both best and worst cases.

The height of Binary Search tree is O(n) in worst case.

But the height of AVL tree is O(log n) in worst case.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

STATEMENT OF PROBLEM

Binary Search tree can become badly unbalanced. So abalanced Binary Search tree like AVL tree can be used.

AVL trees are always balanced.

In worst case Binary Search tree takes O(n) for insert,search and delete operations.

AVL tree takes O(log n) for insert, search and deleteoperations in both best and worst cases.

The height of Binary Search tree is O(n) in worst case.

But the height of AVL tree is O(log n) in worst case.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

STATEMENT OF PROBLEM

Binary Search tree can become badly unbalanced. So abalanced Binary Search tree like AVL tree can be used.

AVL trees are always balanced.

In worst case Binary Search tree takes O(n) for insert,search and delete operations.

AVL tree takes O(log n) for insert, search and deleteoperations in both best and worst cases.

The height of Binary Search tree is O(n) in worst case.

But the height of AVL tree is O(log n) in worst case.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

STATEMENT OF PROBLEM

Binary Search tree can become badly unbalanced. So abalanced Binary Search tree like AVL tree can be used.

AVL trees are always balanced.

In worst case Binary Search tree takes O(n) for insert,search and delete operations.

AVL tree takes O(log n) for insert, search and deleteoperations in both best and worst cases.

The height of Binary Search tree is O(n) in worst case.

But the height of AVL tree is O(log n) in worst case.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

STATEMENT OF PROBLEM

Binary Search tree can become badly unbalanced. So abalanced Binary Search tree like AVL tree can be used.

AVL trees are always balanced.

In worst case Binary Search tree takes O(n) for insert,search and delete operations.

AVL tree takes O(log n) for insert, search and deleteoperations in both best and worst cases.

The height of Binary Search tree is O(n) in worst case.

But the height of AVL tree is O(log n) in worst case.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

STATEMENT OF PROBLEM

Binary Search tree can become badly unbalanced. So abalanced Binary Search tree like AVL tree can be used.

AVL trees are always balanced.

In worst case Binary Search tree takes O(n) for insert,search and delete operations.

AVL tree takes O(log n) for insert, search and deleteoperations in both best and worst cases.

The height of Binary Search tree is O(n) in worst case.

But the height of AVL tree is O(log n) in worst case.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

STATEMENT OF PROBLEM

Binary Search tree can become badly unbalanced. So abalanced Binary Search tree like AVL tree can be used.

AVL trees are always balanced.

In worst case Binary Search tree takes O(n) for insert,search and delete operations.

AVL tree takes O(log n) for insert, search and deleteoperations in both best and worst cases.

The height of Binary Search tree is O(n) in worst case.

But the height of AVL tree is O(log n) in worst case.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

ALGORITHM

AVL tree is Balanced Binary Search tree that is eitherempty or has the following properties.

The height of the left and right subtrees differ by atmost1.

The left and right and left subtrees are AVL trees.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

ALGORITHM

AVL tree is Balanced Binary Search tree that is eitherempty or has the following properties.

The height of the left and right subtrees differ by atmost1.

The left and right and left subtrees are AVL trees.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

ALGORITHM

AVL tree is Balanced Binary Search tree that is eitherempty or has the following properties.

The height of the left and right subtrees differ by atmost1.

The left and right and left subtrees are AVL trees.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

ALGORITHM

AVL tree is Balanced Binary Search tree that is eitherempty or has the following properties.

The height of the left and right subtrees differ by atmost1.

The left and right and left subtrees are AVL trees.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

EXAMPLE OF AN AVL TREE

D

B

A

K L

C

G

F

E

N S

M

I

H J

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

Properties of an AVL tree

PROPERTY 1

If the AVL tree has ’n’ nodesThe closest leaf to the root is at level kThen the height of the tree can be atmost 2k-1

PROPERTY 2

If the leaf node closest to the root node is at level k.Then all the nodes at level 1 to k-2 have 2 children

PROPERTY 3

In an AVL tree, if a node has only one child. Then thechild is a leaf node.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

Properties of an AVL tree

PROPERTY 1

If the AVL tree has ’n’ nodesThe closest leaf to the root is at level kThen the height of the tree can be atmost 2k-1

PROPERTY 2

If the leaf node closest to the root node is at level k.Then all the nodes at level 1 to k-2 have 2 children

PROPERTY 3

In an AVL tree, if a node has only one child. Then thechild is a leaf node.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

Height of an AVL tree

Proposition

The height of an AVL tree with ’n’ nodes is O(log n)

Justification

Let n(h) be the minimum number of nodes for an AVL tree ofheight h.

We can find that n(1) = 1 and n(2) = 2For h >= 3, an AVL tree of height ’h’ contains

1 root node,2 one AVL subtree of height h-13 the other AVL subtree of height h-1 or h-2

Therefore n(h) = 1 + n(h-1) + n(h-2)

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

Height of an AVL tree

Proposition

The height of an AVL tree with ’n’ nodes is O(log n)

Justification

Let n(h) be the minimum number of nodes for an AVL tree ofheight h.

We can find that n(1) = 1 and n(2) = 2For h >= 3, an AVL tree of height ’h’ contains

1 root node,2 one AVL subtree of height h-13 the other AVL subtree of height h-1 or h-2

Therefore n(h) = 1 + n(h-1) + n(h-2)

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

Height of an AVL tree

Knowing n(h-1) >= n(h-2), we getSo we can say that,

n(h) = 1 + n(h-1) + n(h-2) > 2n(h-2)n(h) > 2n(h-2)

> 4n(h-4)> 8n(h-6)...> 2in(h − 2i) where i is the number of steps

When i = h/2− 1 we get,n(h) > 2h/2−1n(2) = 2h/2

On taking logarithms, h < 2log n(h)In general ’n’a >= ’n(h)’b

So h is always less than nThus the height of an AVL tree is O(log n)

a’n’ - Number of nodes in the treeb’n(h)’ - minimum possible number of nodes in a tree of height ’h’

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

Height of an AVL tree

Knowing n(h-1) >= n(h-2), we getSo we can say that,

n(h) = 1 + n(h-1) + n(h-2) > 2n(h-2)n(h) > 2n(h-2)

> 4n(h-4)> 8n(h-6)...> 2in(h − 2i) where i is the number of steps

When i = h/2− 1 we get,n(h) > 2h/2−1n(2) = 2h/2

On taking logarithms, h < 2log n(h)In general ’n’a >= ’n(h)’b

So h is always less than nThus the height of an AVL tree is O(log n)

a’n’ - Number of nodes in the treeb’n(h)’ - minimum possible number of nodes in a tree of height ’h’

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

ROTATION

Rotation is the basic mechanism that rebalance theunbalanced tree.

The AVL tree may become unbalanced after insert anddelete operation.

Nodes that are not in the subtree of the item rotatedabout are unaffected by the rotation.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

ROTATION

Rotation is the basic mechanism that rebalance theunbalanced tree.

The AVL tree may become unbalanced after insert anddelete operation.

Nodes that are not in the subtree of the item rotatedabout are unaffected by the rotation.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

ROTATION

Rotation is the basic mechanism that rebalance theunbalanced tree.

The AVL tree may become unbalanced after insert anddelete operation.

Nodes that are not in the subtree of the item rotatedabout are unaffected by the rotation.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

ROTATION

Rotation is the basic mechanism that rebalance theunbalanced tree.

The AVL tree may become unbalanced after insert anddelete operation.

Nodes that are not in the subtree of the item rotatedabout are unaffected by the rotation.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

LEFT ROTATION

In Left Rotation operation the tree is rotated left side torebalance the tree .

LEFT ROTATION-ALGORITHM

Node RotateLeft(Node x)

{Node y = x.right;

x.right= y.left;

y.left = x;

return y;

}

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

LEFT ROTATION

In Left Rotation operation the tree is rotated left side torebalance the tree .

LEFT ROTATION-ALGORITHM

Node RotateLeft(Node x)

{Node y = x.right;

x.right= y.left;

y.left = x;

return y;

}

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

LEFT ROTATION

In Left Rotation operation the tree is rotated left side torebalance the tree .

LEFT ROTATION-ALGORITHM

Node RotateLeft(Node x)

{Node y = x.right;

x.right= y.left;

y.left = x;

return y;

}

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

LEFT ROTATION

LEFT ROTATION - EXAMPLE

Consider a tree with three nodes of the following structure.

A - Root node.B - A’s Right child node.C - B’s Right child node.

A

B

C

- B

A C

After rotation the resulting tree structure will be as following.

B becomes the root node.A becomes the B’s Left child node.C will be B’s Right Child node.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

LEFT ROTATION

LEFT ROTATION - EXAMPLE

Consider a tree with three nodes of the following structure.

A - Root node.B - A’s Right child node.C - B’s Right child node.

A

B

C

- B

A C

After rotation the resulting tree structure will be as following.

B becomes the root node.A becomes the B’s Left child node.C will be B’s Right Child node.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

LEFT ROTATION

LEFT ROTATION - EXAMPLE

Consider a tree with three nodes of the following structure.

A - Root node.B - A’s Right child node.C - B’s Right child node.

A

B

C

- B

A C

After rotation the resulting tree structure will be as following.

B becomes the root node.A becomes the B’s Left child node.C will be B’s Right Child node.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

RIGHT ROTATION

In Right Rotation the tree is left heavy.

So Right Rotation operation is performed to rebalance thetree.

RIGHT ROTATION-ALGORITHM

Node RotateRight(Node x)

{Node y = x.left;

x.left= y.right;

y.right = x;

return y;

}

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

RIGHT ROTATION

In Right Rotation the tree is left heavy.

So Right Rotation operation is performed to rebalance thetree.

RIGHT ROTATION-ALGORITHM

Node RotateRight(Node x)

{Node y = x.left;

x.left= y.right;

y.right = x;

return y;

}

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

RIGHT ROTATION

In Right Rotation the tree is left heavy.

So Right Rotation operation is performed to rebalance thetree.

RIGHT ROTATION-ALGORITHM

Node RotateRight(Node x)

{Node y = x.left;

x.left= y.right;

y.right = x;

return y;

}

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

RIGHT ROTATION

RIGHT ROTATION - EXAMPLE

Consider a tree with three nodes of the following structure.

A - Root node.B - A’s Left child node.C - B’s Left child node.

A

B

C

- B

C A

After rotation the resulting tree structure will be as following.

B becomes the root node.A becomes the B’s Right child node.C will be B’s Left Child node.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

RIGHT ROTATION

RIGHT ROTATION - EXAMPLE

Consider a tree with three nodes of the following structure.

A - Root node.B - A’s Left child node.C - B’s Left child node.

A

B

C

- B

C A

After rotation the resulting tree structure will be as following.

B becomes the root node.A becomes the B’s Right child node.C will be B’s Left Child node.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

RIGHT ROTATION

RIGHT ROTATION - EXAMPLE

Consider a tree with three nodes of the following structure.

A - Root node.B - A’s Left child node.C - B’s Left child node.

A

B

C

- B

C A

After rotation the resulting tree structure will be as following.

B becomes the root node.A becomes the B’s Right child node.C will be B’s Left Child node.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

RIGHT - LEFT ROTATION

Two rotations need to be performed inorder to balancethe tree.

First the tree is rotated right side and then to the left side.

RIGHT - LEFT ROTATION : ALGORITHM

Right Rotation Algorithm is called first.

Then Left Rotation is called to balance the tree.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

RIGHT - LEFT ROTATION

Two rotations need to be performed inorder to balancethe tree.

First the tree is rotated right side and then to the left side.

RIGHT - LEFT ROTATION : ALGORITHM

Right Rotation Algorithm is called first.

Then Left Rotation is called to balance the tree.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

RIGHT - LEFT ROTATION

Two rotations need to be performed inorder to balancethe tree.

First the tree is rotated right side and then to the left side.

RIGHT - LEFT ROTATION : ALGORITHM

Right Rotation Algorithm is called first.

Then Left Rotation is called to balance the tree.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

RIGHT LEFT ROTATION - EXPLANATION

Consider a tree with three nodes of the following structure.

A - Root node.B - A’s Right child node.C - B’s Left child node.

RIGHT ROTATION ON ’B’ AND ’C’

A

B

C

- A

C

B

After right rotation the resulting tree structure will be asfollowing.

A - Root NodeC - A’s Right child nodeB - C’s Right child node

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

RIGHT LEFT ROTATION - EXPLANATION

Consider a tree with three nodes of the following structure.

A - Root node.B - A’s Right child node.C - B’s Left child node.

RIGHT ROTATION ON ’B’ AND ’C’

A

B

C

- A

C

B

After right rotation the resulting tree structure will be asfollowing.

A - Root NodeC - A’s Right child nodeB - C’s Right child node

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

RIGHT LEFT ROTATION - EXPLANATION

Consider a tree with three nodes of the following structure.

A - Root node.B - A’s Right child node.C - B’s Left child node.

RIGHT ROTATION ON ’B’ AND ’C’

A

B

C

- A

C

B

After right rotation the resulting tree structure will be asfollowing.

A - Root NodeC - A’s Right child nodeB - C’s Right child node

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

The tree is rotated left to get a balanced AVL tree.

LEFT ROTATION ON ’A’ AND ’C’

A

C

B

- C

A B

After left rotation the resulting tree structure will be asfollowing.

C - Root nodeA - will become C’s left child node.B - will become C’s right child node.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

The tree is rotated left to get a balanced AVL tree.

LEFT ROTATION ON ’A’ AND ’C’

A

C

B

- C

A B

After left rotation the resulting tree structure will be asfollowing.

C - Root nodeA - will become C’s left child node.B - will become C’s right child node.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

The tree is rotated left to get a balanced AVL tree.

LEFT ROTATION ON ’A’ AND ’C’

A

C

B

- C

A B

After left rotation the resulting tree structure will be asfollowing.

C - Root nodeA - will become C’s left child node.B - will become C’s right child node.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

LEFT - RIGHT ROTATION

A Left-Right rotation is the mirror image of Right-LeftRotation opertaion.

Two rotations need to be performed inorder to balancethe tree.

First the tree is rotated left side and then to the right side.

LEFT-RIGHT ROTATION : ALGORITHM

Left Rotation Algorithm is called first.

Then Right Rotation is called to balance the tree.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

LEFT - RIGHT ROTATION

A Left-Right rotation is the mirror image of Right-LeftRotation opertaion.

Two rotations need to be performed inorder to balancethe tree.

First the tree is rotated left side and then to the right side.

LEFT-RIGHT ROTATION : ALGORITHM

Left Rotation Algorithm is called first.

Then Right Rotation is called to balance the tree.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

LEFT - RIGHT ROTATION

A Left-Right rotation is the mirror image of Right-LeftRotation opertaion.

Two rotations need to be performed inorder to balancethe tree.

First the tree is rotated left side and then to the right side.

LEFT-RIGHT ROTATION : ALGORITHM

Left Rotation Algorithm is called first.

Then Right Rotation is called to balance the tree.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

LEFT-RIGHT ROTATION : EXPLANATION

Consider a tree with three nodes of the following structure.

A - Root node.B - A’s Left child node.C - B’s Right child node.

LEFT ROTATION ON ’B’ AND ’C’

A

B

C

- A

C

B

After left rotation the resulting tree structure will be asfollowing.

A - Root NodeC - A’s Left child nodeB - C’s Left child node

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

LEFT-RIGHT ROTATION : EXPLANATION

Consider a tree with three nodes of the following structure.

A - Root node.B - A’s Left child node.C - B’s Right child node.

LEFT ROTATION ON ’B’ AND ’C’

A

B

C

- A

C

B

After left rotation the resulting tree structure will be asfollowing.

A - Root NodeC - A’s Left child nodeB - C’s Left child node

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

LEFT-RIGHT ROTATION : EXPLANATION

Consider a tree with three nodes of the following structure.

A - Root node.B - A’s Left child node.C - B’s Right child node.

LEFT ROTATION ON ’B’ AND ’C’

A

B

C

- A

C

B

After left rotation the resulting tree structure will be asfollowing.

A - Root NodeC - A’s Left child nodeB - C’s Left child node

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

The tree is rotated right to get a balanced AVL tree.

RIGHT ROTATION ON ’A’ AND ’C’

A

C

B

- C

B A

After right rotation the resulting tree structure will be asfollowing.

C - Root nodeB - will become C’s left child node.A - will become C’s right child node.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

The tree is rotated right to get a balanced AVL tree.

RIGHT ROTATION ON ’A’ AND ’C’

A

C

B

- C

B A

After right rotation the resulting tree structure will be asfollowing.

C - Root nodeB - will become C’s left child node.A - will become C’s right child node.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

The tree is rotated right to get a balanced AVL tree.

RIGHT ROTATION ON ’A’ AND ’C’

A

C

B

- C

B A

After right rotation the resulting tree structure will be asfollowing.

C - Root nodeB - will become C’s left child node.A - will become C’s right child node.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

AVL TREE OPERATIONS

There are three basic operations in an AVL tree.

1 Insert

2 Search

3 Delete

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

AVL TREE OPERATIONS

There are three basic operations in an AVL tree.

1 Insert

2 Search

3 Delete

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

INSERT OPERATION

Inserting a node ’n’ into an AVL tree changes the heightof some nodes in the tree.

The only nodes whose heights can increase are theancestors of node ’n’.

If the insertion causes the tree to become unbalancedthen some ancestors of ’n’ would have a height imbalance.

After the insertion of a node the tree need to rotated onlyonce.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

INSERT OPERATION

Inserting a node ’n’ into an AVL tree changes the heightof some nodes in the tree.

The only nodes whose heights can increase are theancestors of node ’n’.

If the insertion causes the tree to become unbalancedthen some ancestors of ’n’ would have a height imbalance.

After the insertion of a node the tree need to rotated onlyonce.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

STEPS - INSERT OPERATION

Insert the node ’n’a in the appropriate position.Travel up the tree towards the root node ’r’ and check forheight imbalance of ancestors of newly added nodeIf all nodes till ’r’b are balanced

thenThe newly added node did not cause imbalance

elseFind the first imbalanced node ’x’c in the pathRotate ’x’ with ’y’d and ’z’e

a’n’ - newly added nodeb’r’ - root nodecx - first encountered unbalanced ancestor in the pathdy- child node of ’x’ that lies in the path from ’n’ to ’r’ez - grandchild node of ’x’ that lies in the path from ’n’ to ’r’

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

STEPS - INSERT OPERATION

Insert the node ’n’a in the appropriate position.Travel up the tree towards the root node ’r’ and check forheight imbalance of ancestors of newly added nodeIf all nodes till ’r’b are balanced

thenThe newly added node did not cause imbalance

elseFind the first imbalanced node ’x’c in the pathRotate ’x’ with ’y’d and ’z’e

a’n’ - newly added nodeb’r’ - root nodecx - first encountered unbalanced ancestor in the pathdy- child node of ’x’ that lies in the path from ’n’ to ’r’ez - grandchild node of ’x’ that lies in the path from ’n’ to ’r’

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

EXAMPLE - INSERT OPERATION

45

16

32

78

50

48 63

88

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

EXAMPLE - INSERT OPERATION

INSERT - 55

45

16

32

78

50

48 63

55

88

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

EXAMPLE - INSERT OPERATION

45

16

32

78

63

50

48 55

88

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

EXAMPLE - INSERT OPERATION

45

16

32

63

50

48 55

78

88

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

EXAMPLE - SEARCH OPERATION

The Search operation in AVL tree is same as in case ofBinary search tree.

The Search operation can be performed with the keyvalue that need to be searched in the given AVL tree.

The search operation returns a node from a tree if thenode value matches with the key value.

If the key value does not match with any node value inthe tree no value is returned.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

EXAMPLE - SEARCH OPERATION

The Search operation in AVL tree is same as in case ofBinary search tree.

The Search operation can be performed with the keyvalue that need to be searched in the given AVL tree.

The search operation returns a node from a tree if thenode value matches with the key value.

If the key value does not match with any node value inthe tree no value is returned.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

STEPS - SEARCH OPERATION

Start searching from the root node.

1 if nodeValue = key2 return key found3 else if {4 if(key < nodeValue)5 nodeValue = nodeValue.LeftChild6 Repeat from Step 17 else if(key > nodeValue)8 nodeValue = nodeValue.RightChild9 Repeat from Step 110 }11 if the key value does not match with any nodeValue12 {13 return key value not found14 }

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

STEPS - SEARCH OPERATION

Start searching from the root node.

1 if nodeValue = key2 return key found3 else if {4 if(key < nodeValue)5 nodeValue = nodeValue.LeftChild6 Repeat from Step 17 else if(key > nodeValue)8 nodeValue = nodeValue.RightChild9 Repeat from Step 110 }11 if the key value does not match with any nodeValue12 {13 return key value not found14 }

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

EXAMPLE - SEARCH OPERATION

42

33

23 39

46

43 47

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

EXAMPLE - SEARCH OPERATION

Key Value to be searched - 23

42

33

23 39

46

43 47

key value found

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

DELETION OPERATION

Deletion is more complicated than insertion.The deletion of a node from a tree may decrease theheight of the tree which may lead to unbalanced treestructure.The node to be deleted may be either of the followingcases

1 Leaf Node - Delete the leaf node.2 Node with one children - Delete the node and replace it with

the only child.3 Node with two children - Replace the node with left-most node

in the right subtree.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

DELETION OPERATION

Deletion is more complicated than insertion.The deletion of a node from a tree may decrease theheight of the tree which may lead to unbalanced treestructure.The node to be deleted may be either of the followingcases

1 Leaf Node - Delete the leaf node.2 Node with one children - Delete the node and replace it with

the only child.3 Node with two children - Replace the node with left-most node

in the right subtree.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

DELETION OPERATION

Deletion is more complicated than insertion.The deletion of a node from a tree may decrease theheight of the tree which may lead to unbalanced treestructure.The node to be deleted may be either of the followingcases

1 Leaf Node - Delete the leaf node.2 Node with one children - Delete the node and replace it with

the only child.3 Node with two children - Replace the node with left-most node

in the right subtree.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

DELETION OPERATION

STEPS - DELETION OPERATION

1 Delete the node based on the three cases.2 Start from the deleted node place3 Travel up to the root node and check for height balance

of ancestor.4 if (an unbalanced ancestor is encountered){5 Let ’x’ be the Unbalanced ancestor found6 Find ’y’ - child of ’x’ with greater height7 Find ’z’ - child of ’y’ with greater height8 Perform Rotation operation on ’x’ with ’y’ and ’z’9 Repeat from Step 3 till root node is reached.10 }11 if the checked ancestor node is rootnode and is balanced12 Then tree is balanced

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

DELETION OPERATION

STEPS - DELETION OPERATION

1 Delete the node based on the three cases.2 Start from the deleted node place3 Travel up to the root node and check for height balance

of ancestor.4 if (an unbalanced ancestor is encountered){5 Let ’x’ be the Unbalanced ancestor found6 Find ’y’ - child of ’x’ with greater height7 Find ’z’ - child of ’y’ with greater height8 Perform Rotation operation on ’x’ with ’y’ and ’z’9 Repeat from Step 3 till root node is reached.10 }11 if the checked ancestor node is rootnode and is balanced12 Then tree is balanced

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

EXAMPLE - DELETE OPERATION

41

33

23 39

43

42 46

47

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

EXAMPLE - DELETE OPERATION

Delete - 41

42

33

23 39

43

46

47

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

EXAMPLE - DELETE OPERATION

42

33

23 39

43

46

47

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

EXAMPLE - DELETE OPERATION

42

33

23 39

46

43 47

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

TIME COMPLEXITY

AVL tree takes O(log n) time for insert, search and deleteoperations in both average and worst case.

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

REFERENCES

Allen B. Tucker Computer science handbook Chapman andHall/CRC, USA 2004

Dinesh P. Mehta, Sartaj Sahni Handbook of data structuresand applications Chapman and Hall/CRC, USA 2005

Hermann A. Maurer New results and new trends in computerscience Graz, Austria 1991

Frank Dehne, Marina Gavrilova, Jorg-Rudiger Sack, CsabaD.Toth(Eds.) Algorithms and Data Structures:11thInternational Symposium WADS 2009, Banff, Canada 2009

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA kbalaraman@cs.indstate.edu

REFERENCES

William J.Collins Data Structures and the Standard TemplateLibrary McGraw-Hill, New York, USA 2003

Alfred V. Aho, John E. Hopcroft, Jeffrey D. Ullman DataStructures and Algorithms Addison-Wesley, USA 1985

Mark Allen Weiss Data Structures and Algorithm Analysis TheBenjamin/Cummings Publishing Company, USA 1995

Recommended