I feel like this should work. I can get other binary tree leafs, but this one is giving me an error. Am I somehow changing it and I don't know? I've been debugging this for hours and I can't get it to work. Thanks in advance for your help.
Everything else is working for me on this, so I'm not sure where my logic went wrong.
struct node{
int id;
string data;
node* left;
node* right;
};
class binSearchTree
{
private:
node* head;
public:
binSearchTree(node* n);
node* CreateLeaf(int);
// change add to void
void add(int id, node* & nPtr);
void inorderTraverse(node* nPtr);
node* getEntry(int id, node* nPtr);
int getID(node* nPtr);
node* getHead();
string getData(node* nPtr);
// bool remove(int);
};
binSearchTree::binSearchTree(node* n){
this->head = n;
}
node* binSearchTree::CreateLeaf(int id){
node*n = new node;
n->data = "This has id number: ";
n->id = id;
n->left = NULL;
n->right = NULL;
// returns pointer to node
return n;
}
void binSearchTree::add(int id, node* & nPtr){
// if root is empty, set root to NULL
if(nPtr == NULL){
nPtr = CreateLeaf(id);
cout << "Adding head " << nPtr->id << endl;
}
// Check if need to go left
else if(id < nPtr->id){
// if id is less and left not NULL, go left again
if(nPtr->left != NULL){
add(id, nPtr->left);
}
else{
nPtr->left = CreateLeaf(id);
}
}
// go right
else if(id > nPtr->id){
if(nPtr->right != NULL){
add(id, nPtr->right);
}
else{
nPtr->right = CreateLeaf(id);
}
}
// if id has already been used print out error message
else{
cout << "The id " << id << " has already been used" << endl;
}
}
void binSearchTree::inorderTraverse(node* nPtr){
// If the binary tree isn't empty
if(nPtr != NULL){
// go left
if(nPtr->left != NULL){
inorderTraverse(nPtr->left);
}
// print out the id
cout << "Now printing out " << nPtr->id << endl;
// go right
if(nPtr->right != NULL){
inorderTraverse(nPtr->right);
}
}
// If empty output that binary tree is empty
else{
cout << "This binary tree is empty" << endl;
}
}
node* binSearchTree::getEntry(int find, node* nPtr){
//if head passed in is NULL, return NULL
// this is the base case
if(nPtr == NULL){
// cout << "Nothing here" << endl;
return NULL;
}
else{
// return node if found
if(nPtr->id == find){
return nPtr;
}
// if less go left
else if(find < nPtr->id){
return getEntry(find, nPtr->left);
}
// if more go right
else if(find > nPtr->id){
return getEntry(find, nPtr->right);
}
}
}
int binSearchTree::getID(node* nPtr){
return nPtr->id;
}
string binSearchTree::getData(node* nPtr){
return nPtr->data;
}
node* binSearchTree::getHead(){
return this->head;
}
int main(){
int TreeKeys[16] = {50,76, 21, 4, 32, 64, 15, 52, 14, 100, 83, 2, 3, 70, 87, 80};
node* head;
head = NULL;
binSearchTree myTree(head);
myTree.inorderTraverse(head);
for(int i = 0; i < 16; i++){
myTree.add(TreeKeys[i], head);
}
myTree.inorderTraverse(head);
node* temp;
temp = myTree.getEntry(50, head);
cout << myTree.getData(temp) << myTree.getID(temp) << endl;
delete temp;
temp = NULL;
temp = myTree.getEntry(80, head);
cout << myTree.getData(temp) << myTree.getID(temp) << endl;
delete temp;
temp = NULL;
temp = myTree.getEntry(120, head);
if(temp != NULL){
cout << myTree.getData(temp) << myTree.getID(temp) << endl;
}
else{
cout << "Not found" << endl;
}
delete temp;
// Get the head
temp = NULL;
temp = myTree.getHead();
cout << myTree.getData(temp) << myTree.getID(temp) << endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire