So I am trying to create a simple binary tree that will ask the user for a command so that is either 'insert or find' and with insert they can put in a number to this tree and with find they can ask if 53 is in this tree and the program will output yes or no..
Help plz.
So by the looks of your solution (or lack there of), I'm guessing you got no idea have you...google binary search tree and maybe that should get you started.
that example looks fine except that it won't work because:
(1) - since your struct node is private, you'd have to create member functions, that goes in the public part of your class BTree, that will access your private members, look up setters and getters. If your quite new to it I'd suggest using setters and getters, otherwise use your class constructor to do it.
(2) - member functions are declared but not defined, they are functions after all and functions need to be declared then defined. You are also missing left and right pointers since trees have branches going left and right.
Example
-----------
//declaration
void postorder(node*);
//definition - define your functions outside your class
void BTree::postorder(root)
{
postorder(root->left);
postorder(root->right);
root->data();
}
this code is what post order traverse looks like but this code will not necessarily work since your node*root pointer is private and so too data.
Trust me its better to understand the concept first do not worry too much about the syntax, that will come later but once you understand the concept then you will see that it will get a lot easier and clearer.
your binary tree shell code looks mostly correct. You will not be able to search anything unless you populate your tree first, currently there is nothing in your tree, its empty. You probably have to manually hard code some numbers to your tree or read some numbers from a file and store those numbers to the tree and then search if a particular number exits in the tree.
Your binary tree code looks okay but you're only inputting a single number. Try changing the insert and find code in main so they accept multiple values:
1 2 3 4 5 6 7 8 9 10 11 12
while (true) {
cout << "Enter a value to insert, or -1 to quit: ";
cin >> no;
if (no == -1) break;
n.insert(no);
}
while (true) {
cout<<" Enter a value to find, or -1 to quit: "<<endl;
cin>>fi;
if (fi == -1) break;
cout << "find() returned " << n.find(fi) << ends;
}