Thread: C++ Binary Search Tree

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    3

    C++ Binary Search Tree

    Hi all,
    I am working on a Binary Search Tree but cant seem to get it to run. Evrything I run the code i does some funny stuff that i dont understand. Please would someone help sort out the problem. thanks for any help
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.h>
    
    
    struct tree_node
    {
    	int data;
       int menu;
    	tree_node* left;
       tree_node* right;
    };
    
    
    tree_node * insert(tree_node *, int);
    //void tree();
    void in_order(tree_node *tree);
    void ass_order(tree_node *tree);
    void desc_order(tree_node *tree);
    tree_node* insert(tree_node * tree, int element)
    
    	{
       	if(tree==NULL)
       	{
          	tree = new tree_node;
             tree->left=tree->right=NULL;
             tree->data=element;
          }
          else if(element < tree->data)
          {
    		tree->left=insert(tree->left,element);
          }
          else
          tree->right=insert(tree->right,element);
    
          return(tree);
    	}
    
    		void in_order(tree_node *tree)
             {
          	if(tree!=NULL)
             {
             	cout<<tree->data;
                in_order(tree->left);
                in_order(tree->right);
                getch();
             }
             }
    
          void ass_order(tree_node *tree)
          {
          	if(tree!=NULL)
             {
             	ass_order(tree->left);
                cout<<tree->data;
                ass_order(tree->right);
                getch();
             }
          }
    
    
    void desc_order(tree_node *tree)
          {
          	if(tree!=NULL)
             {
             	desc_order(tree->right);
                cout<<tree->data;
                desc_order(tree->left);
                getch();
             }
          }
    
    menu()
    {
    	tree_node * tree;
    	tree=NULL;
       int choice, element;
       do{
    
    
          cout<< "========  Main Menu  ========\n";
    		cout<<"1.Enter Number for the tree.\n";
          cout<<"2.Numbers in inputed order.\n";
          cout<<"3.Numbers in ascending order.\n";
          cout<<"3.Numbers in descending order.\n";
          cout<<"4.Exit.\n";
          cout<<"\nPlease make a choice: ";
          cin>>choice;
    
           } while(choice!=1);
    
    
          	switch(choice)	//used to get the choice form the user.
             {
          	case 1:	//promts the user to put a number into the tree
    			cout<<"\nEnter a number to add to the tree:";
    			cin>>element;
    			tree = insert(tree,element);
    
    
          	case 2:	//prnits numbers in order inputed
    			cout<<"\nInput Order: ";
    			in_order(tree);
    
    
             case 3:	//prnits numbers in ascending order
    			cout<<"\nAscending Order: ";
    			ass_order(tree);
    
    
             case 4:	//prnits numbers in descending order
    			cout<<"\nDescending Order: ";
    			desc_order(tree);
    
           }
    }    return main;

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    100
    From a brief look I've noticed that the last line of your code places a return statement outside of a function implementation. Also the last function in your code does not have a return type specified.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    3
    ok so what do i have to do to fix that?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Okay, you clearly don't have a clue what you're doing. If you don't know how to declare main, then this is probably about your first program, and you probably don't know anything about pointers. So I would guess that you copied much of that code from someone else.
    You also appear to have never used a switch statement before since you left off every single break.
    Your indentation is all over the place with a mix of tabs and spaces, another sign you're an absolute beginner.
    Furthurmore since you aren't able to understand something basic that someone tell you with regards to fixing the code, that basically proves you've copied it.

    Please go off on your own and actually attempt to learn the language for a bit, then come back and post something closer to a compileable program, and tell us about the exact errors you are facing. We don't write it for you, and we aren't here to help you to just slip through with a passing grade without really learning anything at all.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    3
    Christ mate, feel like I should apologise for asking for the help. I can see when people find it so hard to learn coding with stuck up people like you riding around on there programming hight horse.
    Yes I did copy parts of my code from other websites as i am a very confident at C++ but I still need to do this.
    Cheers

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You misunderstand. You're not being bashed just for being a newbie asking for help.
    You'll find that we get a regular number of people coming here and dumping large amounts of code, saying that there is some problem with it and expecting a perfectly fixed up polished version of their code in the shortest amount of time possible before they check back on their thread later.
    For one it's incredibly rude and selfish of them, and two the person posting doesn't learn anything, and three it's cheating.
    You'll also find that there are many of us that react to this in the same way. Today you got me; next person will probably get the same spiel from someone else.

    Now, by all means you may not be one of those people, but if you don't want to seem like one of those people then you'll need to try to make a few adjustments to the way you post.
    1. Did you read the forum rules? You should show that you are making an effort to solve things yourself.
    2. Try your best to get code to compile. If you can't get it to compile, then always post the exact compiler error messages along with the code.
    3. Don't be vague if you can at all help it, e.g. "some funny stuff that i don't understand". We don't want to make a project and compile your code, run it several times to see what it does, and guess what it's doing that isn't what it should be doing. We'd all rather you tell us exactly what it's doing wrong so that we can look at the relevent lines of code and give you a quick answer. All we're asking is that you do your best to make it easier for others to help you. It's in your best interests if you want a quick resolution.
    4. Everybody that ever posts asking for help on any forums should always be familiar with the following article: http://www.catb.org/~esr/faqs/smart-questions.html Now it can be a bit blunt in places, and may even be a bit outdated, but nevertheless it really gets the point across. People from forums everywhere direct people to this article.

    Seriously though, one does have to wonder how it came to be that you're in the process of learning some of the much more advanced concepts of programming when you don't appear to even know how to write a "Hello world" program. One has got to assume you either didn't do any of your classwork for months, or you cheated your way through to this point. To that end I think my original response was perhaps appropriate. If you can show an alternative explanation then I'll take it all back.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

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