Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is the heap structure in Python. How do I implement the heapify and the add function without changing anything else? Heaps Overview For this

image text in transcribed

image text in transcribed

image text in transcribed

This is the heap structure in Python. How do I implement the heapify and the add function without changing anything else?

Heaps Overview For this assignment you will start by modifying the heap data stucture implemented in class to allow it to keep its elements sorted by an arbitrary priority (identified by a key function), then use the augmented heap to efficiently compute the running median of a set of numbers. 1. Augmenting the Heap with a key function 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 l > i 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) wll 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.) Heaps Overview For this assignment you will start by modifying the heap data stucture implemented in class to allow it to keep its elements sorted by an arbitrary priority (identified by a key function), then use the augmented heap to efficiently compute the running median of a set of numbers. 1. Augmenting the Heap with a key function 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 l > i 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) wll 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.)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Databases With Application To GIS

Authors: Philippe Rigaux, Michel Scholl, Agnès Voisard

1st Edition

1558605886, 978-1558605886

More Books

Students also viewed these Databases questions

Question

What is the basis for Security Concerns in Cloud Computing?

Answered: 1 week ago

Question

Describe the three main Cloud Computing Environments.

Answered: 1 week ago