Question: Define the percolate_down(heap)function which takes a Python list as a parameter. The Python list represents a binary heap with one new root value (as in

Define the percolate_down(heap)function which takes a Python list as a parameter. The Python list represents a binary heap with one new root value (as in the diagram above) - this new value will always be at index 1 of the Python list (as index 1 represents the root of the binary heap). The function restores the order property by repeatedly swapping this new value with its smaller child - a process referred to as percolating the value down the heap. Once this process is finished, the Python list should represent a valid binary heap again.

Define the percolate_down(heap)function which takes a Python list as a parameter. The

Consider the following binary heap into which a value, 100, has just been used to replace the old root node (you can assume the old root node was removed). This new root value is circled in red. To maintain the shape property of the binary heap when the root is removed, this new value (originally the last leaf of the tree, i.e. right child of 22) replaces the root value. However, the order property for binary heaps is now violated and needs to be corrected to restore the binary heap. This can be fixed by continually swapping the new value with its smaller child until the order property is restored. In this case, the 100 and 7 need to be swapped, then the 100 and 9, and finally the 100 and 16. Define the percolate_down (heap)function which takes a Python list as a parameter. The Python list represents a binary heap with one new root value (as in the diagram above) - this new value will always be at index 1 of the Python list (as index 1 represents the root of the binary heap). The function restores the order property by repeatedly swapping this new value with its smaller child - a process referred to as percolating the value down the heap. Once this process is finished, the Python list should represent a valid binary heap again. You should count how many swaps are performed to restore the property - and then return this count. NOTE: You may like to re-use the get_min_child_index(function defined in an earlier question (you will need to submit this function as well as the percolate_down() function). The header of the function is: def percolate_down (heap): For example: Test Result values = [0, 100, 7, 8, 9, 22, 45, 12, 16, 27, 36] Binary heap = [0, 7, 9, 8, 16, 22, 45, 12, 100, 27, 36] swaps - percolate_down(values) Swaps = 3 print('Binary heap =', values)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!