45
AVL TREES Balanced Binary Search Trees 3) Name : Ehsan Elahi Roll No: 15034198-043

Avl Tree Implementation

Embed Size (px)

Citation preview

Topic: AVL TREE

AVL TREESBalanced Binary Search Trees3) Name : Ehsan ElahiRoll No: 15034198-043

BASIC CONCEPTAVL tree is the special case ofBINARY SEARCH TREE.WHAT IS BINARY SEARCH TREE Every node contain only two child.Small value at left child node and large value at right child node as compare to Root node.The main advantage of BST is that it is easy to perform operations like inserting, searching, traversing and deleting.

Root NodeLeft child NodeRight child Node

PROBLEM WITH BSTThe disadvantage of skewed binary search tree is that the worst case time complexity of a search is O(n).

504060304555704050302010SymmetricSkewed12312345O(Log N)O(N)

NEED TO RESOLVE PROBLEMThere is a need to maintain the binary search tree to be of the balanced height, so that it is possible to obtained for the search option a time complexity of O(log N) in the worst case.One of the most popular balanced tree was introduced by Andelson-Velskii and Landis(AVL)

Prepared By:Awais Ahmad

DEFINITION OF AVL TREEA binary tree is said to be an AVL tree if T is a root of tree and T(L) is its left sub tree and T(R) is its right sub-tree of tree T and H(T(L)) and H(T(R)) are the heights of the left and right sub-trees of T respectively, and |H(T(L)) - H(T(R))| 1 && key < node->left->key) return rightRotate(node); // Right Right Case if (balance < -1 && key > node->right->key) return leftRotate(node); // Left Right Case if (balance > 1 && key > node->left->key) { node->left = leftRotate(node->left); return rightRotate(node); } // Right Left Case if (balance < -1 && key < node->right->key) { node->right = rightRotate(node->right); return leftRotate(node); }

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 23

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 235BF=0

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 235BF=-17BF=0

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 235BF=-27BF=-119BF=0

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 2357BF=-119BF=0BF=-2RR

1975BF=0BF=0BF=0RR RotationABAB

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 231975BF=0BF=0BF=0

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 231975BF=-1BF=0BF=112BF=0H(T(L)) = 1H(T(R)) = 2BF = H(T(L)) - H(T(R))BF = 1 - 2BF = -1

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 231975BF=0BF=212BF=110BF=0BF=-2

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 231975BF=0BF=212BF=110BF=0BF=-2LLAB

Inserted Node

LL Rotation75BF=0191210BF=0BF=0BF=0BF=-1

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 2375BF=0191210BF=0BF=0BF=0BF=-1

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 2375BF=0191210BF=0BF=1BF=-115BF=-2BF=0

Prepared By:Awais Ahmad

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 2375BF=0191210BF=0BF=1BF=-115BF=-2BF=0

Inserted NodeRRA

RR RotationB1271915510BF=0BF=0BF=0BF=0BF=0BF=1

Prepared By:Awais Ahmad

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 231271915510BF=0BF=0BF=0BF=0BF=0BF=1

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 231271915510BF=0BF=0BF=-1BF=0BF=-118BF=2BF=0

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 231271915510BF=0BF=0BF=-1BF=0BF=-118BF=2BF=0ALRBC

Inserted Node

LR Rotation127510BF=0BF=0BF=0BF=0181519BF=0BF=0BF=0

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 23127510BF=0BF=0BF=0BF=0181519BF=0BF=0BF=0

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 23127510BF=0BF=0BF=0BF=-1181519BF=0BF=-1BF=-120BF=0

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 23127510BF=0BF=0BF=0181519BF=020BF=-125BF=0BF=-2BF=-2BF=-2

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 23127510BF=0BF=0BF=0181519BF=020BF=-125BF=0BF=-2BF=-2BF=-2RRABRR Rotation

Inserted Node

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 23127510BF=0BF=0BF=0181520BF=01925BF=0BF=0BF=0BF=-1BF=-1

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 23127510BF=0BF=0BF=0181520BF=01925BF=0BF=1BF=-123BF=0BF=-2BF=-2

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 23127510BF=0BF=0BF=0181520BF=01925BF=0BF=1BF=-123BF=0BF=-2BF=-2ARRRR Rotation

B

Inserted Node

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 23127510BF=0BF=0BF=0201825BF=023BF=0BF=1BF=0BF=-11519BF=0BF=0

Prepared By:Awais Ahmad