heap question

This is a discussion on heap question within the C Programming forums, part of the General Programming Boards category; i tried really hard to avoid posting this question but i cant seem to spot the problem so here it ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    71

    heap question

    i tried really hard to avoid posting this question but i cant seem to spot the problem

    so here it is..
    basically i want to print the highest number of 'search_counts' from a heap.
    > there is an array of struct HEAP
    > there is only 1 element in HEAP , it is DATA *dataPtr , basically it points to a struct DATA (which has been allocated memory dynamically)
    > i have to delete the top most item from the heap ary and print it and then reheapDown to get it back to a heap form.

    problem: it prints all but the last element correctly. for the last element it prints out garbage.

    i have been through the debugger so many times but yet it failed.

    here is the code

    int j is the count of how many elements were inserted into the heap ary
    int whatFn is basically a checker to see that if the number of elements inserted was less than the HEAP_SIZE which is 7 then use j otherwise use HEAP_SIZE for the restrictor

    Code:
    void printHeap1 (HEAP *ary, int j, int whatFn)
    {
    	int i = 0;
    	HEAP *pWalker, *pCur, *pLast, dataOut;
    
    	pCur = ary;
    
    	printf("--- 7 Most popular searches ---\n");
    
    	if (whatFn == NOT_EQUAL)
    	{
    		pLast = &ary[j];
    		j--;
    		for (pWalker = pCur; pWalker < pLast; pWalker++)
    		{
    			if (deleteHeap (ary, &j, &dataOut) == 1)
    				printToScreen (&dataOut, "heap");
    			else
    				printf("Unable to delete node\n");
    		}
    	}
    	else
    	{
    		pLast = &ary[HEAP_SIZE -1];
    		j--;
    		for (pWalker = pCur; pWalker < pLast; pWalker++)
    		{
    			if (deleteHeap (ary, &j, &dataOut) == 1)
    				printToScreen (&dataOut, "heap");
    			else
    				printf("Unable to delete node\n");
    		}
    	}
    	return;
    }
    this goes to the delete heap function that is

    Code:
    int deleteHeap (HEAP *heap, int *last, HEAP *dataOut)
    {
    //	int item;
    
    	if (*last < 0)
    	{
    		return 0;
    	}
    
    	dataOut->dataPtr = heap[0].dataPtr;
    	heap [0].dataPtr = heap [*last].dataPtr;
    //	heap[*last].dataPtr = NULL;
    	(*last)--;
    
    	reheapDown (heap, 0, *last);
    
    	return 1;
    }
    this later goes to the reheapDown function which is

    Code:
    void reheapDown (HEAP *heap, int root, int last)
    {
    	DATA hold, leftKey, rightKey, largeChildKey;
    	int largeChildIndex = 0;
    	//int parent;
    
    	if ((root * 2 + 1) <= last)
    	{
    		leftKey = *(heap [root * 2 + 1].dataPtr);
    
    		if ((root * 2 + 2) <= last)
    		{
    			rightKey = *(heap [root * 2 + 2].dataPtr);
    		}
    		else
    		{
    			rightKey.search_count = -1;
    		}
    
    		if (leftKey.search_count > rightKey.search_count)
    		{
    			largeChildKey = leftKey;
    			largeChildIndex = root * 2 + 1;
    		}
    		else
    		{
    			largeChildKey = rightKey;
    			largeChildIndex = root * 2 + 2;
    		}
    
    		if (heap[root].dataPtr->search_count < heap [largeChildIndex].dataPtr->search_count)
    		{
    			hold = *(heap [root].dataPtr);
    			heap [root].dataPtr = heap [largeChildIndex].dataPtr;
    			heap [largeChildIndex].dataPtr = &hold;
    
    			reheapDown (heap, largeChildIndex, last);
    		}
    	}
    	return;
    }
    any form of help is appreciated. i just need someone to spot the error. i can fix the rest myself
    thanks

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    71
    dont worry about it

    i fixed it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. heap vs stack memory question
    By donglee in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2009, 04:34 PM
  2. Understanding the stack and heap
    By yougene in forum C Programming
    Replies: 4
    Last Post: 01-19-2009, 05:22 AM
  3. Basic Heap Question?
    By justConfused in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 12:26 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. I have a Question about memory usage for C.
    By bobthefish3 in forum C Programming
    Replies: 34
    Last Post: 12-24-2001, 04:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21