template <class T>
void BinaryTree<T>::LoadTree(const char *file)
{
ifstream fin;
fin.open(file);
string buffer;
T buff;
while (!fin.eof())
{
getline(fin,buffer,'~');
fin>>buff;
TreeNode<T> *temp,*temp1;
temp=Root;
temp1=temp;
while (temp!=NULL)
{
temp1=temp;
TreeNode<T> *Right=temp->RightChild;
TreeNode<T> *Left=temp->LeftChild;
if (temp->key>buff)
{
temp=temp->LeftChild;
}
else if (temp->key<buff)
{
temp=temp->RightChild;
}
}
temp=new TreeNode<T>(buff,buffer);
if (temp!=Root)
temp->Parent=temp1;
}
fin.close();
}
I am making a Binary search tree.This is my piece of code where I take input for a file which contains a name and a key like this "Alex~ 231423".Is my code making a right BST because when I run it,there is a msg which says "This application has requested the Runtime to terminate it in an unusual way.Please contact the application's support team for more information".I really don't understand what's wrong.I would appreciate any help ***the previous problem is solved,but the msg is appearing for insertNode function which is as follows:
template <class T>
void BinaryTree<T>::insertNode(T Key,string Val)
{
TreeNode<T> *temp,*temp1;
temp=Root;
while (temp!=NULL)
{
temp1=temp;
if (temp->key>Key)
{
temp=temp->LeftChild;
}
else if (temp->key<Key)
{
temp=temp->RightChild;
}
}
temp=new TreeNode<T>(Key,Val);
temp->Parent=temp1;
}
Here is the TreeNode part
template <class T>
struct TreeNode{
string value;
T key;
TreeNode<T> *Parent;
TreeNode<T> *LeftChild;
TreeNode<T> *RightChild;
TreeNode (T k,string Val)
{
this->value=Val;
this->key=k;
this->Parent=NULL;
this->LeftChild=NULL;
this->RightChild=NULL;
}
};
Actually I was getting the error for search function,not insert function.I am sorry for inconvenience.here is the code
template <class T>
string BinaryTree<T>::searchNode(T Key)
{
TreeNode<T> *temp=Root;
while (temp!=NULL)
{
if (temp->key==Key)
{
return temp->value;
}
if (temp->key>Key)
{
temp=temp->LeftChild;
}
else if (temp->key<Key)
{
temp=temp->RightChild;
}
}
return NULL;
}