I have a binary tree, that I am searching:
TreeNode<Vessel*>* node = this->tree_->search("PotatoFace");
string mystring = node->print();
when I run it, node contains the correct data, but when I go to print that data as soon as I enter:
string TreeNode<T>::print()
{
return data_->toString();
}
‘this’ (which should be the ‘node’ and has the same memory address as ‘node’) has all of its data members including the Vessel* set to null.
Any ideas?
Thank you!
Full Tree Node:
#pragma once
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;
template <class T>
class TreeNode
{
private:
TreeNode<T>* greaterNode_;
TreeNode<T>* lessNode_;
TreeNode<T>* parentNode_;
TreeNode<T>* getLowest_();
T data_;
public:
TreeNode();
TreeNode(T data);
void add(T data);
bool operator==(const string &rhs);
TreeNode* search(T data);
void seqSearch(string data, TreeNode<T>* node);
void del(TreeNode<T>* root);
void toFile(ofstream& BSTFile);
TreeNode* compare(int sig[4]);
TreeNode* getRoot();
TreeNode* forward(TreeNode<T>* node);
string print();
};
template <class T>
TreeNode<T>::TreeNode(T data)
{
data_ = data;
greaterNode_ = lessNode_ = parentNode_= NULL;
}
template <class T>
TreeNode<T>::TreeNode()
{
}
template <class T>
void TreeNode<T>::seqSearch(string data, TreeNode<T>* node )
{
if(*data_ == data)
{
*node = this->data_;
}
if(this->lessNode_)
{
this->lessNode_->seqSearch(data, node);
}
if(this->greaterNode_)
{
this->greaterNode_->seqSearch(data, node);
}
}
template <class T>
string TreeNode<T>::print()
{
return data_->toString();
}
Still not entirely sure how to explain why it wasn’t working, but it was a scope issue, outside the binary tree class tree nodes lost data. Taken out all tree functions that returned nodes and everything works now.
Still not entirely sure how to explain why it wasn’t working, but it was a scope issue, outside the binary tree class tree nodes lost data.
It was rectified by ensuring that the Binary Tree class doesn’t return anything of type TreeNode*, and running whatever other function I wanted once I had the value of the node was done inside the binary tree class. This works now.
Thank you for the help!