Thread: binary search tree

  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 11: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,981
    >>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,817
    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);
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  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
    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-28-2004 at 12:46 AM.
    "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, 11:07 AM
  2. BST (Binary search tree)
    By praethorian in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2005, 09:11 AM
  3. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 05:15 PM
  4. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 10:18 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM