| | |
Traversing through a Binary Tree
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2007
Posts: 2
Reputation:
Solved Threads: 0
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.
Output is as follows:
C++ Syntax (Toggle Plain Text)
string encoding; void FileData::getEncoding(Node *root,string encoding){ if(root!=NULL){ cout << "root!=NULL" << endl; if(root->isLeaf){ cout << "This is a leaf" << endl; cout << "Encoding for character '"<< root->value << "' is '" << encoding << "'" << endl; } }else{ cout << "return reached" << endl; return; } string encode0; encode0=encoding+"0"; cout << "getEncoding for left" << endl; getEncoding(root->left,encode0); string encode1; encode1=encoding+"1"; cout << "getEncoding for right" << endl; getEncoding(root->right,encode1); }
C++ Syntax (Toggle Plain Text)
root!=NULL getEncoding for left root!=NULL getEncoding for left root!=NULL This is a leaf Encoding for character 'l' is '00' getEncoding for left root!=NULL getEncoding for left return reached getEncoding for right root!=NULL Segmentation fault
•
•
Join Date: Oct 2008
Posts: 40
Reputation:
Solved Threads: 6
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.
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.
![]() |
Similar Threads
- Help w/ Traversing a Huffman Tree (C++)
- What means O(log n) time ? (C++)
- Recursive Binary Search Tree Header File (C++)
- Traversing a Huffman Tree (C++)
- recursive algorithm (C++)
- Binary Search Tree (C++)
Other Threads in the C++ Forum
- Previous Thread: Linked List, Classes, and Pointers HELP
- Next Thread: Check if given one string part of other using wildcards
Views: 446 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





