0
 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;  
          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;
          }
      }
      temp=new TreeNode<T>(buff,buffer);          
      if (temp!=Root)
      temp->Parent=temp1;
}
fin.close();
}

I am making a Binary search tree.This is my piece of code where I take input for a file which contains a name and a key like this "Alex~ 231423".Is my code making a right BST because when I run it,there is a msg which says "This application has requested the Runtime to terminate it in an unusual way.Please contact the application's support team for more information".I really don't understand what's wrong.I would appreciate any help ***the previous problem is solved,but the msg is appearing for insertNode function which is as follows:

template <class T>
void BinaryTree<T>::insertNode(T Key,string Val)
{

 TreeNode<T> *temp,*temp1;
 temp=Root;
 while (temp!=NULL)
 {
       temp1=temp;
       if (temp->key>Key)
       {
           temp=temp->LeftChild;
       }
       else if (temp->key<Key)
       {
            temp=temp->RightChild;
       }
 }
 temp=new TreeNode<T>(Key,Val);              
 temp->Parent=temp1;
}

Here is the TreeNode part

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

Actually I was getting the error for search function,not insert function.I am sorry for inconvenience.here is the code

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;
}
User14229754
  • 85
  • 1
  • 12
  • Have you searched for similar topics? I guess I read about BST's the last three days here... – bash.d Feb 25 '13 at 13:32
  • 1
    You're doing something wrong with memory/pointers. Use a debugger. – Dariusz Feb 25 '13 at 13:35
  • 1
    Please see, this http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong about why it is considered bad to eof inside loop condition or this http://stackoverflow.com/questions/730910/ifstream-object-eof-not-working – Shafik Yaghmour Feb 25 '13 at 13:42
  • I don't suppose posting the constructor for `TreeNode` would be possible? (**please** post it as an edit to the question; **not** as a comment here). – WhozCraig Feb 25 '13 at 13:46
  • @WhozCraig it is simply Root=NULL – User14229754 Feb 25 '13 at 13:50
  • @User14229754 that is in the constructor for `BinaryTree`. I asked for the constructor for `TreeNode`. Not that it matters at this point. You just answered a different but more important question. Look at the above code, and tell yourself, then us, *"How does Root **ever** get set to a non-NULL value?"* – WhozCraig Feb 25 '13 at 13:54
  • `while (!fin.eof())` **bad bad bad** go to jail, do not pass go, do not collect £200 – Jonathan Wakely Feb 25 '13 at 13:58
  • 1
    @User14229754 and your new post has at least one indeterminate pointer assignment (on the first insert, where you set you new node's parent to the value of `temp1`, which was never set to NULL. Further, you never posted what was asked for, `TreeNodeT>::TreeNode()`. Also, this is a different question. This is not a debugging service. Voting to close. – WhozCraig Feb 25 '13 at 14:00
  • please uplift my ban as I have posted the piece of code that you wanted.I am new to programming and so i was bothering you more than usual but that wasn't my intention.I really get a lot of useful help from stackoverflow.Really really sorry for any offence created on my part – User14229754 Feb 25 '13 at 15:19

3 Answers3

1

Not sure if this is the problem, but you need a pointer to a pointer, otherwise you just modify the local variable, not the left / right child member.

 TreeNode<T> **temp = &Root,
             *temp1 = NULL; // otherwise Root will have an invalid parent pointer
 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;
Bernhard Barker
  • 50,899
  • 13
  • 85
  • 122
  • it still doesn't solve the problem.I am getting an error,"request for member 'key' in 'temp' which is of non-class type 'TreeNode*' – User14229754 Feb 25 '13 at 14:35
  • @User14229754 Did you copy it as is? Note that `temp1->key` is intentionally **not** `temp->key`, if you want to use `temp` it would be: `(*temp)->key`. – Bernhard Barker Feb 25 '13 at 14:45
  • yes i did but i just found that my problem was in search function and not insert.sorry – User14229754 Feb 25 '13 at 14:51
0

You need to make some more initializations (Root and a child):

template <class T>
void BinaryTree<T>::LoadTree(const char *file)
{
   ifstream fin(file);
   string buffer;
   T buff;
  while (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;     }
     } // allow duplicate key??

     temp=new TreeNode<T>(buff,buffer); 
     if (     !Root)   {  Root=temp;  continue;      }
     //else if (temp !=Root)     temp->Parent=temp1;

     // insert the new node in the correct branch, that was NULL
     if (temp1->key >= buff)
      {
          temp1->LeftChild=temp;
      }
     else temp1->RightChild=temp;
  }
fin.close();
}

Also, on failure, your searchNode have to return "" ?? But not NULL.

qPCR4vir
  • 3,423
  • 1
  • 18
  • 30
0

The way you are writing this means you can only create it from a file. Write operator>> to use an istream. It will make it easier to test, too:

template <class T>
class BinaryTree {
public:
    istream& operator>>(istream& is) {
        //...
        return is;
    }
};

int main() {
    stringstream ss("tree formatted data");

    BinaryTree<int> tree;
    ss >> tree;
    return 0;
}

Replace "tree formatted data" with whatever your data is.

Peter Wood
  • 21,348
  • 4
  • 53
  • 90