Thread: a 4-heap implementation..

  1. #1
    Compiling
    Join Date
    Jun 2003
    Posts
    69

    a 4-heap implementation..

    Hello, all:
    I wrote a function to implement a 4-heap with 100 data in it. But it cannot work correctly.
    the step is: first, read the data from a file and put them in to an array one by one. Second, use the percolate down (slide) algorithm to adjust the heap, and the relationship between the parents and the children is "the left most child = parent * 4 - 2", so the function is (without the data-read part):
    Code:
    void
    Im4Heap(int si, int i, PriorityQueue H)
    {
      int temp, num, pointer, child, start, end;
      temp = H->Elements[i];
      H->Size = si; 
      for (i; i * 4 - 2 <= H->Size; i = child)
        {
          start = i * 4 - 2;
          end = i * 4 + 1;
          child = i * 4 - 2;
          num = Smallest(&H->Elements[start], &pointer, start, end);
          if (temp > num)
    	H->Elements[i] = H->Elements[pointer];
          else
    	break;
        }
      H->Elements[i] = temp;
    }
    The "Smallest" function is used for finding the smallest value in an array:
    Code:
    int
    Smallest(int array[], int *i, int start, int end)
    {
      int num;
      num = array[start];
      for(*i = start; *i <= end; *i++)
        {
          if(array[*i] < num)
    	num = array[*i];
        }
      printf("%d\n", num);
      return num;
    }
    Thank you very much!

  2. #2
    Compiling
    Join Date
    Jun 2003
    Posts
    69
    could anyone helps me?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. heap
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 11-10-2007, 11:49 PM
  2. Heap Work
    By AndyBomstad in forum C++ Programming
    Replies: 1
    Last Post: 05-16-2005, 12:09 PM
  3. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  4. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 05:03 AM
  5. stach and heap.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 01-26-2002, 09:37 AM