46
AVL TREE Prepared By: AWAIS AHMAD Software Engineering 3 rd Semester University of Gujrat Lahore Campus

AVL Tree

Embed Size (px)

Citation preview

Topic: AVL TREE

AVL TREEPrepared By:AWAIS AHMADSoftware Engineering3rd SemesterUniversity of GujratLahore Campus

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 NodePrepared By: Awais Ahmad

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)Prepared By: Awais Ahmad

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); }Prepared By: Awais Ahmad

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 23Prepared By: Awais Ahmad

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 235BF=0Prepared By: Awais Ahmad

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

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

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

1975BF=0BF=0BF=0RR RotationABABPrepared By: Awais Ahmad

GENERATE AVL TREE FOR THE VALUES5, 7, 19, 12, 10, 15, 18, 20, 25, 231975BF=0BF=0BF=0Prepared By: Awais Ahmad

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 = -1Prepared By: Awais Ahmad

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

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=-1Prepared By: Awais Ahmad

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

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

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=1Prepared By: Awais Ahmad

Prepared By:Awais Ahmad

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

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

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=0Prepared By: Awais Ahmad

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

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

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

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 NodePrepared By: Awais Ahmad

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

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

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 NodePrepared By: Awais Ahmad

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

Prepared By:Awais Ahmad

Prepared By: Awais Ahmad

Prepared By: Awais AhmadBS-SE-AUOG Lahore Campus