Professional Documents
Culture Documents
Dhammika Elkaduwe
Dhammika Elkaduwe
Dhammika Elkaduwe
Min-heap example
Example of 2 possible heaps on numbers from 0 to 8.
Dhammika Elkaduwe
Put the new value in the next position at the bottom of the
heap (do not worry about the ordering now)
Building a min-heap
Dhammika Elkaduwe
I
I
Dhammika Elkaduwe
Dhammika Elkaduwe
Dhammika Elkaduwe
Dhammika Elkaduwe
If you have two children and the root is big replace it with the
smaller of the two children
void bubble_down(heap_t h)
{
int *data = h -> data;
int i = 0;
int last = h -> size - 1;
/* implement the rest! */
}
Dhammika Elkaduwe
Dhammika Elkaduwe
We can sort a given data set using a heap: add all elements
to the heap, and then remove. Elements will be removed in
sorted order.
Dhammika Elkaduwe
Heaps in Java
Home work:
I
Dhammika Elkaduwe