Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Huffman Tree and Pointers
    primarykey
    data
    text
    <p>I am creating a Huffman Tree and to do so I started with making a Min Heap. The heap is set up and works sorting the values by frequency in the document but my problem comes when I try to start creating the tree.</p> <p>I am popping the top two items off the heap and putting a node above them and reinserting into the heap. The heap is array based so it does not touch the *left and *right pointers of the nodes. When the heap is down to only one node however both it's left and right node pointers are null so I believe it may be an issue with my pointers...? I am new to c++ from java for give my dumb mistakes.</p> <pre><code> while(theHeap.getheapSize() &gt; 1) { Node top; Node *min1=new Node(theHeap.topandPop()); Node *min2=new Node(theHeap.topandPop()); top.left=min1; top.right=min2; top.freq=min1-&gt;freq+min2-&gt;freq; theHeap.insert(top); } Node r=theHeap.topAndPop(); //null pointers for left and right children -------------------------------------- //code for heap. arr is in the header file is Node *arr; void Heap::insert(Node c) { if (heapSize != arrSize) { heapSize=heapSize+1; arr[heapSize - 1] = c; //does this call copy constructor??? percolateUp(heapSize - 1); } } void Heap::percolateUp(int nodeIndex) { int parentIndex; Node tmp; if (nodeIndex != 0) { parentIndex = getParentPos(nodeIndex); if (arr[parentIndex].freq &gt; arr[nodeIndex].freq) { tmp = arr[parentIndex]; arr[parentIndex] = arr[nodeIndex]; arr[nodeIndex] = tmp; percolateUp(parentIndex); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload