PDA

View Full Version : C++ Binary Search Error

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;

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);
}
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!

Sander
Jul 6, 2011, 03:12 AM
Of a new TreeNode you construct, what are its initial values for "parent", "left", and "right"..? And what is the initial value of "root" when you construct a new Tree with its default constructor..?

jCrabb13
Jul 6, 2011, 05:47 PM
Of a new TreeNode you construct, what are its initial values for "parent", "left", and "right"..? And what is the initial value of "root" when you construct a new Tree with its default constructor..?

:eek: they weren't defined. but even when i make them all NULL, i still pull the same error. this is what happens in the console:

chown33
Jul 6, 2011, 06:10 PM
:eek: they weren't defined. but even when i make them all NULL, i still pull the same error. this is what happens in the console:

First: use the debugger. See what happens after the console output of "TreeNode made: 3". In other words, where is the EXC_BAD_ACCESS coming from?

If you don't know how to use the debugger, now's a good time to learn. You should at least be able to set breakpoints, step through code a line at a time, and examine values of variables. That's the minimum for a debugger to be useful.

Second: point out the code that assigns something into left, right, or parent after a new TreeNode is created.

naples98
Jul 6, 2011, 06:11 PM
:eek: they weren't defined. but even when i make them all NULL, i still pull the same error. this is what happens in the console:

It has been awhile since I've done C++ but I'm pretty sure you never initialize Tree T in your BinarySearch struct so you are attempting to access some random memory location.

jCrabb13
Jul 6, 2011, 08:35 PM
First: use the debugger. See what happens after the console output of "TreeNode made: 3". In other words, where is the EXC_BAD_ACCESS coming from?

If you don't know how to use the debugger, now's a good time to learn. You should at least be able to set breakpoints, step through code a line at a time, and examine values of variables. That's the minimum for a debugger to be useful.

Second: point out the code that assigns something into left, right, or parent after a new TreeNode is created.

the debugger has it stopping after the first loop on this line (47) in the Tree.h file. i think it cant figure out what t->value equals but i dont know why.

It has been awhile since I've done C++ but I'm pretty sure you never initialize Tree T in your BinarySearch struct so you are attempting to access some random memory location.

thats the way we did it all year...i'm pretty sure that parts right.

ehoui
Jul 6, 2011, 09:40 PM
Ask yourself where you are setting the left, right and parent values of a new node. You are not. That's a problem.

jCrabb13
Jul 6, 2011, 10:00 PM
Ask yourself where you are setting the left, right and parent values of a new node. You are not. That's a problem.

Ya I realized that and adjusted the TreeNode's default constructor to this:

TreeNode()
{
value=NULL;
parent=NULL;
left=NULL;
right=NULL;
}

and the tree's to this:

Tree()
{
root=NULL;
}

still pulling the exact same error though! :/

ehoui
Jul 6, 2011, 10:11 PM
Ya I realized that and adjusted the TreeNode's default constructor to this:

TreeNode()
{
value=NULL;
parent=NULL;
left=NULL;
right=NULL;
}

and the tree's to this:

Tree()
{
root=NULL;
}

still pulling the exact same error though! :/

And where are those constructors being called? These are the questions you need to ask yourself. The debugger would tell you that TreeNode() is not being called. Why because are calling a different constructor: TreeNode::TreeNode(int) not TreeNode::TreeNode(). I think you need a review of the earlier chapters :-).

jCrabb13
Jul 6, 2011, 10:16 PM
And where are those constructors being called? These are the questions you need to ask yourself. The debugger would tell you that TreeNode() is not being called. Why because are calling a different constructor: TreeNode::TreeNode(int) not TreeNode::TreeNode(). I think you need a review of the earlier chapters :-).

:rolleyes: God I am so stupid. lol. it's always something like that that I forget to do, thanks a ton! i'll update the thread if i get stumped again!