BST and filing
The code gets input from text file and inserts in BST but while traversing
it gives infinte loop of the first value
assume values in data.txt as
1
2
3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
struct Tree
{
int data;
Tree *Left;
Tree *Right;
};
Tree *root =NULL;
void insert(Tree * temp , Tree *prev , int d)
{
cout<<"temp!=NULL\n";
if(temp == NULL)
{
temp = new Tree;
temp->data=d;
temp->Left=NULL;
temp->Right=NULL;
if(root==NULL){
root=temp;}
else
{
if(d>=prev->data){ prev->Right=temp;}
else{prev->Left=temp;}
}
}
else if(d>=temp->data)
{insert(temp->Right,temp,d); }
else
{insert(temp->Left,temp,d);}
}
void Traverse(Tree *temp)
{
while (temp!=NULL)
{
Traverse(temp->Left);
cout<<temp->data<<endl;
Traverse(temp->Right);
}
}
int main()
{
int id;
ifstream fin;
fin.open("Data.txt",ios::in);
do
{
fin>>id;
insert(root,root,id);
}
while(!fin.eof());
Traverse(root);
getch();
}
|
Line 40 - a while loop in your Traverse function?
Thanks it took me an hour was looking error in my logic in insert function :(
Topic archived. No new replies allowed.