data_structure

data_structure - Priority Queue: Heap Structures F...

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

View Full Document Right Arrow Icon
Abdelghani Bellaachia, Algorithms - 1 - Priority Queue: Heap Structures F Definition: A max-heap ( min-heap ) is a complete BT with the property that the value (priority) of each node is at least as large ( small ) as the values at its children (if they exist). F Implementation : Sequential representation F Example : 90 67 85 30 57 31 24 11 8 20 2 25 F Operations: Insertion Construct heap Deletion Delete_min (Delete_max)
Background image of page 1

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

View Full DocumentRight Arrow Icon
Abdelghani Bellaachia, Algorithms - 2 - F Insertion of a heap Procedure Insert(A [ 1..n ] ,i) /* Insert A [ i ] into the already hear A [ 1..n ] */ Begin While (I>1) and (A [ i ] > A [ i 2 ] ) do Begin swap(A [ i ] , A [ i 2 ] ); i = i 2 ; Endwhile End; Example: •• List of elements: 40, 80, 35, 90, 85, 100 •• The heap is empty •• Insert(40) 40 •• Insert(80) 40 80 ====> 80 40 •• Etc.
Background image of page 2
- 3 - F Construction of a heap First method : •• Procedure construct_heap1(A [ 1..n ] ) /* The array will contain the heap */ Integer i; Begin For I=2 to n do Insert (A [ 1..n ] ,i); endfor; end; •• Analysis : Theorem: Construct_heap1 takes O(nlogn) in the worst case. Proof : The worst case is when the elements are inserted in ascending order. The Insert procedure takes O(longn). Therefore, we have O(nlogn). End proof . •• Note : The best case when the elements are inserted in descending order. In this case The Insert procedure takes O(1). Therefore, Construct_heap takes O(n). Second method : •• Takes the input array as a complete binary tree. •• Construct the heap level by level from the leaves
Background image of page 3

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

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

Page1 / 17

data_structure - Priority Queue: Heap Structures F...

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

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