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:40
Access defaults to searching for information on the
Answers: 1
question
Computers and Technology, 22.06.2019 00:30
Jenny wants to look at row 345 and compare it to row 17. what can she do if she wanted to easily adjust to see both at once?
Answers: 3
question
Computers and Technology, 23.06.2019 08:30
When you interpret the behavior of others according to your experiences and understanding of the world your evaluation is
Answers: 1
question
Computers and Technology, 23.06.2019 13:30
Best laptops for college [$100-$500 range]?
Answers: 2
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
Biology, 04.11.2019 04:31
Questions on the website: 13722362