I'm working on adding fruit into a binary subtree for school and I wrote a program and these are the compilation errors I'm getting. I cannot see any glaring syntax errors and I know it helps to have other sets of eyes inspect the code as well.
//BST.CPP//
#include <iostream>
#include <cstdlib>
#include "BST.h"
using namespace std;
BST::BST(){
root = NULL;
}
BST::node* BST::CreateLeaf(int key, string name){
node* n = new node;
n->key = key;
n->name = name;
n->left = NULL;
n->right = NULL;
return n;
}
void BST::AddLeaf(int key, string name){
AddLeafPrivate(key, name, root);
}
void BST::AddLeafPrivate(int key, string name, node* ptr){
if(root == NULL){
root = CreateLeaf(key, name);
}
else if(key < ptr->key)
{
if(ptr->left != NULL){
AddLeafPrivate(key, name, ptr->left);
}
else{
ptr->left = CreateLeaf(key, name);
}
}
else if(key > ptr->key)
{
if(ptr->right != NULL){
AddLeafPrivate(key, name, ptr->right);
}
else{
ptr->right = CreateLeaf(key, name);
}
}
else{
cout << "The fruit"<< name<<" has alreadt been added \n";
}
}
void BST::PrintInOrder(){
PrintInOrderPrivate(root);
}
void BST::PrintInOrderPrivate(node* ptr){
if(root!= NULL){
if(ptr->left != NULL){
PrintInOrderPrivate(ptr->left);
}
cout<< ptr->key<< " ";
cout << ptr->name<< " ";
if(ptr->right != NULL){
PrintInOrderPrivate(ptr->right);
}
}
else{
cout<<"The tree is empty\n";
}
}
//BST.h//
#include <string>
using namespace std;
class BST{
private:
struct node{
int key;
string name;
node* left;
node* right;
};
node* root;
void AddLeafPrivate(int key, string name, node* ptr);
void PrintInOrderPrivate(node* ptr);
public:
BST();
node* CreateLeaf(int key, string name);
void AddLeaf(int key, string name);
void PrintInOrder();
};
//main.cpp//
#include <iostream>
#include <cstdlib>
#include "BST23.cpp"
using namespace std;
int main(){
int treekeys[7] = {1 , 2, 3, 4, 5, 6 ,7};
string treenames[7] = {"blueberry", "peach", "apricot", "pear", "cherry", "mango", "papaya"};
BST tree;
cout << "Tree before adding any values:\n";
tree.PrintInOrder();
for(int i=0; i < 7; i++){
tree.AddLeaf(treekeys[i], treenames[i]);
}
cout<<"Printing tree in order";
tree.PrintInOrder();
return 0;
}