Ahoj, mohl by mi někdo prosím poradit co mám špatně v tomto C++ kodu? Jde o tu metodu bool search(int), která by měla vzít uživatelem zadané číslo a zjistit zdali strom obsahuje takovýto uzel. Podle toho by měla vracet buď TRUE nebo FALSE. Bohužel při spuštění programu se nic nevrací (program se spustí, žádne chyby se neobjeví, po zadání čísla jakoby ta metoda proběhne až po ten brake ale nevrátí to ani TRUE ani FALSE). Děkuji za pomoc.
#include <process.h>
#include <conio.h>
#include <iostream>
#include <cstdlib>
using namespace std;
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
int data;
};
tree_node* root;
public:
BinarySearchTree()
{
root = NULL;
}
void insert(int);
bool isEmpty() const { return root==NULL; }
bool search(int);
};
//----------------------------------------------------------
void BinarySearchTree::insert(int d)
{
tree_node* t = new tree_node;
tree_node* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
parent = NULL;
// is this a new tree?
if(isEmpty()) root = t;
else
{
//Note: ALL insertions are as leaf nodes
tree_node* curr;
curr = root;
// Find the Node's parent
while(curr)
{
parent = curr;
if(t->data > curr->data) curr = curr->right;
else curr = curr->left;
}
if(t->data < parent->data)
parent->left = t;
else
parent->right = t;
}
}
bool BinarySearchTree::search(int d) {
tree_node* temp = root;
while (temp != NULL) {
if (temp->data == d) {
return true;
}
else {
if (d > temp->data) {
temp = temp->right;
}
else {
temp = temp->left;
}
}
}
return false;
}
//----------------------------------------------------------
int main()
{
BinarySearchTree b;
int ch,tmp,tmp1;
while(1)
{
cout<<endl<<endl;
cout<<" Binary Search Tree Operations "<<endl;
cout<<" ----------------------------- "<<endl;
cout<<" 1. Insertion/Creation "<<endl;
cout<<" 2. Does BST contain this number? "<<endl;
cout<<" 3. Exit "<<endl;
cout<<" Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1 : cout<<" Enter Number to be inserted : ";
cin>>tmp;
b.insert(tmp);
break;
case 2 : cout<<" Enter number to be found : ";
cin>>tmp1;
b.search(tmp1);
break;
case 3 : system("pause");
return 0;
break;
}
}
}