Binary tree getting stack overflow error

[b][red]This message was edited by chtexlion at 2004-7-31 9:42:4[/red][/b][hr]
I wrote a Binary tree program and it works..I need to read in a second file and at the same time search the tree for any words that match in the tree and then print the matched words..

Here is the function:
[code]
T* find(T stuff)
{
if(n->data==stuff)
cout<<stuff<<" ";
if(stuff<n->data && n->left != NULL)
return find(stuff);
else if(stuff>n->data&&n->right!=NULL)
return find(stuff);
return NULL;
}

//Here is thepart in main I am asking about:

infile.open("a:Ithillian.txt");
string s;
while(!infile.eof())

{
infile>>s;
tree.find(s)
}
[/code]

Comments

  • [blue]the second parameter is optional -- just don't use it.[/blue]
    [code]
    tree.find(s);
    [/code]


    : I wrote a Binary tree program and it works..I need to read in a second file and at the same time search the tree for any words that match in the tree and then print the matched words..
    :
    : Here is the function:
    : [code]
    : T* find(T stuff, node*n=root)
    : {
    : if(n->data==stuff)
    : return n;
    : if(stuffdata && n->left != NULL)
    : return find(stuff,n->left);
    : else if(stuff>n->data&&n->right!=NULL)
    : return find(stuff,n->right);
    : return NULL;
    : }
    :
    : //Here is thepart in main I am asking about:
    :
    : infile.open("a:Ithillian.txt");
    : string s;
    : while(!infile.eof())
    :
    : {
    : infile>>s;
    : tree.find(s,[red]???[/red]);//[green]What do I need to send
    : } //as a parameter since it is a
    : //node* in the function?
    :

  • Ok, think I got that problem solved but now after my program prints the first word of the second file I am getting a stack overflow exception:[green](An unhandled exception of type 'System.StackOverflowException' occurred in Unknown Module.)[/green]
    ?? What should I do about that?


    I wrote a Binary tree program and it works..I need to read in a second file and at the same time search the tree for any words that match in the tree and then print the matched words..
    :
    : Here is the function:
    : [code]
    : T* find(T stuff, node*n=root)
    : {
    : if(n->data==stuff)
    : return n;
    : if(stuffdata && n->left != NULL)
    : return find(stuff,n->left);
    : else if(stuff>n->data&&n->right!=NULL)
    : return find(stuff,n->right);
    : return NULL;
    : }
    :
    : //Here is thepart in main I am asking about:
    :
    : infile.open("a:Ithillian.txt");
    : string s;
    : while(!infile.eof())
    :
    : {
    : infile>>s;
    : tree.find(s,[red]???[/red]);//[green]What do I need to send
    : } //as a parameter since it is a
    : //node* in the function?
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories