{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture 06 - Amrita

# lecture 06 - Amrita - Binary Tree Structures Algorithms for...

This preview shows pages 1–16. Sign up to view the full content.

Binary Tree Structures : Algorithms for Heap

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Heap Structures Demand : To keep inserting the elements into a list At the same time keep pointing to the largest / smallest element Queue : Is good for insertion, but locating the largest concurrently can be cumbersome Requirement : Is to device a sort of PRIORITY QUEUE structure HEAP is the required structure
Definition : A Heap is a complete binary tree with the property that the value at each node is at least as large as the values at its children ( if they exist) This implies that the largest is at the root of the heap if the elements are distinct If the logic is reversed we get the smallest value in the root 60 40 80 A Heap 80 40 60 Not a Heap 50 40 60 45 42 32 35 A Heap

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Insertion into a HEAP : One adds a new item at the bottom of the heap Let us consider a heap of six elements 45 70 80 40 35 50 1 2 3 4 5 6 Let us try to insert the seventh element, say 90
STEP 1 : The element is inserted at the bottom most location as the latest child 50 45 70 80 40 35 1 2 3 4 5 6 90 7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
STEP 2 : Check whether the insertion of the content in this child location creates a HEAP 50 70 3 6 90 7 Latest child 7 Corresponding parent 7/2 = 3 Corresponding local heap structure Ch1 2 * Parent 6 Ch2 Ch1 + 1 7 This does not make a HEAP Hence the content of parent_node (3) should be swapped with the content of latest_child_node (7) 6 50 90 3 70 7
This implies the new content is moved to node 3 Hence newly identified child node is 3 STEP 3 : Therefore it is now required to check whether the movement of the content into the node makes a HEAP treating the node as a child node Child node 3 Corresponding Parent 3/2 = 1 Ch1 2 Ch2 3 45 80 1 2 90 3 This does not result in a HEAP

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Hence the content of the parent_node (1) should be swapped with the content of the newly identified child_node (3) 45 80 1 2 90 3 45 90 1 2 80 3 This implies that the new content is moved to node 1 Hence newly identified child node 1
STEP 4 : Therefore it is now required to check whether the movement of the content into the node makes a HEAP treating the node as a child node Child node 1 Corresponding Parent 1/2 = 0 Ch1 x Ch2 x This is INVALID INSERTION PROCESS TERMINATES 70 50 45 80 90 40 35 1 2 3 4 5 6 7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Algorithm : 70 50 45 80 90 40 35 1 2 3 4 5 6 7 . . . . . . . : : : N - 1 N Latest Location ITEM
{ latest_location N A(latest_location) ITEM new_child latest_location Back : corresponding_ parent new_child/2 if corresponding_parent < 1 then { Insertion is Completed Terminate } else if( A(corresponding_parent) < ITEM then { A(new_child) A(corresponding_parent} A(corresponding_parent) ITEM new_child corresponding_parent go Back } else { ITEM is settled Terminate } }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
70 50 45 80 90 40 35 1 2 3 4 5 6 7 Best Case Insertion
98 50 45 80 90 40 35 1 2 3 4 5 6 7 90 80 50 45 98 40 35 1 2 3 4 5 6 7 80 50 45 90 98 40 35 1 2 3 4 5 6 7 Worst Case Insertion

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
85 50 45 80 90 40 35 1 2 3 4 5 6 7 90 80 50 45 85 40 35 1 2 3 4 5 6 7 One of the Average Case Insertion
Time Complexity Analysis Best Case Time Complexity : When the new element gets settled in the latest location.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}