lecture 06 - Amrita

lecture 06 - Amrita - Binary Tree Structures : Algorithms...

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

View Full Document Right Arrow Icon
Binary Tree Structures : Algorithms for Heap
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
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
Background image of page 9

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

View Full DocumentRight Arrow Icon
Algorithm : 70 50 45 80 90 40 35 1 2 3 4 5 6 7 . . . . . . . : : : N - 1 N Latest Location ITEM
Background image of page 10
{ 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 } }
Background image of page 11

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

View Full DocumentRight Arrow Icon
70 50 45 80 90 40 35 1 2 3 4 5 6 7 Best Case Insertion
Background image of page 12
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
Background image of page 13

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

View Full DocumentRight Arrow Icon
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
Background image of page 14
Time Complexity Analysis Best Case Time Complexity : When the new element gets settled in the latest location.
Background image of page 15

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

View Full DocumentRight Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 70

lecture 06 - Amrita - Binary Tree Structures : Algorithms...

This preview shows document pages 1 - 16. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online