Skip to main content
Logo image

Section 6.10 Binary Heap Operations

The basic operations we will implement for our binary heap are as follows:
  • BinaryHeap() creates a new empty binary heap.
  • insert(k) adds a new item to the heap.
  • get_min() returns the item with the minimum key value, leaving the item in the heap.
  • delete() returns the item with the minimum key value, removing the item from the heap.
  • is_empty() returns True if the heap is empty, False otherwise.
  • size() returns the number of items in the heap.
  • heapify(list) builds a new heap from a list of keys.
Listing 6.10.1 demonstrates the use of some of the binary heap methods. Notice that no matter what order we add items to the heap, the smallest is removed each time.
Listing 6.10.1.
You have attempted 1 of 2 activities on this page.