Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

implement a deconstructor for the following code: my current deconstructor is se

ID: 3836057 • Letter: I

Question

implement a deconstructor for the following code: my current deconstructor is seg faulting on me because im deleting stuff that i dont have.. Using c++! thanks!

class Node {
public:
    // constructor with left and right NULL nodes
    Node(char charTemp, int c) {
      ch = charTemp;
      count = c;
      left = NULL;
      right = NULL;
    };

// constuctor used while building the tree
Node(char charTemp, int c, Node *n1, Node *n2) {
    ch = charTemp;
    count = c;
    left = n1;
    right = n2;
};

~Node() {
    delete left;
    delete right;
}

int count; // counting the frequency of the character in the string
char ch;
Node *left;
Node *right;
};

struct Comp { // compare used while building the minheap
bool operator()(const Node *a, const Node *b) {
    return a->count > b->count;
}
};
// our class
class Huffman {

private:
    std::priority_queue<Node*,std::vector<Node*>,Comp> pq; // using standard priority queue
std::vector<char> a;
std::vector<Node*> nodeCounter;
Node *root;

void incrementCount(char inc) {
    // internal function to calculate the frequency of the character in the string
    for (int i = 0; i < nodeCounter.size(); i++) {
      if (nodeCounter.at(i) -> ch == inc) {
        nodeCounter.at(i) -> count++; // incrementing the frequency of the character
        return;
      }
    }
}

public:
    // default constructor
    Huffman() {

    }
    // deconstructor
    ~Huffman() {
      delete root;
      for (int i = 0; i < nodeCounter.size(); i++) {
        delete nodeCounter.at(i);
      }
    }

Explanation / Answer

try to delete the root later the node counter. here is what I would suggest.

#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>

using namespace std;

class Node {
public:
// constructor with left and right NULL nodes
Node(char charTemp, int c) {
ch = charTemp;
count = c;
left = NULL;
right = NULL;
};
// constuctor used while building the tree
Node(char charTemp, int c, Node *n1, Node *n2) {
ch = charTemp;
count = c;
left = n1;
right = n2;
};
~Node() {
delete left;
delete right;
}
int count; // counting the frequency of the character in the string
char ch;
Node *left;
Node *right;
};
struct Comp { // compare used while building the minheap
bool operator()(const Node *a, const Node *b) {
return a->count > b->count;
}
};
// our class
class Huffman {
private:
std::priority_queue<Node*,std::vector<Node*>,Comp> pq; // using standard priority queue
std::vector<char> a;
std::vector<Node*> nodeCounter;
Node *root;
void incrementCount(char inc) {
// internal function to calculate the frequency of the character in the string
for (int i = 0; i < nodeCounter.size(); i++) {
if (nodeCounter.at(i) -> ch == inc) {
nodeCounter.at(i) -> count++; // incrementing the frequency of the character
return;
}
}
}
public:
// default constructor
Huffman() {
}
// deconstructor
~Huffman() {
//delete root;
for (int i = 0; i < nodeCounter.size(); i++) {
delete nodeCounter.at(i);
}
delete root;
}
};

hope this helps. Please try to give the complete code so that it would easier to give you the output also.