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