next up previous contents
Next: Sorting Up: Heap/Priority Queue: ADT Previous: Array Heaps: Percolate Up   Contents

Pointer Heaps: Introduction [124]




\begin{picture}(748,213)(6,626)
\thicklines\put( 60,800){\circle{28}}
\put( 20,7...
...t(570,715){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm 19}}}
\end{picture}



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) { }



Ted Billard 2001-10-25