c++ - How to find out if Binary Search Tree contains a node given by user -
c++ - How to find out if Binary Search Tree contains a node given by user -
i create bool method bool search(int) find out if bst(binary search tree) contains node given user.
i know algorithm: start comparing number given user number of bst root. there 3 possibilities: 1.given number equal root - search ends successfully, method returns true 2.given number greater root - go on recursively on right descendant (right subtree) 3.given number less root - go on recursively on left descendant (left subtree)
i know concept of problem , how should work still can not write code works. please help me prepare it?
here code (it doesn´t work think concept correct):
#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 { homecoming 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; // new tree? if(isempty()) root = t; else { //note: insertions leaf nodes tree_node* curr; curr = root; // find 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) { if (d == tree_node* root) homecoming true; else if (d > tree_node* root) { if (this->right != null) // case has right descendant this->right->search(d); else homecoming false; } else { if (this->left != null) // case has left descendant this->left->search(d); else homecoming 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. bst contain number? "<<endl; cout<<" 3. exit "<<endl; cout<<" come in selection : "; cin>>ch; switch(ch) { case 1 : cout<<" come in number inserted : "; cin>>tmp; b.insert(tmp); break; case 2 : cout<<" come in number found : "; cin>>tmp1; b.search(tmp1); break; case 3 : system("pause"); homecoming 0; break; } } }
you can using while loop. iterate through proper nodes. if value found returns true, else if temp node becomes null , value not found, loop terminates , false returned.
bool binarysearchtree::search(int d) { tree_node* temp = root; while (temp != null) { if (temp->data == d) { homecoming true; } else { if (d > temp->data) { temp = temp->right; } else { temp = temp->left; } } } homecoming false; } line of code: this->right->search(d); phone call function called search right structure. right of type tree_node, there's no function search in it, hence error. suppose trying create recursive phone call search class.
c++ binary-search-tree
Comments
Post a Comment