CSCE 3100 Data Structures and Algorithm Analysis - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

CSCE 3100 Data Structures and Algorithm Analysis

Description:

A heap T storing n keys has height h = log(n 1) , which is O(log n) ADT for Min Heap ... Merging Example. 15. 16. 12. 4. 9. 6. 20. 23. 25. 15. 16. 5. 12. 4 ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 36
Provided by: Adm979
Category:

less

Transcript and Presenter's Notes

Title: CSCE 3100 Data Structures and Algorithm Analysis


1
CSCE 3100Data Structures and Algorithm Analysis
  • Heaps
  • Reading Chap.6 Weiss

2
Heaps
  • A heap is a binary tree T that stores a
    key-element pairs at its internal nodes
  • It satisfies two properties
  • MinHeap key(parent) ? key(child)
  • OR MaxHeap key(parent) gt key(child)
  • all levels are full, except
  • the last one, which is
  • left-filled

3
What are Heaps Useful for?
  • To implement priority queues
  • Priority queue a queue where all elements have
    a priority associated with them
  • Remove in a priority queue removes the element
    with the smallest priority
  • insert
  • removeMin

4
Heap or Not a Heap?
5
Heap Properties
  • A heap T storing n keys has height h ?log(n
    1)?, which is O(log n)

6
ADT for Min Heap
objects n gt 0 elements organized in a binary
tree so that the value in each node is at least
as large as those in its children method
Heap Create(MAX_SIZE) create an empty heap
that can hold a
maximum of max_size elements Boolean
HeapFull(heap, n) if (nmax_size) return
TRUE
else return FALSE Heap Insert(heap, item,
n) if (!HeapFull(heap,n)) insert
item into heap and return the
resulting heap
else return error Boolean
HeapEmpty(heap, n) if (ngt0) return FALSE

else return TRUE Element Delete(heap,n) if
(!HeapEmpty(heap,n)) return one
instance of the smallest element in
the heap and
remove it from the heap
else return error
7
Heap Insertion
  • Insert 6

8
Heap Insertion
  • Add key in next available position

9
Heap Insertion
  • Begin Unheap

10
Heap Insertion
11
Heap Insertion
  • Terminate unheap when
  • reach root
  • key child is greater than key parent

12
Heap Removal
  • Remove element
  • from priority queues?
  • removeMin( )

13
Heap Removal
  • Begin downheap

14
Heap Removal
15
Heap Removal
16
Heap Removal
  • Terminate downheap when
  • reach leaf level
  • key child is greater than key parent

17
Bottom-up Heap Construction
  • We can construct a heap storing n given keys
    using a bottom-up construction with log n phases
  • In phase i, pairs of heaps with 2i -1 keys are
    merged into heaps with 2i1-1 keys

2i1-1
18
Merging Two Heaps
  • We are given two two heaps and a key k
  • We create a new heap with the root node storing k
    and with the two heaps as subtrees
  • We perform heapDown to restore the heap-order
    property

7
3
2
5
8
6
4
2
3
4
5
8
6
7
19
Merging Example
Insert 16, 15, 4, 12, 6, 9, 23, 20
Insert 25, 5, 11, 27
20
Example (contd.)
heapDown
21
Example (contd.)
Insert 7, 8
heapDown
22
Example (end)
Insert 10
heapDown
23
Bottom up Heap Construction
16
4
15
12
6
7
23
20
25
5
11
27
9
8
14
24
Bottom up Heap Construction
25
Bottom up Heap Construction
26
Bottom up Heap Construction
27
Bottom up Heap Construction
28
Bottom up Heap Construction
29
Bottom up Heap Construction
30
Bottom up Heap Construction
31
Heap Implementation
  • Using arrays
  • Parent k Children 2k , 2k1
  • Why is it efficient?

4
32
Insertion into a Heap
void insertHeap(element item, int n) int i
if (HEAP_FULL(n)) fprintf(stderr, the
heap is full.\n) exit(1) i
(n) while ((i!1)(item.keyltheapi/2.key))
heapi heapi/2 i / 2
heapi item
2k-1n gt k?log2(n1)?
O(log2n)
33
Deletion from a Heap
element deleteHeap(int n) int parent,
child element item, temp if
(HEAP_EMPTY(n)) fprintf(stderr, The heap
is empty\n) exit(1) / save value of
the element with the highest key / item
heap1 / use last element in heap to adjust
heap / temp heap(n)-- parent 1
child 2
34
Deletion from a Heap (contd)
while (child lt n) / find the smaller
child of the current parent / if
((child lt n) (heapchild.keygtheapchil
d1.key)) child if (temp.key lt
heapchild.key) break / move to the next
lower level / heapparent heapchild
parent child child 2
heapparent temp return item
35
Heap Sorting
  • Step 1 Build a heap
  • Step 2 removeMin( )
  • Running time?
Write a Comment
User Comments (0)
About PowerShow.com