C++ 二叉搜索树(搜索函数返回NULL) #包括 #包括 #包括 使用名称空间std; 模板 树状结构{ 字符串值; T键; TreeNode*父代; TreeNode*LeftChild; TreeNode*右儿童; 树节点(Tk,字符串值) { 该->值=Val; 该->键=k; 此->父项=空; 此->LeftChild=NULL; 此->RightChild=NULL; } }; 模板 类二叉树{ 私人: 树根; 公众: 二叉树(); void LoadTree(const char文件[]); ~BinaryTree(); void insertNode(T键,字符串Val); 无效删除节点(T键); 字符串搜索节点(T键); void UpdateKey(T newkey,T oldkey); 内部高度(TreeNode*节点); int高度(); }; 模板 BinaryTree::BinaryTree() { Root=NULL; } 模板 void BinaryTree::LoadTree(const char*文件) { 流鳍; 打开(文件); 字符串缓冲区; T浅黄色; 而(!fin.eof()) { getline(fin,buffer,“~”); 鳍>>浅黄色; 树节点*temp,*temp1; 温度=根; temp1=temp; while(temp!=NULL) { temp1=temp; 如果(临时->按键>缓冲) { temp=temp->LeftChild; } 否则如果(temp->keyRightChild; } } temp=新的树节点(buff,buffer); 如果(温度!=根) temp->Parent=temp1; 库尔夫特奇尔德; } 否则如果(temp->keyRightChild; } } 返回“\0”; }

C++ 二叉搜索树(搜索函数返回NULL) #包括 #包括 #包括 使用名称空间std; 模板 树状结构{ 字符串值; T键; TreeNode*父代; TreeNode*LeftChild; TreeNode*右儿童; 树节点(Tk,字符串值) { 该->值=Val; 该->键=k; 此->父项=空; 此->LeftChild=NULL; 此->RightChild=NULL; } }; 模板 类二叉树{ 私人: 树根; 公众: 二叉树(); void LoadTree(const char文件[]); ~BinaryTree(); void insertNode(T键,字符串Val); 无效删除节点(T键); 字符串搜索节点(T键); void UpdateKey(T newkey,T oldkey); 内部高度(TreeNode*节点); int高度(); }; 模板 BinaryTree::BinaryTree() { Root=NULL; } 模板 void BinaryTree::LoadTree(const char*文件) { 流鳍; 打开(文件); 字符串缓冲区; T浅黄色; 而(!fin.eof()) { getline(fin,buffer,“~”); 鳍>>浅黄色; 树节点*temp,*temp1; 温度=根; temp1=temp; while(temp!=NULL) { temp1=temp; 如果(临时->按键>缓冲) { temp=temp->LeftChild; } 否则如果(temp->keyRightChild; } } temp=新的树节点(buff,buffer); 如果(温度!=根) temp->Parent=temp1; 库尔夫特奇尔德; } 否则如果(temp->keyRightChild; } } 返回“\0”; },c++,C++,上面是头文件、构造函数和函数,用于从文件构建树并搜索其中的节点。我不明白我的函数缺少什么,因为每当我运行搜索函数时,它总是返回NULL,这是文件/树中存在节点键时的默认条件。 非常感谢大家,它现在正在工作。我真的很感激。我用我的插入函数建立了我的树 #include <iostream> #include <string> #include <fstream> using namespace std; template <class T> stru

上面是头文件、构造函数和函数,用于从文件构建树并搜索其中的节点。我不明白我的函数缺少什么,因为每当我运行搜索函数时,它总是返回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 "\0";
}
模板
void BinaryTree::insertNode(T键,字符串Val)
{
树节点**温度=&根节点;
TreeNode*temp1=NULL;
如果(*temp==NULL)
{
根=新树节点(键,Val);
返回;
}
否则{
while(*temp!=NULL)
{
temp1=*temp;
如果(temp1->key>key)
{
temp=&(*temp)->LeftChild;
}
else if(temp1->keyRightChild;
}
}
}
*temp=新树节点(键,Val);
(*temp)->Parent=temp1;
}
模板
void BinaryTree::LoadTree(const char*文件)
{
流鳍;
打开(文件);
字符串缓冲区;
T浅黄色;
而(!fin.eof())
{
getline(fin,buffer,“~”);
鳍>>浅黄色;
insertNode(buff,buffer);
}          
fin.close();
}

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

提示:
  • 编写一个添加节点的方法,并在LoadTree方法中使用该方法
  • 在所述函数中:如果尚未存储任何节点,则将节点存储在何处?(提示:它是类的关键成员)

  • 您在
    之前尝试过
    断言(Root!=NULL)
    吗?我相信您没有正确设置Root。断言函数做什么?@user1777067
    断言(Root!=NULL)
    表示您告诉程序变量
    Root
    不能为
    NULL
    ,如果是,您的程序将抛出错误。请参阅。请注意
    assert
    不应包含副作用,例如
    assert(x++)
    。它是分散的printf语句和
    gdb
    用法的替代方法,不过您应该学习如何使用调试器。@user1777067:不客气。代码中还有一些地方稍有偏差(例如,您可以简单地使用
    return std::string();
    return“”
    ,而不是
    “\0”
    as not found return value)。此外,如果可能,您应该在
    类中移动
    TreeNode
    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();
    }