Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8301325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:53:52+00:00 2026-06-08T16:53:52+00:00

I created a program which, so far, creates a binary tree of nodes for

  • 0

I created a program which, so far, creates a binary tree of nodes for a Huffman coding program. For some reason, when the debugger gets to the point where it tests if the two children equal Null or not (so there an actual character on the tree and not just a parent), the program freezes. Codes is an array of the codes for each object comprising of Code structs which have two member variables.

void Huff::traverse(Node* root, vector<Code>* code, string code_base){
    if(root->childL == NULL && root->childR == NULL){ //stops here
        Code c;
        c.content = root->litteral;
        c.symbol = code_base;
        code->push_back(c);
    }else{
        if (root->childL != NULL)
            traverse(root->childL, code, code_base + '0');
        if (root->childR != NULL)
            traverse(root->childR, code, code_base + '1');
    }
}

The function which calls this one is (it’s called towards the end):

vector<Code>* Huff::compress(){
    //-------GETTING WEIGHTS/FREQUENCIES------
    vector<Node *>* nodes = new vector<Node*>; // Vector of nodes for later use
    map<char, int>* freq = new map<char, int>; //  Map to find weight of nodes
    for(unsigned int i = 0; i < content.length(); i++)
        (*freq)[content[i]]++; 
    CopyTo copyto(nodes); //sets vector<Node*> to copy to 
    for_each(freq->begin(), freq->end(), copyto); // Copies 
    delete freq;
    vector<Node *>::iterator beg = nodes->begin();

    //-------SETTING UP TO BUILD TREE------
    if(nodes->size() % 2 == 1){ //makes sure there are an even number of nodes
        Node* fill = new Node;
        fill->set_node(0, '*', NULL, NULL);
        nodes->push_back(fill);
    }
    huff_sort(nodes); // sort nodes by weight

    //-------BUILDING TREE------
    while(nodes->size() != 1){ //Sorts nodes by weight and then removes two of them and replaces them with one
        int w= (**beg).weight + (**(beg+1)).weight;
        Node* p = new Node;
        p->set_node(w, '*', *nodes->begin(), *(nodes->begin()+1)); //making it the parent node of the two lowest nodes
        nodes->erase(nodes->begin(), nodes->begin()+2);
        unsigned int i = 0;
        while(w > (*nodes)[i]->weight && i <= nodes->size()){ //finds where to insert the parent node based on weight
            i++;
        }
        if(i > nodes->size()) //if it needs to be inserted at the end
            nodes->push_back(p);
        else
            nodes->insert(nodes->begin()+i, p);
        delete p;
    }

    //-------TRAVERSING TREE------
    Node* root = (*nodes)[0];
    delete nodes;
    vector<Code>* codes;
    traverse(root, codes , "");
    delete root;
    return codes;
}

Note: The while loop before the traverse tree block of code is where the tree is created

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:53:55+00:00Added an answer on June 8, 2026 at 4:53 pm

    You need to say delete nodes; after you call traverse. What you have right now makes root point to NULL before you call traverse.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writting a small graphics program using directx9 which imports some model files created
I have created a program which allows me to upload images to my server.
I created a Java program which sends an email from my gmail account. I
I've created a program in Eclipse / MinGW / C (project type: C) which
I have created a concurrent, recursive directory traversal and file processing program, which sometimes
I have a program which creates JButtons which are then added to a JPanel
I need a simple C program which creates tcl interpreter, initializes tcl and tk
I want to create a PHP recursive program using a Binary Tree and Recursion.
Unix gurus! I have a Java program which passes some parameters to a Servlet
I'm trying to write a program that creates a 2D grid in which the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.