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

  • Home
  • SEARCH
  • 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 974013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:22:03+00:00 2026-05-16T03:22:03+00:00

Im trying to implement my own Huffman Coding algorithm and the priority queue for

  • 0

Im trying to implement my own Huffman Coding algorithm and the priority queue for the C++ STL does not seem to be working correctly. I am taking characters from a string and inserting them into a priority queue by order of their frequency in the string. The code compiles and runs without error, the only thing is the tree seems to not be sorting correctly. Here is the code,

class Node {
 public:
  int freq;
  char data;
  Node(int &f, char &d) { freq=f; data=d; }
  bool operator<(const Node* &n) const { return n->freq < this->freq; }
};

void Init(priority_queue<Node*> &tree, string input) {
 map<char,int> probability;
 for(int i=0 ; i<input.size() ; i++) {
   probability[input[i]]++;
 }
 map<char,int>::iterator it = probability.begin();
 for(it ; it != probability.end() ; it++) {
   Node* blah = new Node(it->second, (char&) it->first);
   tree.push(blah);
 }

}

what am I doing wrong?

Thanks

  • 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-05-16T03:22:04+00:00Added an answer on May 16, 2026 at 3:22 am

    You are storing pointers in the priority_queue, so the elements are sorted by pointer value, not using your operator< overload.

    You either need to store Node objects in the priority queue, or you need to write a custom comparison function for the priority queue that dereferences the stored pointers and compares the Node objects they point to.

    Since you ask “what am I doing wrong?,” here are some other suggestions:

    • Your operator< overload should take a const reference, not a reference to a pointer.
    • Your Node constructor should take its parameters by value, or at the very least by const reference. The cast (char&)it->first is not good. Let const help you write good code, don’t fight against it.
    • You probably should store Node objects directly in the priority queue, not pointers.
    • You are using using namespace std somewhere; you should remove this and spell out std:: wherever you need to.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement my own Thinning Algorithm in Matlab to understand the
I am currently trying to implement my own webserver in C++ - not for
I'm trying to implement my own List system in Java. the List class file
I'm trying to implement my own GenericIdentity implementation but keep receiving the following error
I am trying to implement my own authentication method for AuthKit and am trying
I'm trying to implement my own version of the 'cd' command that presents the
For all those ligthning fast shop users. I'm trying to implement my own first
I am trying to implement my own memory allocation code which is simple yet
I am trying to implement my own is_member_function_pointer and I'm having trouble with it.
I'm trying to implement my own IRC client as a personal proejct and I

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.