1

So I'm to write a heap data structure for class, and it's supposed to implement heap sort using only insert and delete operations.

It works .. for smaller sets of ints (size 10 and below). However, when I input a large set of numbers(100+) in, the result is only semi sorted..

Here's my insert method:

 void insert(heapHndl H, int priority) {
     // Makes sure heap has enough room for new element.
     assert(!isFull(H));
     // Increase the current size.
     H->currentSize++;
     // Add to array.
     int pos = H->currentSize;
     // Work up.
     while (pos > 1 && priority < H->array[pos/2]) {
         swap (H, pos, pos/2);
         pos /= 2;
     }
     H->array[pos] = priority;
 }

Here's my delete method:

 void deleteMax(heapHndl H) {
     // Makes sure the heap is not empty.
     assert(!heapEmpty(H));

     int root = 1;
     // Swap the root and last element.
     swap(H, root, H->currentSize);
     H->array[H->currentSize] = -1;
     H->currentSize--;

     // Work your way down from the root.
     while(root != H->currentSize) {
         int leftChild = 2*root;
         int rightChild = 2*root + 1;
         int minChild;
         // If root is smaller than children..
         if (H->array[root] <= H->array[leftChild] &&
                 H->array[root] <= H->array[rightChild])
             break;
         // If at a leaf node.
         if (rightChild > H->currentSize){
             if (leftChild >= H->currentSize)
                 break;
             else minChild = leftChild;
         } else {
             // Determines which child to swap with.
             if (H->array[leftChild] <= H->array[rightChild])
                 minChild = leftChild;
             else minChild = rightChild;
         }
         swap(H, root, minChild);
         root = minChild;
     }
 }

Here's heapHndl.

 typedef struct HeapStruct {
     int maxSize;
     int currentSize;
     int *array;
 } HeapStruct;

typedef HeapStruct* heapHndl;

I can't seem to figure out what other cases I'm missing for the delete method. I'm pretty sure my insert method is fine (manual checking). Thanks for the help. Edit: ignore the name, deleteMax. It's actually a min heap. Root is the smallest integer.

5
  • Can we see the details of heapHndl ? int root = 1; is very suspicious. Commented May 2, 2014 at 4:17
  • Int root = 1, because the array starts at 1. That way, leftChild = 2*root does not = 0, if root = 0. Commented May 2, 2014 at 4:22
  • I do not see where you are allocating memory for HeapStruct member int *array; and yet I see statements like: ` H->array[pos/2])` and ` H->array[pos/2])` Have you used any malloc() or calloc() calls? If so, how much memory did you allocate? Commented May 2, 2014 at 4:44
  • @ryyker, tmp->array = malloc(sizeof(int) * (maxSize +1 )); Commented May 2, 2014 at 4:48
  • @user3288944 - In the context of your post, there are no m(c)alloc() statements. Nor is there a tmp->array. It would help to see them in context. You might re-think how you are presenting this question, and after doing some of your own debugging, if the problem persists, re-post. Here are some suggestions on how to improve a post. Commented May 3, 2014 at 21:36

1 Answer 1

1

The following two lines are definitely a problem

     if (leftChild > H->currentSize || rightChild > H->currentSize)
         break;

The loop is not allowed to break if one of the children is missing, only if both children are missing. If the right child is missing, you must still check, and potentially swap with the left child.


Edit: On the other hand, if the parent node is the least of the three (parent,left,right), then the loop does need to break.

Sign up to request clarification or add additional context in comments.

11 Comments

I fixed that, but still the same problem. Any other suggestions? Thanks. Is a while loop possible? Or should I make it a recursive function?
@user3288944 You can definitely do it with a while loop. Can you append the modified code to the question?
@user3288944 I've updated the answer with another observation.
If my insert function is implemented properly, is it ever possible for the parent node to be smaller than its child?
Update: That fixed the problem. So I guess it is possible for the parent to be smaller than its child.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.