Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-15-2008, 03:09 PM
Ratchet2246 Ratchet2246 is offline
Newbie
 
Join Date: Jul 2008
Location: Moray, Scotland
Age: 14
Posts: 12
Rep Power: 0
Ratchet2246 is on a distinguished road
Default 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 12:48 PM. Reason: add code tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-15-2008, 03:20 PM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: C++ - Problem with a binary tree program; need help

Missing semicolon.
Code:
struct node
{
   int key_value;
   node *left;
   node *right;
};
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-15-2008, 03:28 PM
Ratchet2246 Ratchet2246 is offline
Newbie
 
Join Date: Jul 2008
Location: Moray, Scotland
Age: 14
Posts: 12
Rep Power: 0
Ratchet2246 is on a distinguished road
Default 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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-15-2008, 03:30 PM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-15-2008, 03:32 PM
Ratchet2246 Ratchet2246 is offline
Newbie
 
Join Date: Jul 2008
Location: Moray, Scotland
Age: 14
Posts: 12
Rep Power: 0
Ratchet2246 is on a distinguished road
Default Re: C++ - Problem with a binary tree program; need help

I get it now, thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial: Starting C# with C# 2008 Express Edition Jordan CSharp Tutorials 19 08-08-2008 01:48 PM
BSP Tree Problem bananamilk C# Programming 0 06-26-2008 04:28 PM
How to write the code to delete the whole binary search tree ?? worried_student C and C++ 1 11-23-2007 11:09 AM
need help with simple C++ TicTacToe game with AI flupke1 C and C++ 11 08-14-2007 11:27 AM


All times are GMT -5. The time now is 11:04 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads