C++ Binary Search Error


lucky me student @ texas a&m , bjarne stroustrup @ school. have made past classes book covers idea , general coding knowledge book. being said, lets it!

trying make binary_search function using tree find element in vector (way more work worth me that's question asks for) comes 26.1 , exercise 1 of c26 stroustrup's programming principles , practice tree requirement coming current prof.

can't seem figure out problem is. have made 1 before , used same structure, keeps stopping after first loop on line (47) in tree.h file when trying insert vector elements tree.

code:
else if(n < (t->value))
here files 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<<" 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<<" 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 appreciated. finishing problem along others difference in important letter grade me.

, gig 'em!
 

of new treenode construct, initial values "parent", "left", , "right"..? , initial value of "root" when construct new tree default constructor..?
 


Forums Macs Mac Programming


  • iPhone
  • Mac OS & System Software
  • iPad
  • Apple Watch
  • Notebooks
  • iTunes
  • Apple ID
  • iCloud
  • Desktop Computers
  • Apple Music
  • Professional Applications
  • iPod
  • iWork
  • Apple TV
  • iLife
  • Wireless

Comments

Popular posts from this blog

Has to be the funniest headline ive ever seen

Man Brings Goat to Apple Store and has Pizza Delivered

Elusive Ninja: The Shadowy Thief - Can you survive the fast-paced ninja chaos?