7.mod 4-7 avl (1)

116
DATA STRUCTURES AND ALGORITHMS MODULE 4 AVL TREES HEIGHT BALANCED TREES

Upload: robinpt

Post on 10-Sep-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

DATA STRUCTURES AND ALGORITHMS MODULE 4

DATA STRUCTURES AND ALGORITHMSMODULE 4AVL TREESHEIGHT BALANCED TREES

BST Insert 7,5,4,8,6,9BST Insert 4,5,6,7,8,9456789758469Tree 1Tree 2Depending on the input order, we have different trees456789758469Tree 1Tree 2Search 9456789758469Tree 1Tree 2Search 9456789758469Tree 1Tree 23 comparisons6 comparisons456789758469Tree 1Tree 2No. of comparisons1 Rchild=PptrPptr->Height=ComputeHeight(Pptr)Aptr->Height=ComputeHeight(Aptr)Stop

AVL rotationsCase 1: Example

Unbalance occurs due to the insertion in the left subtree of the leftchild of the pivot node (LEFT-TO-LEFT insertion)

6425819110000130AVL rotationsUnbalance occurs due to the insertion in the left subtree of the leftchild of the pivot node

Insert 0

64258192211001030AVL rotationsUnbalance occurs due to the insertion in the left subtree of the leftchild of the pivot node

To balance this tree:Choose pivot node : select node 4

Rotate Right64258192211001030pivotAVL rotationsUnbalance occurs due to the insertion in the left subtree of the leftchild of the pivot node

To balance this tree:Choose pivot node : select node 4

Rotate Right64258192211001030pivotAVL rotationsUnbalance occurs due to the insertion in the left subtree of the leftchild of the pivot node

To balance this tree:Choose pivot node : select node 4

Rotate Right6425819211001030pivotAVL rotationsUnbalance occurs due to the insertion in the left subtree of the leftchild of the pivot node

To balance this tree:Choose pivot node : select node 4

Rotate Right6425819211001030pivotAVL rotations64258192211001030pivot6214809101000130pivot50AVL rotations64258192211001030pivot621480910100013050Right subtree of left child of pivot node becomes the left subtree of PP becomes the rightchild of ALeft subtree of A remains the samepivotAAAVL rotationsCase 2:Unbalance occurs due to the insertion in the right subtree of the rightchild of the pivot node (RIGHT-TO-RIGHT insertion)

PAAlArPlAVL rotationsCase 2:Unbalance occurs due to the insertion in the right subtree of the rightchild of the pivot node (RIGHT-TO-RIGHT insertion)

PBBlBrPlPBBlBrPlAlgorithmRightToRightRotationInput: Pointer Pptr to the pivot nodeOutput: AVL rotation corresponding to the unbalance due to insertion in the right subtree of the right child of Pptr

Bptr=pptr->rchildPptr->rchild=Bptr->lchildBptr->lchild=PptrPptr->Height=ComputeHeight(Pptr)Bptr->Height=ComputeHeight(Bptr)Stop

AVL rotationsCase 2: Example Unbalance occurs due to the insertion in the right subtree of the rightchild of the pivot node (RIGHT-TO-RIGHT insertion)

54279110001810006AVL rotationsCase 2: Example Unbalance occurs due to the insertion in the right subtree of the rightchild of the pivot node (RIGHT-TO-RIGHT insertion)

Insert 11

54279210002810016110AVL rotationsCase 2: Example Unbalance occurs due to the insertion in the right subtree of the rightchild of the pivot node (RIGHT-TO-RIGHT insertion)

Insert 11Chose pivot node

54279210002810016110pivotAVL rotationsCase 2: Example Unbalance occurs due to the insertion in the right subtree of the rightchild of the pivot node (RIGHT-TO-RIGHT insertion)

Insert 11Chose pivot nodeRotate left

54279210002810016110pivotAVL rotationsCase 2: Example Unbalance occurs due to the insertion in the right subtree of the rightchild of the pivot node (RIGHT-TO-RIGHT insertion)

Insert 11Chose pivot nodeRotate left

54279210002810016110pivotAVL rotationsCase 2: Example Unbalance occurs due to the insertion in the right subtree of the rightchild of the pivot node (RIGHT-TO-RIGHT insertion)

Insert 11Chose pivot nodeRotate left

54279210002810016110pivotAVL rotationsCase 2: Example Unbalance occurs due to the insertion in the right subtree of the rightchild of the pivot node (RIGHT-TO-RIGHT insertion)

Insert 11Chose pivot nodeRotate left

54279210002810016110pivotAVL rotationsCase 2: Example Unbalance occurs due to the insertion in the right subtree of the rightchild of the pivot node (RIGHT-TO-RIGHT insertion)

54279110010810016110pivot54279210002810016110pivotAVL rotations54279110010810016110pivot54279210002810016110pivotleft subtree of right child of pivot node becomes the right subtree of PP becomes the left child of BRight subtree of B remains the sameBBAVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

PAAlBrPrBBlAVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

PAAlBrPrBBlPAAlBrPrBBlAVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)It involves 2 rotationsRotation 1

PAAlBrPrBBlPAAlBrPrBBlAVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)It involves 2 rotationsRotation 2

PAAlBrPrBBlPAAlBrPrBBlAlgorithmLeftToRightRotationInput: Pointer Pptr to the pivot nodeOutput: AVL rotation corresponding to the unbalance due to insertion in the right subtree of the left child of PptrAptr=Pptr->LchildRightToRight Rotation(Aptr)LeftToLeftRotation(Pptr)Stop

AVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

8325101111010001604090AVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

83251011121101016041907AVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

83251011121101016041907pivotABPAAlBrPrBBlAVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

83251011121101016041907pivotABPAAlBrPrBBlAVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

83251011121101016041907pivotABPAAlBrPrBBlAVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

83251011121101016041907pivotABPAAlBrPrBBlAVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

83251011121101016041907pivotABPAAlBrPrBBlAVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

83251011121101016041907pivotABPAAlBrPrBBlAVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

83251011121101016041907pivotABPAAlBrPrBBlAVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

83251011121101016040907pivotABPAAlBrPrBBl1AVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

83251011121101016040907pivotABPAAlBrPrBBl1AVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

83251011121101016040907pivotABPAAlBrPrBBl1AVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

83251011121101016040907pivotABPAAlBrPrBBl1AVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

83251011121101016040907pivotABPAAlBrPrBBl1AVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

83251011121101016040907pivotABPAAlBrPrBBl1AVL rotationsCase 3:Unbalance occurs due to the insertion in the right subtree of the leftchild of the pivot node (LEFT-TO-RIGHT insertion)

83251011101101016040907pivotAB0PAAlBrPrBBlAVL rotationsCase 4:Unbalance occurs due to the insertion in the left subtree of the rightchild of the pivot node (RIGHT-TO-LEFT insertion)

PAArBrPlBBlAVL rotationsCase 4:Unbalance occurs due to the insertion in the left subtree of the rightchild of the pivot node (RIGHT-TO-LEFT insertion)

PAArBrPlBBlAVL rotationsCase 4:Unbalance occurs due to the insertion in the left subtree of the rightchild of the pivot node (RIGHT-TO-LEFT insertion)

PAArBrPlBBlPAArBrPlBBlAVL rotationsCase 4:Unbalance occurs due to the insertion in the left subtree of the rightchild of the pivot node (RIGHT-TO-LEFT insertion)

PAArBrPlBBlPAArBrPlBBlAlgorithmRightToLeftRotationInput: Pointer Pptr to the pivot nodeOutput: AVL rotation corresponding to the unbalance due to insertion in the left subtree of the right child of Pptr

Aptr=pptr->RchildLeftToLeftRotation(Aptr)RightToRightRotation(Pptr)Stop

QUESTION:

Insert the following elements into a height balanced tree6, 7, 9, 4, 3, 5, 8

6, 7, 9, 4, 3, 5, 8606, 7, 9, 4, 3, 5, 8676, 7, 9, 4, 3, 5, 86792106, 7, 9, 4, 3, 5, 8679210pivot6, 7, 9, 4, 3, 5, 867900pivot06, 7, 9, 4, 3, 5, 8679101406, 7, 9, 4, 3, 5, 867920243106, 7, 9, 4, 3, 5, 86792024310pivot6, 7, 9, 4, 3, 5, 8679104300pivot06, 7, 9, 4, 3, 5, 867910430006, 7, 9, 4, 3, 5, 8679204310150pivot6, 7, 9, 4, 3, 5, 8679204310150pivot6, 7, 9, 4, 3, 5, 8679204310150pivot6, 7, 9, 4, 3, 5, 8679204310150pivot6, 7, 9, 4, 3, 5, 8679204310050pivot6, 7, 9, 4, 3, 5, 86790430050pivot106, 7, 9, 4, 3, 5, 867914300502180pivot6, 7, 9, 4, 3, 5, 86791430050218pivot6, 7, 9, 4, 3, 5, 86790430050080pivot06, 7, 9, 4, 3, 5, 867904300500800???Search for a node in a BST which contains 1,00,000 nodes

Search the path from root to the node that contains the specified element.What if the nodes are located in different blocks in the memory?Most of the search time is spent on memory access

Use Search Trees With Degree More Than 2

MULTIWAY SEARCH TREESTarget : Reduce height So that memory access time is reduced

M-way search treesEach node has atmost m subtrees Keys are arranged in a defined order within the node. All keys in the subtree to the left of a key are predecessors of the key and that on the right are successors of the key

So binary search tree is a 2-way search tree

Definition:An m-way search tree is a tree in whichAll the nodes are of degree