2013-02-25 259 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"與任何您的數據。