Hello,
I'm doing a full traversal of a binary search tree structure looking for a node by a field that is not the sort key. If the node i'm looking for is the root node, no problem. If it's any other node then the function has a Seg Fault. Anyone know what is going on?
I'm pretty sure the problem is in my logic in what i'm returning from Find_Trav_Subtree, but cannot pinpoint it. gdb debugger gives me this, but i'm not sure if it helps.
Breakpoint 1, BSTClass::Print_Node (this=0x80537a4, NodePtr=0xb7f1e888) at bstree.cpp:227
227 {
(gdb) next
228 cout << NodePtr->Info.fields.size() << endl;
(gdb)
402
229 cout << setw(15) << NodePtr->Info.fields.at(0);
(gdb)
Program received signal SIGSEGV, Segmentation fault.
0x00cfa1e6 in std::operator<< <char, std::char_traits<char>, std::allocator<char> > () from /usr/lib/libstdc++.so.6
Note - I can print using the same syntax (NodePtr->Info.fields.at(0)) from another function that just prints the entire tree and doesn't take a NodePtr as an argument.
// screen output
checking current info
checking current info
checking current info
about to return current
checking current info
checking current info
checking current info
Result returned successfully
about to print node
Segmentation fault
// Initial function calls
void Delete_by_ID( int id_choice )
{
BSTNodePtr Result;
FoundResult = NULL;
//Find by ID
//Result = reinterpret_cast <AVLNodePtr> (AVLTreeContacts.Find_Trav(id_choice));
Result = AVLTreeContacts.Find_Trav(id_choice);
cout << "Result returned successfully" << endl;
if (Result == NULL)
cout << "Result is Null" << endl;
else {
cout << "about to print node" << endl;
AVLTreeContacts.Print_Node(Result);
}
//if user agrees, FreeNode
}
void BSTClass::Print_Node(BSTNodePtr NodePtr)
{
cout << setw(15) << NodePtr->Info.fields.at(0);
cout << setw(15) << NodePtr->Info.fields.at(1) << endl;
}
BSTNodePtr BSTClass::Find_Trav(int ID)
{
return Find_Trav_Subtree(Root, ID);
}
BSTNodePtr BSTClass::Find_Trav_Subtree(BSTNodePtr Current, int ID)
{
if (Current != NULL) {
cout << "checking current info" << endl;
if (Current->Info.contact_id == ID){
cout << "about to return current";
return Current;
}
if (Current->Right) {
Find_Trav_Subtree(Current->Right, ID);
}
if (Current->Left) {
Find_Trav_Subtree(Current->Left, ID);
}
}
if (Current == NULL)
return NULL;
}