7300_05

# 7300_05 - CSC7300 Heaps 1 Heaps Insert Extractmin...

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

1 CSC 7300 Algorithms Design and Analysis  Heaps

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

View Full Document
2 Heaps Given a set S of data with ordered keys Support two main operations Insert Extract-min Decrease-key
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

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

View Full Document
4 Satisfying Shape Property Example of a tree with shape  property
5 Not  Satisfying Shape Property This level (not deepest) is NOT  fully filled

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

View Full Document
6 Not  Satisfying Shape Property Deepest level NOT filled from  left to right
7 Satisfying Heap Property

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

View Full Document
8 Not  Satisfying Heap Property Help!
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

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

View Full Document
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
11 How to do Extract-Min? 3 8 4 23 13 12 24 38 43 Heap before Extract-Min

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

View Full Document
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
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)

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

View Full Document
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)
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 !!!

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

View Full Document
16 How to do Insert? 3 8 4 23 13 12 24 38 43 Heap before Insert
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

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

View Full Document
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)
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)

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

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

{[ snackBarMessage ]}

### Page1 / 97

7300_05 - CSC7300 Heaps 1 Heaps Insert Extractmin...

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

View Full Document
Ask a homework question - tutors are online