CS1102 Tut 8 Heaps - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

CS1102 Tut 8 Heaps

Description:

public Comparable find2ndMax( ) if (currentSize = 1) throw new NoSuchElementException ... void updateKey ( int p, Comparable newVal ) { if (p (currentSize-1) ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 33
Provided by: dcsa
Category:
Tags: comparable | cs1102 | heaps | tut

less

Transcript and Presenter's Notes

Title: CS1102 Tut 8 Heaps


1
CS1102 Tut 8 Heaps
  • Max Tan
  • tanhuiyi_at_comp.nus.edu.sg
  • S15-03-07 Tel65164364http//www.comp.nus.edu.sg
    /tanhuiyi

2
First 15 minutes
  • Priority Queue ADT
  • Heap Data structure that realizes the P-Queue
    ADT
  • Question Must a P-Queue be a heap?

3
First 15 minutes
  • Priority Queue ADT
  • A queue which dequeues items with priority first
  • Implementation
  • Unsorted array
  • Sorted array
  • Heap
  • and many others!

4
First 15 minutes
  • How heapSort works

last
26
  • Pick the largest,
  • Swap it with the last element of the array

7
18
3
4
16
15
2
1
5
First 15 minutes
  • How heapSort works

last
2
  • Re-establish heap property (bubble down)

7
18
3
4
16
15
1
6
First 15 minutes
  • How heapSort works

last
18
  • Select max node, swap with last

7
16
3
4
2
15
1
7
First 15 minutes
  • How heapSort works

last
1
  • Bubble down

7
16
3
4
2
15
8
First 15 minutes
  • How heapSort works

last
16
  • Select biggest and swap

7
15
3
4
2
1
9
First 15 minutes
  • How heapSort works

last
1
  • Select biggest and swap
  • Repeat until done!
  • Hence this is an INPLACE sorting algorithm

7
15
3
4
2
10
First 15 minutes
  • Is heapSort STABLE ? (preserve the same order?)

11
First 15 minutes
  • Is heapSort STABLE ? (preserve the same order?)
  • Simple example

11
11
12
13
12
13
12
Groups Assignment
  • Question 1 Group 4
  • Question 2 Group 1
  • Question 3 Group 2
  • Question 4 Group 3
  • Question 5 Group 4
  • Question 6 Group 1

13
Groups Assignment
  • Question 1 Group 1
  • Question 2 Group 2
  • Question 3 Group 3
  • Question 4 Group 1
  • Question 5 Group 2
  • Question 6 Group 3

14
Q1 (a) Answer
  • Show the result of
  • (a) inserting 12, 10, 1, 14, 6, 5, 8, 15, 3, 9,
    7, 4, 11, 13, and 2, one at a time, in an
    initially empty heap.

15
Q1 (b) Answer
  • (b) Then show the result by using the heap
    construction algorithm instead.

16
Q2
  • Given the following heap class definition
  • class BinaryHeap
  • int currentSize //maintain the number of
    elements in the heap
  • Comparable array //stores the heap
    elements
  • BinaryHeap() //constructor
  • currentSize 0
  • array new Comparable DEFAULT_CAPACITY
  • // with other standard heap methods ...
  • you are required to write two methods,
  • (a) isHeap(), which verifies the heap property,
    and
  • (b) findSecondMax(), which returns the second
    largest elements in the heap.

17
Q2a
  • Heap property
  • Complete and parent gt both children
  • Can either check from Top down, or Bottom up

18
Q2 (a) Answer (1)
  • public boolean isHeap() return isHeap(0)
  • private boolean isHeap(int i) // max heap
  • if (2i1 gt currentSize - 1) return
    true
  • if (arrayi.compareTo(array2i1) gt 0)
    // gt left child
  • if (2(i1)lt currentSize) // has right
    child
  • if (arrayi.compareTo(array2(i1)) gt
    0) // gt right child
  • return isHeap(2i1)
    isHeap(2(i1))
  • else return false // lt right
    child
  • else return true // 2i1 currentSize
    1, has only left child
  • else return false // lt left child

19
Q2 (a) Answer (2)
  • Alternative solution
  • public boolean isHeap()
  • for (int icurrentSize-1 igt1 i--)
  • if (arrayi.compareTo(array(i-1)/2)gt0)
    return false
  • return true

20
Q2 (b)
  • Where can you find the 2nd largest node?

21
Q2 (b) Answer
  • public Comparable find2ndMax( )
  • if (currentSize lt 1)
  • throw new NoSuchElementException()
  • else if (currentSize 2)
  • return array1
  • if (array1.compareTo(array2) gt 0)
  • return array1
  • else
  • return array2

22
Q2 (b)
  • What is the time complexity?
  • How about the 3rd largest?
  • How about the kth largest?

23
Q3
  • Using the same heap definition in Q2, write
    two more methods as follows (assume you were
    given bubbleUp (p) and bubbleDown (p) which
    bubble up and down respectively at position p)
  • (a) updateKey (p, v), which changes the value of
    the key at position p to v.
  • (b) delete (p), which deletes the key at position
    p of the heap.

24
Q3 updateKey()
  • What can happen to the old value after calling
    updateKey()
  • Either increase or decrease

IF oldvalue increases Would you need to bubble
down?
Call updateKey on this node
25
Q3 updateKey()
  • What can happen to the old value after calling
    updateKey()
  • Either increase or decrease

IF oldvalue decreases Would you need to bubble
up?
Call updateKey on this node
26
Q3 (a) Answer
  • public void updateKey ( int p, Comparable newVal
    )
  • if (p gt (currentSize-1) p lt 0)
  • throw new NoSuchElementException()
  • int c newVal.compareTo(arrayp)
  • arrayp newVal
  • if (c gt 0)
  • bubbleUp(p)
  • else if (c lt 0)
  • bubbleDown(p)

27
Q3 (b) Answer (1)
  • public void delete( int p )
  • if (p gt (currentSize-1) p lt 0)
  • throw new NoSuchElementException()
  • currentSize--
  • int c arrayp.compareTo(arraycurrentSize)
  • arrayp arraycurrentSize
  • if (c gt 0)
  • bubbleDown(p)
  • else if (c lt 0)
  • bubbleUp(p)

28
Q3 (b) Answer (2)
  • Alternative solution
  • Do the update/delete and
  • Just call both bubbleUp(p) bubbleDown(p)

29
Q4
  • Mr. B. C. Dull claims to have developed a new
    data structure for priority queues that supports
    the operations Insert and Extract-Max, all in
    O(1) worst-case time. Show Mr. Dull that it is
    impossible.

30
Q4 Answer
  • n insert operations followed by n Extract-Max
    operations will take only O(n), which means that
    a sequence can be sorted in O(n), which is
    impossible.

31
Q5
  • Q5. Which of the following is true about the
    heapsort?
  • the heapsort does not require a second array
  • the heapsort is more efficient than the mergesort
    in the worst case
  • the heapsort is more efficient than the mergesort
    in the average case
  • the heapsort is better than the quicksort in the
    average case
  • Answer 1

32
Q6
  • Q6. The heapsort is ______ in the worst case.
  • O(n)
  • O(log n)
  • O(n log n)
  • O(n2 )
  • Answer 3
Write a Comment
User Comments (0)
About PowerShow.com