簡體   English   中英

二進制搜索樹(搜索函數返回NULL)

[英]Binary Search Tree(search function returning 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";
}

上面是頭文件和構造函數和函數,用於從文件構建樹並搜索其中的節點。我不明白我的函數中缺少什么,因為每當我運行搜索函數時,它總是返回NULL,這是默認的當節點密鑰存在於文件/樹中時的條件。 非常感謝大家,它現在正在工作。我真的很感激。我使用插入功能來構建我的樹。

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. 在所述函數中:如果尚未存儲任何節點,您在哪里存儲節點? (提示:這是你班上的一個重要成員)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM