Python: Delete the smallest element from the given Heap and then inserts a new item
Python heap queue algorithm: Exercise-5 with Solution
Write a Python program that deletes the smallest element from a heap and then inserts a new item.
import heapq as hq heap = [25, 44, 68, 21, 39, 23, 89] hq.heapify(heap) print("heap: ", heap) hq.heapreplace(heap, 21) print("heapreplace(heap, 21): ", heap) hq.heapreplace(heap, 110) print("heapreplace(heap, 110): ", heap)
heap: [21, 25, 23, 44, 39, 68, 89] heapreplace(heap, 21): [21, 25, 23, 44, 39, 68, 89] heapreplace(heap, 110): [23, 25, 68, 44, 39, 110, 89]
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python function which accepts an arbitrary list and converts it to a heap using Heap queue algorithm.
Next: Write a Python program to sort a given list of elements in ascending order using Heap queue algorithm.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join