jCrabb13

Jul 5, 2011, 11:34 PM

Lucky for me I am a student at Texas A&M and Bjarne Stroustrup is at my school. I have made it past the classes that his book covers but the idea and general coding knowledge is mostly from that book. with that being said, lets get to it!

I am trying to make a binary_search function using a tree to find an element in a vector (way more work than its worth to me but that's what the question asks for) This comes from 26.1 and then Exercise 1 of c26 from Stroustrup's Programming Principles and Practice with the tree requirement coming from my current prof.

I can't seem to figure out what my problem is. I have made one before and I used the same structure, but it keeps stopping after the first loop on this line (47) in the Tree.h file when trying to insert my vector elements into the tree.

else if(n < (t->value))

Here are the files I am using;

main.cpp

#include <iostream>

#include "vector.h"

#include "BinarySearch.h"

int main ()

{

vector<int> nums;

nums.push_back(3);

nums.push_back(5);

nums.push_back(9);

nums.push_back(13);

nums.push_back(13);

nums.push_back(20);

nums.push_back(56);

nums.push_back(78);

nums.push_back(94);

nums.push_back(99);

BinarySearch BS(nums);

if(BS.isEmpty())

cout<<"well ****"<<endl;

if(BS.find(56)) cout<<"FOUND"<<endl;

else cout<<"NOT FOUND"<<endl;

return 0;

}

tree.h

#include <cstddef>

#include <iostream>

struct TreeNode

{

int value;

TreeNode* parent;

TreeNode* left;

TreeNode* right;

TreeNode()

{

}

TreeNode(int n)

{

value=n;

}

};

struct Tree

{

TreeNode *root;

Tree()

{

}

Tree(int n)

{

root = new TreeNode(n);

}

TreeNode *getRoot()

{

return root;

}

void insert(int n,TreeNode *t)

{

if(t==NULL)

{

root = new TreeNode(n);

cout<<"TreeNode made: "<<n<<endl;

}

else if(n < (t->value))

{

insert(n,t->left);

cout<<n<<" moved left"<<endl;

}

else if(n > (t->value))

{

insert(n,t->right);

cout<<n<<" moved right"<<endl;

}

//error protection

else if(n==t->value)

cout<<n<<" is a duplicate value. SKIPPING ENTRY"<<endl;

}

int find(int n, TreeNode *t)

{

while(t!=NULL)

{

if(n<t->value) t=t->left;

else if(n>t->value) t=t->right;

else

{

cout<<n<<" is found!"<<endl;

return true;

}

}

return false;

}

};

BinarySearch.h

#include <math.h>

#include "Tree.h"

struct BinarySearch

{

Tree T;

BinarySearch()

{

}

BinarySearch(vector<int> n)

{

for(int i=0;i<n.size();i++)

{

T.insert((int)n[i],T.root);

}

}

bool isEmpty()

{

if(T.root==NULL)

return true;

return false;

}

bool find(int n)

{

return T.find((int)n,T.getRoot());

}

};

any help would be greatly appreciated. finishing this problem along with some others is the difference in an important letter grade for me.

Thanks and Gig 'em!

I am trying to make a binary_search function using a tree to find an element in a vector (way more work than its worth to me but that's what the question asks for) This comes from 26.1 and then Exercise 1 of c26 from Stroustrup's Programming Principles and Practice with the tree requirement coming from my current prof.

I can't seem to figure out what my problem is. I have made one before and I used the same structure, but it keeps stopping after the first loop on this line (47) in the Tree.h file when trying to insert my vector elements into the tree.

else if(n < (t->value))

Here are the files I am using;

main.cpp

#include <iostream>

#include "vector.h"

#include "BinarySearch.h"

int main ()

{

vector<int> nums;

nums.push_back(3);

nums.push_back(5);

nums.push_back(9);

nums.push_back(13);

nums.push_back(13);

nums.push_back(20);

nums.push_back(56);

nums.push_back(78);

nums.push_back(94);

nums.push_back(99);

BinarySearch BS(nums);

if(BS.isEmpty())

cout<<"well ****"<<endl;

if(BS.find(56)) cout<<"FOUND"<<endl;

else cout<<"NOT FOUND"<<endl;

return 0;

}

tree.h

#include <cstddef>

#include <iostream>

struct TreeNode

{

int value;

TreeNode* parent;

TreeNode* left;

TreeNode* right;

TreeNode()

{

}

TreeNode(int n)

{

value=n;

}

};

struct Tree

{

TreeNode *root;

Tree()

{

}

Tree(int n)

{

root = new TreeNode(n);

}

TreeNode *getRoot()

{

return root;

}

void insert(int n,TreeNode *t)

{

if(t==NULL)

{

root = new TreeNode(n);

cout<<"TreeNode made: "<<n<<endl;

}

else if(n < (t->value))

{

insert(n,t->left);

cout<<n<<" moved left"<<endl;

}

else if(n > (t->value))

{

insert(n,t->right);

cout<<n<<" moved right"<<endl;

}

//error protection

else if(n==t->value)

cout<<n<<" is a duplicate value. SKIPPING ENTRY"<<endl;

}

int find(int n, TreeNode *t)

{

while(t!=NULL)

{

if(n<t->value) t=t->left;

else if(n>t->value) t=t->right;

else

{

cout<<n<<" is found!"<<endl;

return true;

}

}

return false;

}

};

BinarySearch.h

#include <math.h>

#include "Tree.h"

struct BinarySearch

{

Tree T;

BinarySearch()

{

}

BinarySearch(vector<int> n)

{

for(int i=0;i<n.size();i++)

{

T.insert((int)n[i],T.root);

}

}

bool isEmpty()

{

if(T.root==NULL)

return true;

return false;

}

bool find(int n)

{

return T.find((int)n,T.getRoot());

}

};

any help would be greatly appreciated. finishing this problem along with some others is the difference in an important letter grade for me.

Thanks and Gig 'em!