Traversing through a Binary Tree

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 2
Reputation: tiki_master is an unknown quantity at this point 
Solved Threads: 0
tiki_master tiki_master is offline Offline
Newbie Poster

Traversing through a Binary Tree

 
0
  #1
Apr 6th, 2009
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.
  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:
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 40
Reputation: seanhunt is an unknown quantity at this point 
Solved Threads: 6
seanhunt seanhunt is offline Offline
Light Poster

Re: Traversing through a Binary Tree

 
0
  #2
Apr 6th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC