Before: Array ``Balanced'' Heaps
Now: Pointer ``Unbalanced'' Heaps but with same property.
Unbalanced: O( )
Approximately the same algorithm but with pointers.
Note: ``Leftist'' Heaps (see text) are different.
Pointer Heaps: minheap1 and minheap2 [125]
Finish Coding: minheap1.c [``Good'' executable is minheap2]
% gcc -o minheap1 minheap1.c > tcc minheap1.c
Same Data File with Test Cases: minheap.dat
% minheap2 minheap.dat
h(elp; q(uit; i(nsert x; D(elete_min; d(isplay_tree
Inorder: visit left, print, then visit right
depth pointer left right element
1 144552 0 0 21
0 144528 144552 144576 13
3 144768 0 0 19
2 144648 144768 0 16
1 144576 144648 144600 14
4 144696 0 0 65
3 144624 144696 0 32
2 144600 144624 144672 24
4 144720 0 0 68
3 144672 144720 144744 26
4 144744 0 0 31
Delete order: 13, 14, 16, 19, 21 24, 26, 31, 32, 65, 68
Pointer Heap Exercises: minheap1.c [126]
struct tree_node {
element_type element;
tree_ptr left;
tree_ptr right;
};
typedef tree_ptr PRIORITY_QUEUE;
PRIORITY_QUEUE create_node(element_type x);
/* Ex1 : if H == NULL, create_node
else if x < root's value then
root gets x; insert root's value on the left
else if x > root's value then
insert x on the right */
PRIORITY_QUEUE insert(element_type x, PRIORITY_QUEUE H) { }
/* Ex2 : return the root's value as an output parameter
if both children are NULL then free the root
else if left child < right child then
delete_min from the left
else delete_min from the right */
PRIORITY_QUEUE delete_min(PRIORITY_QUEUE H, element_type *min_element) { }