7300_05

7300_05 - CSC7300 AlgorithmsDesignandAnalysis Heaps 1 Heaps

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

View Full Document Right Arrow Icon
1 CSC 7300 Algorithms Design and Analysis  Heaps
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Heaps Given a set S of data with ordered keys Support two main operations Insert Extract-min Decrease-key
Background image of page 2
3 binary heap  is a   binary tree  that satisfies both: (1)   Shape  Property All levels, except deepest, are fully filled Deepest level is filled from left to right  (1)   Heap  Property Value of a node Value of its children   Binary 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
4 Satisfying Shape Property Example of a tree with shape  property
Background image of page 4
5 Not  Satisfying Shape Property This level (not deepest) is NOT  fully filled
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Not  Satisfying Shape Property Deepest level NOT filled from  left to right
Background image of page 6
7 Satisfying Heap Property
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Not  Satisfying Heap Property Help!
Background image of page 8
9 Q. Q.   Given a heap, what is so special about the root’s value? A. A.       … always the minimum Min Heap Because of this, the previous heap is also  called   a  min heap
Background image of page 9

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

View Full DocumentRight Arrow Icon
10   Find-Min   :    find the minimum value          Θ (1) time   Extract-Min   :   delete the minimum value           Ο (log  n ) time   (how??)   Insert   :     insert a new value into heap          Ο (log  n ) time   (how??) Heap Operations n  = # nodes in the heap
Background image of page 10
11 How to do Extract-Min? 3 8 4 23 13 12 24 38 43 Heap before Extract-Min
Background image of page 11

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

View Full DocumentRight Arrow Icon
12 Step 1:   Restore Shape Property 3 8 4 23 13 12 24 38 43       Copy value of last node to root.       Next,  remove last node
Background image of page 12
13 Next, highlight the root   Only this node  may violate  heap property Step 2:   Restore Heap Property 38 8 4 23 13 12 24 43 If violates, swap highlighted node with “smaller” child (if not, everything  done)
Background image of page 13

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

View Full DocumentRight Arrow Icon
14 Step 2:   Restore Heap Property 4 8 38 23 13 12 24 43  After swapping, only the highlighted node  may  violate  heap property If violates, swap highlighted node with “smaller” child   (if not,  everything done)
Background image of page 14
15 Step 2:   Restore Heap Property 4 8 12 23 13 38 24 43  As soon as the highlighted node  satisfies  the  heap property Everything done !!!
Background image of page 15

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

View Full DocumentRight Arrow Icon
16 How to do Insert? 3 8 4 23 13 12 24 38 43 Heap before Insert
Background image of page 16
17 Step 1:   Restore Shape Property 3 8 4 23 13 12 24 38 43       Create a new node with the new value.     Next, add it to the  heap at correct position 5
Background image of page 17

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

View Full DocumentRight Arrow Icon
18  Highlight the new node  Only this node’s parent    may violate   heap property Step 2:   Restore Heap Property 3 8 4 23 13 12 24 38 43 5       If violates, swap highlighted node with parent                 (if  not, everything done)
Background image of page 18
19 After swapping, only highlighted  node’s parent  may violate  heap  property 3 8 4 5 13 12 24 38 43 23 Step 2:   Restore Heap Property If violates, swap highlighted node with parent            (if not, everything  done)
Background image of page 19

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

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

Page1 / 97

7300_05 - CSC7300 AlgorithmsDesignandAnalysis Heaps 1 Heaps

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

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