binary search tree

This is a discussion on binary search tree within the C++ Programming forums, part of the General Programming Boards category; if anyone has time to help me....i am missing something that i cannot see. the value of x is supposed ...

  1. #1
    OCD script writer syrel's Avatar
    Join Date
    Sep 2004
    Posts
    12

    binary search tree

    if anyone has time to help me....i am missing something that i cannot see. the value of x is supposed to be transferring to p->info but it does not. i cant figure out why. because of this, the expression cannot be evaluated and the recursive function cycles infinitely forcing the program to crash. i have tried everything i can think of over the past 5 days and i am out of ideas.

    i followed the instructor's demonstration as best i could, but apparently i missed something. if anyone can see what i am doing wrong here, i would greatly appreciate the help.


    Code:
    #include <iostream>
    #include <cstring>  // the node.info was previously a string variable, changed to INT till i figure this out
    
    using namespace std;
    
    struct node
    {
    	int info;
    	node *left;
    	node *right;
    };
    
    	node *root;
    	root=new(node);
    	root=NULL;
    
    void insert(node *&p, int x);  // prototype
    
    
    void main()
    {
    	int month;
    	for(int i=1; i<=7; i++)
    	{
    		cout<<"Enter a value: "; 
    		cin>>month;
    		insert(node *&root, month);
    	}
    }  //end MAIN
    
    
    void insert(node *&p, int x)
    {
    	if (p==NULL)
    	{
    		p=new(node);
    		p->info=x;  
    		p->left=NULL;  // debugger always halts here
    		p->right=NULL;
    	};
    	else if(x < p->info)
    		insert(p->left, x);
    	else insert(p->right, x);
    }
    Last edited by syrel; 11-27-2004 at 10:32 PM.
    (3.0 Units of C++ Data Structures) - (Lab Time) = Death

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,484
    >>function cycles infinitely forcing the program to crash
    What program?

    gg

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,681
    Just a few comments in red below after a quick glance:

    Code:
    #include <iostream>
    #include <cstring> 
    using namespace std;
    
    struct node
    {
        int info;
        node *left;
        node *right;
    };
    
    node *root;      // Should be in main
    root=new(node);  // Get rid of this
    root=NULL;       // This belongs in main
    
    void insert(node *&p, int x);  // Probably not how you want the parameter declared
    
    void main()  // main always return an int
    {
        int month;
        for(int i=1; i<=7; i++)
        {
            cout<<"Enter a value: "; 
            cin>>month;
            insert(node *&root, month);  // Wrong way to call the function
        }
    }  //end MAIN
    
    
    void insert(node *&p, int x)  // See prototype comments above
    {
        if (p==NULL)
        {
            p=new(node);
            p->info=x;  
            p->left=NULL;  // debugger always halts here
            p->right=NULL;
        }; // This semicolon should not be here
        else if(x < p->info)
            insert(p->left, x);
        else insert(p->right, x);
    }
    I used to be an adventurer like you... then I took an arrow to the knee.

  4. #4
    OCD script writer syrel's Avatar
    Join Date
    Sep 2004
    Posts
    12
    thanx for the hlp, i tried what u suggested and it appears to have worked. but why is the int main important? i've been usig void main all the time. did it make a difference in this case, or is it just a better strategy?
    (3.0 Units of C++ Data Structures) - (Lab Time) = Death

  5. #5
    nbk
    nbk is offline
    meow nbk's Avatar
    Join Date
    Jul 2004
    Posts
    45
    int main is standard(there is more too it). Try doing a search, and you will see many threads on this.

  6. #6
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    int main is the way the standard is written, and it provides a testable value for successful program execution.
    check this site on void main
    http://users.aber.ac.uk/auj/voidmain.shtml
    google.com is your friend.
    Last edited by xviddivxoggmp3; 11-27-2004 at 11:46 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 10:07 AM
  2. BST (Binary search tree)
    By praethorian in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2005, 08:11 AM
  3. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 04:15 PM
  4. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 09:18 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21