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();
}
我想提出一个二进制搜索tree.This是我的一段代码,我需要输入其中包含一个名字和这样的“亚历克斯〜231423”密钥的文件。是我的代码制作正确的BST,因为当我运行它时,有一个味精说:“这个应用程序已经请求运行时以不寻常的方式终止它。请联系应用程序的支持团队获取更多信息”。我真的不明白什么是错的。我将不胜感激。以前的问题就解决了任何帮助 **,但味精是出现了insertNode函数如下:二叉搜索树
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;
}
这里是树节点部分
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;
}
您是否搜索过类似主题?我想我在这里读了关于BST的最近三天... – 2013-02-25 13:32:48
你在做内存/指针有问题。使用调试器。 – Dariusz 2013-02-25 13:35:21
请看,这个http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong为什么它被认为是不好的eof里面循环条件或这个http:///stackoverflow.com/questions/730910/ifstream-object-eof-not-working – 2013-02-25 13:42:57