2013-02-25 248 views
0
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; 
} 
+0

您是否搜索过类似主题?我想我在这里读了关于BST的最近三天... – 2013-02-25 13:32:48

+1

你在做内存/指针有问题。使用调试器。 – Dariusz 2013-02-25 13:35:21

+1

请看,这个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

回答

0

IMO,你忘插平等条件:

if (temp->key > key) 
    { 
     temp=temp->LeftChild; 
    } 
    else if (temp->key < key) 
    { 
     temp=temp->RightChild; 
    } 
    else // if (temp->key == key) 
    { 
    // Do something (e.g break loop) 
    } 
0

你需要做一些更多的初始化(根和一个孩子):

template <class T> 
void BinaryTree<T>::LoadTree(const char *file) 
{ 
    ifstream fin(file); 
    string buffer; 
    T buff; 
    while (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;  } 
    } // allow duplicate key?? 

    temp=new TreeNode<T>(buff,buffer); 
    if ( !Root) { Root=temp; continue;  } 
    //else if (temp !=Root)  temp->Parent=temp1; 

    // insert the new node in the correct branch, that was NULL 
    if (temp1->key >= buff) 
     { 
      temp1->LeftChild=temp; 
     } 
    else temp1->RightChild=temp; 
    } 
fin.close(); 
} 

另外,如果失败,你的searchNode必须返回“”?但不是NULL。

+0

@ User14229754创建的任何攻击。请测试这个。 – qPCR4vir 2013-02-26 00:01:12

1

不知道这是问题,但你需要一个指针的指针,否则你只是修改本地变量,而不是左/右子成员。

TreeNode<T> **temp = &Root, 
      *temp1 = NULL; // otherwise Root will have an invalid parent pointer 
while (*temp != NULL) 
{ 
     temp1 = *temp; 
     if (temp1->key > Key) 
     { 
      temp = &(*temp)->LeftChild; 
     } 
     else if (temp1->key < Key) 
     { 
      temp = &(*temp)->RightChild; 
     } 
} 
*temp = new TreeNode<T>(Key,Val);    
(*temp)->Parent = temp1; 
+0

它仍然不能解决问题。我收到一个错误,“请求成员'密钥'在'非'类'的Tree类 *' – User14229754 2013-02-25 14:35:30

+0

@ User14229754'temp'你是否照原样复制?那'temp1-> key'是故意不是**'temp-> key',如果你想使用'temp',它将是:'(* temp) - > key'。 – Dukeling 2013-02-25 14:45:17

+0

是的,但我只是发现我的问题是在搜索功能,而不是insert.sorry – User14229754 2013-02-25 14:51:14

0

你写这个的方式意味着你只能从一个文件创建它。写operator>>使用istream。这将使它更容易测试,也:

template <class T> 
class BinaryTree { 
public: 
    istream& operator>>(istream& is) { 
     //... 
     return is; 
    } 
}; 

int main() { 
    stringstream ss("tree formatted data"); 

    BinaryTree<int> tree; 
    ss >> tree; 
    return 0; 
} 

更换"tree formatted data"与任何您的数据。