Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jCrabb13

macrumors newbie
Original poster
Apr 8, 2010
29
0
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.

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

Here are the files I am using;

main.cpp
Code:
#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
Code:
#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
Code:
#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

macrumors 6502a
Apr 24, 2008
520
67
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

macrumors newbie
Original poster
Apr 8, 2010
29
0
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:


TreeNode made: 3
Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
: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:


TreeNode made: 3
Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all

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

macrumors member
Sep 9, 2008
95
3
Houston
: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:


TreeNode made: 3
Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all

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

macrumors newbie
Original poster
Apr 8, 2010
29
0
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

macrumors regular
Jan 27, 2011
217
0
Ask yourself where you are setting the left, right and parent values of a new node. You are not. That's a problem.
 
Last edited:

jCrabb13

macrumors newbie
Original poster
Apr 8, 2010
29
0
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:

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

and the tree's to this:

Code:
Tree() 
	{
	   root=NULL;
	}

still pulling the exact same error though! :/
 

ehoui

macrumors regular
Jan 27, 2011
217
0
Ya I realized that and adjusted the TreeNode's default constructor to this:

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

and the tree's to this:

Code:
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

macrumors newbie
Original poster
Apr 8, 2010
29
0
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.