C++ 二叉搜索树(搜索函数) 模板 树状结构{ 字符串值; T键; TreeNode*父代; TreeNode*LeftChild; TreeNode*右儿童; 树节点(Tk,字符串值) { 该->值=Val; 该->键=k; 此->父项=空; 此->LeftChild=NULL; 此->RightChild=NULL; } }; 模板 类二叉树{ 私人: 树根; 公众: 二叉树(); ~BinaryTree(); void insertNode(T键,字符串Val); 无效删除节点(T键); 字符串搜索节点(T键); void UpdateKey(T newkey,T oldkey); 内部高度(TreeNode*节点); int高度(); }; 模板 字符串二进制树::searchNode(T键) { 树节点*温度=根节点; while(temp!=NULL) { 如果(临时->键==键) { 库特基) { temp=temp->LeftChild; } 否则如果(temp->keyRightChild; } } 返回“\0”; }

C++ 二叉搜索树(搜索函数) 模板 树状结构{ 字符串值; T键; TreeNode*父代; TreeNode*LeftChild; TreeNode*右儿童; 树节点(Tk,字符串值) { 该->值=Val; 该->键=k; 此->父项=空; 此->LeftChild=NULL; 此->RightChild=NULL; } }; 模板 类二叉树{ 私人: 树根; 公众: 二叉树(); ~BinaryTree(); void insertNode(T键,字符串Val); 无效删除节点(T键); 字符串搜索节点(T键); void UpdateKey(T newkey,T oldkey); 内部高度(TreeNode*节点); int高度(); }; 模板 字符串二进制树::searchNode(T键) { 树节点*温度=根节点; while(temp!=NULL) { 如果(临时->键==键) { 库特基) { temp=temp->LeftChild; } 否则如果(temp->keyRightChild; } } 返回“\0”; },c++,C++,我正在制作一个二元搜索树。但当我运行搜索函数时,它总是返回空值,即使该值存在于树中。要么我的构造函数不正确,要么我的搜索函数有问题。我似乎无法解决问题。 以下是构造函数: template <class T> struct TreeNode{ string value; T key; TreeNode<T> *Parent; TreeNode<T> *LeftChild; TreeNode<T> *RightChild; T

我正在制作一个二元搜索树。但当我运行搜索函数时,它总是返回空值,即使该值存在于树中。要么我的构造函数不正确,要么我的搜索函数有问题。我似乎无法解决问题。 以下是构造函数:

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();
       ~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>
string BinaryTree<T>::searchNode(T Key)
{        
TreeNode<T> *temp=Root;
while (temp!=NULL)
{
      if (temp->key==Key)
      {
          cout<<temp->key<<endl;                             
          return temp->value;
      }
      if (temp->key>Key)
      {
          temp=temp->LeftChild;
      }
      else if (temp->key<Key)
      {
           temp=temp->RightChild;
      }                  
}     
return "\0";
}
模板
BinaryTree::BinaryTree()
{
Root=NULL;
流鳍;
fin.open(“names.txt”);
字符串缓冲区;
T浅黄色;
而(!fin.eof())
{
getline(fin,buffer,“~”);
鳍>>浅黄色;
树节点*temp,*temp1;
温度=根;
temp1=temp;
while(temp!=NULL)
{
temp1=temp;
TreeNode*Right=temp->RightChild;
TreeNode*Left=temp->LeftChild;
如果(临时->按键>缓冲)
{
temp=temp->LeftChild;
}
否则如果(temp->keyRightChild;
}
其他的
{
temp=temp->LeftChild;
}
}
如果(温度!=根)
temp->Parent=temp1;
temp=新的树节点(buff,buffer);
}
fin.close();
}

首先,它不属于构造函数。它应该位于
readFile()
方法或某个
操作符>>()

现在转到read函数

不要检查

template <class T>
BinaryTree<T>::BinaryTree()
{
Root=NULL;                       
ifstream fin;
fin.open("names.txt");
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;
          }
          else
          {
              temp=temp->LeftChild;
          }
      }
      if (temp!=Root)
      temp->Parent=temp1;
      temp=new TreeNode<T>(buff,buffer);
}
fin.close();
}
while (!fin.eof())
检查一下

template <class T>
BinaryTree<T>::BinaryTree()
{
Root=NULL;                       
ifstream fin;
fin.open("names.txt");
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;
          }
          else
          {
              temp=temp->LeftChild;
          }
      }
      if (temp!=Root)
      temp->Parent=temp1;
      temp=new TreeNode<T>(buff,buffer);
}
fin.close();
}
while (!fin.eof())
相反


最后,您从不向树中添加任何内容,只向某些
temp
变量添加内容。这可能是搜索功能失败的原因。

使用树时索引的类型是什么?SearchNode()是searchfuntion@User14229754是的,这是我的错。我在二读时发现的。我已经删除了那个评论。