88
Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected] December 7, 2011

Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

Implementation of Dictionaries Using AVL Tree

Kanimozhi BalaramanIndiana State UniversityTerre Haute IN, USA

[email protected]

December 7, 2011

Page 2: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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

Page 3: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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

Page 4: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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

Page 5: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 6: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 7: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 8: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 9: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 10: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 11: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 12: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 13: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 14: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 15: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 16: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 17: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 18: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 19: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 20: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

EXAMPLE OF AN AVL TREE

D

B

A

K L

C

G

F

E

N S

M

I

H J

Page 21: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 22: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 23: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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)

Page 24: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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)

Page 25: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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’

Page 26: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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’

Page 27: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 28: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 29: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 30: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 31: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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;

}

Page 32: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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;

}

Page 33: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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;

}

Page 34: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 35: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 36: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 37: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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;

}

Page 38: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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;

}

Page 39: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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;

}

Page 40: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 41: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 42: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 43: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 44: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 45: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 46: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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

Page 47: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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

Page 48: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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

Page 49: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 50: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 51: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 52: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 53: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 54: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 55: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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

Page 56: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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

Page 57: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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

Page 58: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 59: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 60: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 61: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

AVL TREE OPERATIONS

There are three basic operations in an AVL tree.

1 Insert

2 Search

3 Delete

Page 62: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

AVL TREE OPERATIONS

There are three basic operations in an AVL tree.

1 Insert

2 Search

3 Delete

Page 63: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 64: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 65: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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’

Page 66: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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’

Page 67: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

EXAMPLE - INSERT OPERATION

45

16

32

78

50

48 63

88

Page 68: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

EXAMPLE - INSERT OPERATION

INSERT - 55

45

16

32

78

50

48 63

55

88

Page 69: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

EXAMPLE - INSERT OPERATION

45

16

32

78

63

50

48 55

88

Page 70: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

EXAMPLE - INSERT OPERATION

45

16

32

63

50

48 55

78

88

Page 71: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 72: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 73: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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 }

Page 74: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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 }

Page 75: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

EXAMPLE - SEARCH OPERATION

42

33

23 39

46

43 47

Page 76: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

EXAMPLE - SEARCH OPERATION

Key Value to be searched - 23

42

33

23 39

46

43 47

key value found

Page 77: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 78: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 79: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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.

Page 80: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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

Page 81: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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

Page 82: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

EXAMPLE - DELETE OPERATION

41

33

23 39

43

42 46

47

Page 83: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

EXAMPLE - DELETE OPERATION

Delete - 41

42

33

23 39

43

46

47

Page 84: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

EXAMPLE - DELETE OPERATION

42

33

23 39

43

46

47

Page 85: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

EXAMPLE - DELETE OPERATION

42

33

23 39

46

43 47

Page 86: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

TIME COMPLEXITY

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

Page 87: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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

Page 88: Implementation of Dictionaries Using AVL Tree …cs.indstate.edu/~kbalaraman/presen.pdfImplementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre

Implementation of Dictionaries Using AVL Tree Kanimozhi Balaraman Indiana State University Terre Haute IN, USA [email protected]

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