Closed Thread
Results 1 to 5 of 5

Thread: C++ - Problem with a binary tree program; need help

  1. #1
    Ratchet2246 is offline Newbie
    Join Date
    Jul 2008
    Posts
    12
    Rep Power
    0

    C++ - Problem with a binary tree program; need help

    The Code
    Code:
    #include<iostream>
    using namespace std;
    
    //Defines the 'node' structure
    struct node
    {
        int key_value;
        node *left;
        node *right;
    }
    
    //Defines the 'btree' class
    class btree
    {
    public:
        //The Constructor
        btree();
        //The Destructor
        ~btree();
        //The public "insert" function
        void insert(int key);
        //The public "*search" node function
        node *search(int key);
        //The public "destroy_tree" function
        void destroy_tree();
    private:
        //The private counter-part to the insert function
        void insert(int key,node *leaf);
        //The private counter-part to the "*search" node function
        node *search(int key,node *leaf);
        //The private counter-part to the "destroy_tree" function
        void destroy_tree(node *leaf);
        //The first parent node of the tree
        node *root;
    };
    
    //Defines the constructor function of btree
    btree::btree()
    {
       //Sets the 'root' pointer node to a null pointer 
        root=NULL;
    }
    //Defines the destructor function of btree
    btree::~btree()
    {
        //Starts the public "destroy_tree" function
        destroy_tree();
    }
    //Defines the "insert" public function of btree
    void btree::insert(int key)
    {
        //Checks to see if root is pointing to anything
        if(root!=NULL)
        {
            //Starts the private "insert" function
            insert(key,root);
        }
        else
        {
            //If root isn't pointing to anything creates something for it to point to
            root=new node;
            root->key_value=key;
            root->left=NULL;
            root->right=NULL;
        }
    }
    //Defines the "*start" public function of btree
    node *btree::search(int key)
    {
        //Starts the private "search" function
        return search(key, root);
    }
    //Defines the public "destroy_tree()" function
    void btree::destroy_tree()
    {
        //Starts the private counter-part of the function
        destroy_tree(root);
    }
    //Defines the private function, "insert"
    void btree::insert(int key,node *leaf)
    {
        //If the value entered is less than the parent value
        if(key<leaf->key_value)
        {
            //Check to see if there is a value already (left)
            if(leaf->left!=NULL)
            {
                //If so repeats the process with the next number down the line (left side)
                insert(key,leaf->left);
            }
            else
            {
                //If not creates a new node (on the left side) with the value stated  
                leaf->left=new node;
                leaf->left->key_value=key;
                leaf->left->left=NULL;
                leaf->left->right=NULL;
            }
        }
        //If the value entered is more than or equal to the parent value
        else if(key>=leaf->key_value)
        {
            //Check to see if there is a value already (right)
            if(leaf->right!=NULL)
            {
                //If so repeats the process with the next number down the line (right side)
                insert(key,leaf->right);
            }
            else
            {
                //If not creates a new node (on the right side) with the value stated
                leaf->right=new node;
                leaf->right->key_value=key;
                leaf->right->left=NULL;
                leaf->right->right=NULL;
            }
        }
    }
    //Defines the private "search" function
    node *btree::search(int key,node *leaf)
    {
        //If the leaf is pointing to something
        if(leaf!=NULL)
        {
            if(key==leaf->key_value)
            {
                //If the values are equal, return the name of the leaf
                return leaf;
            }
            else if(key<leaf->key_value)
            {
                //If the inputed value is less then the leaf's value, search left side for the inputed value
                return search(key,leaf->left);
            }
            else
            {
                //If the inputed value is greater then the leaf's value, search right side for the inputed value
                return search(key,leaf->right);
            }
        }
        else
        {
            //If the leaf isn't pointing to anything, return 0;
            return NULL;
        }
    }
    //The private counter-part to the "destroy_leaf()" function
    void btree::destroy_tree(node *leaf)
    {
        if(leaf!=NULL)
        {
            //If the leaf isn't NULL, delete it and start this function on each of it's leaves
            destroy_tree(leaf->left);
            destroy_tree(leaf->right);
            delete leaf;
        }
    }
    
    int main(void)
    {
        int i;
        int input1;
        int input2;
        char yn;
        btree tree;
        cout<<"Enter a starting value for the new binary tree: ";
        cin>>input1;
        cin.ignore();
        tree.insert(input1);
        cout<<"Enter the number of values you wish to insert to the binary tree: ";
        cin>>input1;
        cin.ignore();
        for(i=1;i<input1;i++)
        {
            cout<<i<<". Enter a number to insert into the binary tree: ";
            cin>>input2;
            cin.ignore();
            tree.insert(input2);
        }
        for(i=10;i<5;i++)
        {
            cout<<"Do you wish to search for an inputed number? (y/n) ";
            cin>>yn;
            cin.ignore();
            if(yn=='n')
            {    
                break;
            }
            else if(yn=='y')
            {
                 cout<<"Please input the number you wish to search for: ";
                 cin>>input2;
                 cin.ignore();
                 cout<<tree.search(input2)<<"\n";
            }
            else
            {
                cout<<"Error - You did not input y or n!\n";
            }
        }
        cin.get();
        return 0;
    }
    The Problem
    When I try to compile the program I get the error message: "multiple types in one declaration" on line 35. My compiler is Dev-C++ by the way. I underlined line 35 by the way, to show you where the problem is.

    Thanks for your help
    Ratchet
    Last edited by WingedPanther; 07-16-2008 at 09:48 AM. Reason: add code tags

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: C++ - Problem with a binary tree program; need help

    Missing semicolon.
    Code:
    struct node
    {
       int key_value;
       node *left;
       node *right;
    };

  4. #3
    Ratchet2246 is offline Newbie
    Join Date
    Jul 2008
    Posts
    12
    Rep Power
    0

    Re: C++ - Problem with a binary tree program; need help

    Thanks, that's fixed the problem. By the way why did the compiler tell me the error was on line 35 when it was on line 10 and what does the error message, "multiple types in one declaration" mean?

  5. #4
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: C++ - Problem with a binary tree program; need help

    When semicolons are missing, it makes a mess of the diagnostic messages. For what the compiler knew, you were ending the struct where the class ended -- and there you had too many types in the definition.

  6. #5
    Ratchet2246 is offline Newbie
    Join Date
    Jul 2008
    Posts
    12
    Rep Power
    0

    Re: C++ - Problem with a binary tree program; need help

    I get it now, thanks.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. problem with a binary search tree
    By hadas_zr in forum C and C++
    Replies: 3
    Last Post: 07-14-2011, 07:43 AM
  2. my program for creating a binary tree
    By onus in forum C and C++
    Replies: 0
    Last Post: 09-26-2010, 09:17 AM
  3. Binary tree
    By tomy in forum C and C++
    Replies: 6
    Last Post: 08-16-2010, 11:58 PM
  4. plz help me out to create binary tree
    By kuki in forum PHP Development
    Replies: 11
    Last Post: 03-30-2009, 08:53 AM
  5. plz help me out to create binary tree
    By kuki in forum PHP Development
    Replies: 0
    Last Post: 03-30-2009, 06:15 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts