Page Nav

HIDE

Grid

GRID_STYLE

Classic Header

{fbt_classic_header}

Search This Blog

Notes:

latest

Binary Search Tree

 #include <iostream> using namespace std; struct Node{     int data;     struct Node* left;     struct Node* right;     Node(int key) ...

 #include <iostream>

using namespace std;

struct Node{

    int data;

    struct Node* left;

    struct Node* right;

    Node(int key)

    {

        data=key;

        left=NULL;

        right=NULL;

    }

};

bool isNodeExists(struct Node* node,int key)

{

    if(node==NULL)

    {

        return false;

    }

    if(node->data==key)

    {

        return true;

    }

    bool res1 = isNodeExists(node->left,key);

    if(res1)

     return true;

    bool res2 = isNodeExists(node->right,key);

    if(res2)

    return res2;

}

int main() {

struct Node* root=new Node(1);

root->left=new Node(2);

root->right=new Node(3);

root->left->left=new Node(4);

root->left->right=new Node(5);

root->right->left=new Node(6);

root->right->right=new Node(7);


int key;

cout<<"Enter the key to search: ";

cin>>key;


if(isNodeExists(root,key))

{

    cout<<"Yes";

}

else

{

    cout<<"No";

}

return 0;

}

No comments

If you have any doubts, Please let me know,

Click here to show more posts