二叉搜索树(搜索函数返回NULL)


#include <iostream>
#include <string>
#include <fstream>
using namespace std;
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;
  }
};
template <class T>
class BinaryTree{
  private:
       TreeNode<T> *Root;        
  public: 
       BinaryTree();
       void LoadTree(const char file[]);
       ~BinaryTree();
       void insertNode(T Key,string Val);
       void deleteNode(T Key);
       string searchNode(T Key);
       void UpdateKey(T newkey,T oldkey);
       int Height(TreeNode<T> *node);
       int height();
};
template <class T>
BinaryTree<T>::BinaryTree()
{
    Root=NULL;                       
}
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;  
          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;
      cout<<temp->value<<temp->key<<endl;
      if (temp->LeftChild!=0)
      cout<<(temp->LeftChild)->value<<endl;
}
fin.close();
}
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,这是节点键存在于文件/树中的默认条件。非常感谢大家,现在开始工作了。我真的很感激。我使用insert函数来构建我的树。

template <class T>
void BinaryTree<T>::insertNode(T Key,string Val)
{
 TreeNode<T> **temp=&Root;
 TreeNode<T> *temp1=NULL;
 if (*temp==NULL)
 {
     Root=new TreeNode<T>(Key,Val);
     return;
 }
 else{            
 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;
}
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;
      insertNode(buff,buffer);
}          
fin.close();
}

您的Root成员永远不会被设置。因此,当您尝试搜索树时,它的值仍然是NULL,并且while将永远不会被输入。

提示:

  1. 编写一个方法来添加一个节点,并在你的LoadTree方法中使用它。
  2. 在上述函数中:如果还没有存储任何节点,您将节点存储在哪里?(提示:它是你的类的关键成员)。

最新更新