简体   繁体   中英

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";
}

Above are the header file and constructor and functions for building tree from a file and searching a node in it.I don't understand what am I missing in my function because whenever i run the search function,it always returns NULL which is the default condition when the node key exists in the file/tree. Thanks a lot everyone,it's working now.I am really grateful.I used my insert function to build up my tree.

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();
}

Your Root member never gets set. Thus its value is still NULL when you try to search the tree, and the while will never get entered.

Hints:

  1. Write a method that adds a node and use this in your LoadTree method instead.
  2. In said function: where do you store the node if there hasn't been any node stored yet? (Hint: it's a crucial member of your class).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM