|
1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
- Construct (initialize) a max heap object using data elements of an array
A
- Build a max heap object from an array A of n data elements
- Destroy (delete) a heap object and an array A
- Compare data elements
- Swap data elements
- Rebuild a max heap
- Add/insert an element into a max heap object
- Delete an element from a max heap object
- Do a heap sort for an array A using a max heap object
- Display (print) a heap object at each iteration of heap sort
- Print a max heap object
|
|
6
|
- #define MAX_ELEMENTS 200 /* maximum heap size */
- /* note: the heap is defined in an array that goes from index value 0 to
index value MAX_ELEMENTS 1. The
parameter n keeps track of the next index slot where one can insert a
new element into the heap */
- #define HEAP_FULL(n) (n == MAX_ELEMENTS)
- #define HEAP_EMPTY(n) (!n)
- typedef struct (
- int key;
- /* other fields */
- ) element;
- element heap(MAX_ELEMENTS) ;
- int n = 0;
|
|
7
|
|
|
8
|
|
|
9
|
- Insert max_heap (element, next_pos_avail)
- Put element at the end of the heap (in next_pos_avail);
- While element is not the root and element > parent
- Swap element with its parent
|
|
10
|
- Void insert_max_heap(element item, int * n)
{/* insert item into a max heap of current size *n */
int i;
if (HEAP_FULL(*n)){
fprintf(stderr, The heap is full. \n);
exit(1);}
- i = (*n);
while((i != 0)
- && (item.key > heap[(ROUND(i/2)-1)].key))
- {heap[i] = heap[(ROUND(i/2)-1)];
- i = ROUND(i/2) -1;}
heap[i] = item;
}
|