subject

The heap implementation covered in class is for a so-called "max-heap" — i. e., one where elements are organized such that the one with the maximum value can be efficiently extracted. This limits our usage of the data structure, however. Our heap can currently only accommodate elements that have a natural ordering (i. e., they can be compared using the '>' and '<' operators as used in the implementation), and there's no way to order elements based on some partial or computed property. To make our heap more flexible, you'll update it to allow a key function to be passed to its initializer. This function will be used to extract a value from each element added to the heap; these values, in turn, will be used to order the elements. We can now easily create heaps with different semantics, e. g., Heap(len) will prioritize elements based on their length (e. g., applicable to strings, sequences, etc.) Heap(lambda x: -x) can function as a min-heap for numbers Heap(lambda x: x. prop) will prioritize elements based on their prop attribute If no key function is provided, the default max-heap behavior should be used — the "lambda x:x" default value for the __init__ method does just that. You will, at the very least, need to update the _heapify and add methods, below, to complete this assignment. (Note, also, that pop_max has been renamed pop, while max has been renamed peek, to reflect their more general nature.)

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 18:00
Ihave buncha points. does anyone want any?
Answers: 1
question
Computers and Technology, 22.06.2019 11:00
The great length of north america causes the climate to be varied. true false
Answers: 2
question
Computers and Technology, 23.06.2019 20:30
If chris has a car liability insurance, what damage would he be covered for
Answers: 1
question
Computers and Technology, 24.06.2019 10:30
This device directs network traffic. bridge hub nic repeater router switch
Answers: 3
You know the right answer?
The heap implementation covered in class is for a so-called "max-heap" — i. e., one where elements a...
Questions
question
Mathematics, 01.11.2019 17:31