Haskell代写: 实现MaxHeap

在给定的min heap实现上重写一个max heap, 在此基础上改写一系列heap上的操作。

Altering heaps to do new things

Altering heaps to do new things

In this homework you will alter several programs we have written in class to perform new things.

  • Alter the functions indexOfMaxChild, deleteMax, reHeapDown insertHeap, and reHeapUp from the file Heaps.hs so that they implement Min-heaps instead of Max-heaps.
  • Alter the functions heapSort2, toHeap2, fromHeap2, from the file HeapSort.hs so that they use the altered functions from part (1) above. This will allow you to write a function that sorts an array in decreasing order rather than increasing order.
  • Alter the functions from the file Heaps.hs that implement heaps using leftist trees so that they manipulate data composed of keys and satellite data. The functions will now have different contracts which I list below.
     empty:: (LHeap a)
     isEmpty:: LHeap a -> Bool
     insert:: Ord key => (key,sat) -> LHeap (key,sat) -> LHeap (key,sat)
     merge:: Ord key => LHeap (key,sat) -> LHeap (key,sat) -> LHeap (key,sat)
     findMin:: Ord key => LHeap (key,sat) -> Maybe (key,sat)
     deleteMin:: Ord key => LHeap (key,sat) -> Maybe(LHeap (key,sat))

The point is that heap properties should apply only to the key part of the data.

What to do

  • Cut and paste the program pieces from the files Heaps.hs and HeapSort.hs.
  • Alter the functions that you have cut and pasted so that they implement the features described above.
  • Be sure and rename functions so that names of the altered functions describe what the functions do. I will assign points according to how well you name your functions.
  • I suggest you work in stages, cutting and pasting only a few pieces of a time.
  • Be sure and create test harnesses that test your code. Test it as you proceed, so that you know each piece works, before you alter new pieces that depend upon previously altered pieces,
  • Create a main function that illustrates that your code works.

What to turn in.

  • Create a file by cutting and pasting from the files Heaps.hs and HeapSort.hs.
  • Include your name (as the author of the program) on the very first line
  • Alter the cut and pasted functions to perform the features outlined above.
  • Be sure you include tests
  • Uploaded the file to blackboard
  • Due before class on Tuesday , December 1, 2009.

Back to the Daily Record.

Back to the class web-page.

kamisama wechat
KamiSama