943,917 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 690
  • C++ RSS
Apr 6th, 2009
0

Traversing through a Binary Tree

Expand Post »
I have the following code below which is part of a program to create a Huffman Coding tree. The problem is that it runs through correctly for one node, outputting that character 'l' for a certain file has a specific frequency. However...after going through several iterations, it runs into a segmentation fault on the line "if(root->isLeaf)" I see the output of "root!=NULL", but then it displays Segmentation Fault.
C++ Syntax (Toggle Plain Text)
  1. string encoding;
  2. void FileData::getEncoding(Node *root,string encoding){
  3.  
  4. if(root!=NULL){
  5. cout << "root!=NULL" << endl;
  6. if(root->isLeaf){
  7. cout << "This is a leaf" << endl;
  8. cout << "Encoding for character '"<< root->value << "' is '" << encoding << "'" << endl;
  9. }
  10.  
  11. }else{
  12. cout << "return reached" << endl;
  13. return;
  14. }
  15.  
  16. string encode0;
  17. encode0=encoding+"0";
  18. cout << "getEncoding for left" << endl;
  19. getEncoding(root->left,encode0);
  20.  
  21. string encode1;
  22. encode1=encoding+"1";
  23. cout << "getEncoding for right" << endl;
  24. getEncoding(root->right,encode1);
  25.  
  26.  
  27. }
Output is as follows:
C++ Syntax (Toggle Plain Text)
  1. root!=NULL
  2. getEncoding for left
  3. root!=NULL
  4. getEncoding for left
  5. root!=NULL
  6. This is a leaf
  7. Encoding for character 'l' is '00'
  8. getEncoding for left
  9. root!=NULL
  10. getEncoding for left
  11. return reached
  12. getEncoding for right
  13. root!=NULL
  14. Segmentation fault
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tiki_master is offline Offline
2 posts
since Nov 2007
Apr 6th, 2009
0

Re: Traversing through a Binary Tree

First, using the same name for an argument and a global is confusing (string encoding)...if you are using it as a constant, make sure you initialize it (not all compilers will do this for you, especially in release), and remove the argument from the method declaration and definition.
I don't have everything to debug it, but, this is probably caused by a pointer to memory that is not valid. Pointers may be initialized when they are defined (again, depending on the compiler and optimizations it uses), and are never automatically "reset" to NULL when they are not valid. So the test (root!=NULL) will pass on any pointer, valid or not, that or not initialized to NULL.
Reputation Points: 13
Solved Threads: 6
Light Poster
seanhunt is offline Offline
40 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Linked List, Classes, and Pointers HELP
Next Thread in C++ Forum Timeline: Check if given one string part of other using wildcards





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC