I'm working on a binary tree program and when calling my insert function it appears to insert the value being entered, and when checking for that value I get nothing and am stumped right now what could I be missing or what could be wrong? I've provided the class data, and the insert/search function definitions below, I just have odd feeling this has to do with my implementation of templates or just templates in general help?
struct node
{
string keyValue; //value to stored that is being searched for in our tree
node *left; //left side of pointing tree from parent node
node *right; //right side of pointing tree from parent node
};
template <class T>
class Tree
{
public:
Tree(); //constructor
~Tree(); //destructor
void displayTree();
void insertTree(T key);
node *searchTree(T key);
void deleteTree();
private:
node *root; //points to the root point of our tree
void displayTree(node *leaf);
void insertTree(T key, node *leaf); //takes in our keyValue to
void deleteTree(node*leaf);
node *search(T key, node *leaf);
};
template <class T>
void Tree<T>::insertTree(T key)
{
cout << "instert1" << endl;
if(root != NULL)
{
cout << "instert2" << endl;
insertTree(key, root);
}
else
{
cout << "inster else..." << endl;
root = new node;
root->keyValue = key;
cout << root->keyValue << "--root key value" << endl;
root->left = NULL;
root->right = NULL;
}
}
template <class T>
void Tree<T>::insertTree(T key, node *leaf)
{
if(key < leaf->keyValue)
{
if(leaf->left != NULL)
{
insertTree(key, leaf->left);
}
else //descend tree to find appropriate NULL node to store keyValue (left side of tree)
{
leaf->left = new node; //Creating new node to store our keyValue (data)
leaf->left -> keyValue = key;
leaf->left -> left = NULL; //Assigning left and right child of current child node to NULL
leaf->left -> right = NULL;
}
}
else if(key >= leaf->keyValue)
{
if(leaf->right != NULL)
{
insertTree(key, leaf->right);
}
else //descend tree to find appropriate NULL node to store keyValue (right side of tree)
{
leaf->right = new node; //Creating new node to store our keyValue (data)
leaf->right -> keyValue = key;
leaf->right -> right = NULL; //Assigning left and right child of current child node to NULL
leaf->right -> left = NULL;
}
}
}
template <class T>
node *Tree<T>::searchTree(T key)
{
cout << "searching for...key: " << key << " and given root value:" << endl;
return search(key, root);
}
template <class T>
node *Tree<T>::search(T key, node*leaf)
{
if(leaf != NULL)
{
cout << "check passed for search!" << endl;
if(key == leaf->keyValue)
{
return leaf;
}
if(key < leaf->keyValue)
{
return search(key, leaf->left);
}
else
{
return search(key, leaf->right);
}
}
else
{
cout << key << " Not found...!" << endl;
return NULL;
}
}
Your code seems to work. You can look here: http://ideone.com/w9Aa1w
What you may need to do, which is what I did, is templatize your node struct, like so...
template <typename T>
struct NodeStruct
{
T keyValue; //value to stored that is being searched for in our tree
NodeStruct<T> *left; //left side of pointing tree from parent node
NodeStruct<T> *right; //right side of pointing tree from parent node
};
Then in your tree class typedef your new node type as just 'node':
typedef NodeStruct<T> node;
It seemed to insert properly though.
In the future it would help if you are more clear with the exact problem you are having, and instead of just posting a whole program, determine at least a subsection of code you are having a problem with.
This question is too vague and probably won't be helpful to anybody else as reference material.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.