Pointers - Weird new values

I have the following code that is supposed to perform a pop operation on a minheap that contains TreeNode*'s. At the point that I am looking at the program through gdb, the values of heap[1]->left and heap[1]->right should be NULL. However, while popping this specific time, heap[1]'s values remain the same until right before,
min->right = heap[1]->right;
Then I cannot access any values and the program seg faults. Does anyone have any ideas why this might be happening?

Here's the code...

void MinHeap::pop(TreeNode *min)
{
TreeNode *LastE;

min->ascii = heap[1]->ascii;
min->frequency = heap[1]->frequency;
min->left = heap[1]->left;
min->right = heap[1]->right;
Unless it's something simple like you should be using [0] instead of [1], you probably need to post at least the whole MinHeap class and implementation. Some driver code with complete headers and all in one file would be nice too.
Last edited on
Here is the whole heap.h file.

#ifndef HEAP_H
#define HEAP_H

#include <iostream>

using namespace std;

class MinHeap;
class TreeNode;

class TreeNode{
public:
int ascii, frequency;
TreeNode *left, *right;

int getAscii(){
return ascii;
}
int getFrequency(){
return frequency;
}
TreeNode(){
//cout << "constructor called" << endl;
ascii = 0;
frequency = 0;
left = NULL;
right = NULL;
//cout << "end constructor" << endl;
}
TreeNode(TreeNode *leftNode, TreeNode *rightNode, int freq){
left = leftNode;
right = rightNode;
frequency = freq;
ascii = 0;
}
~TreeNode(){cout << "called" << endl;}
};


class MinHeap{
private:
int capacity;
int heapSize;
public:
TreeNode **heap;
MinHeap(){}
MinHeap(int numCharacters){
capacity = numCharacters + 1;
heapSize = 0;
heap = new TreeNode* [capacity + 2];
for(int i = 0; i < capacity + 2; i++)
heap[i] = new TreeNode(NULL, NULL, 0);
}
/*~MinHeap(){
delete [] heap;
}*/
void insert(int asciiVal, int freq, TreeNode *left, TreeNode *right);
void pop(TreeNode *min);
int getHeapSize(){
return heapSize;
}
};



#endif


Here is the function in main.cpp where I'm calling the pop

void huffman(MinHeap &freqHeap)
{
TreeNode *first, *second, *bt;

int size = freqHeap.getHeapSize();
for(int i = 0; i < size; i++)
{
freqHeap.pop(first);
freqHeap.pop(second); <---Crashes on this pop
bt->left = second;
bt->right = first;
bt->frequency = (first->frequency + second->frequency);

freqHeap.insert(bt->ascii, bt->frequency, bt->left, bt->right);
}
}



Here is the pop and the functions that pop calls:


void MinHeap::pop(TreeNode *min)
{
TreeNode *LastE;

min->ascii = heap[1]->ascii;
min->frequency = heap[1]->frequency;
min->left = heap[1]->left;
min->right = heap[1]->right;

LastE->ascii = heap[heapSize]->ascii;
LastE->frequency = heap[heapSize]->frequency;
LastE->left = heap[heapSize]->left;
LastE->right = heap[heapSize]->right;
heapSize--;

heap[heapSize+1]->ascii = 0;
heap[heapSize+1]->frequency = 0;
heap[heapSize+1]->left = NULL;
heap[heapSize+1]->right = NULL;

int currentNode = 1;
int child = 2;

while(child <= heapSize)
{
if(child < heapSize && heap[child]->frequency >= heap[child + 1]->frequency)
{
if((heap[child]->frequency == heap[child + 1]->frequency) && (minAscii(heap[child]) > minAscii(heap[child + 1])))
child++;
else if(heap[child]->frequency > heap[child + 1]->frequency)
child++;
}

if(LastE->frequency <= heap[child]->frequency)
break;

heap[currentNode]->ascii = heap[child]->ascii;
heap[currentNode]->frequency = heap[child]->frequency;
heap[currentNode]->left = heap[child]->left;
heap[currentNode]->right = heap[child]->right;

currentNode = child;
child *= 2;
}
heap[currentNode]->ascii = LastE->ascii;
heap[currentNode]->frequency = LastE->frequency;
heap[currentNode]->left = LastE->left;
heap[currentNode]->right = LastE->right;
}

int minAscii(TreeNode *node)
{
int min = 400;

DFS(node, min);

return min;
}

void DFS(TreeNode *node, int &min)
{
if(node->left == NULL && node->right == NULL && node->ascii < min)
min = node->ascii;
else
{
if(node->left != NULL)
DFS(node->left, min);
if(node->right != NULL)
DFS(node->right, min);
}
}





Topic archived. No new replies allowed.