Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Binary search tree
- #include<bits/stdc++.h>
- using namespace std;
- struct BstNode{
- int data;
- BstNode* left;
- BstNode* right;
- };
- BstNode* GetNewNode(int data)
- {
- BstNode* NewNode= new BstNode();
- NewNode->data=data;
- NewNode->left=NewNode->right=NULL;
- return NewNode;
- }
- BstNode* insert(BstNode* root, int data)
- {
- if(root==NULL)
- root=GetNewNode(data);
- else if(data<=root->data)
- root->left=insert(root->left, data);
- else if(data>root->data)
- root->right=insert(root->right, data);
- return root;
- }
- bool search(BstNode* root, int data)
- {
- if(root==NULL)
- return false;
- else if(root->data==data)
- return true;
- else if(data<=root->data)
- return search(root->left, data);
- else
- return search(root->right, data);
- }
- int main()
- {
- BstNode* root=NULL;
- root=insert(root, 50);
- root=insert(root, 90);
- root=insert(root, 7);
- root=insert(root, -5);
- if(search(root, -5))
- cout<<"-5 is found in the binary search tree"<<'\n';
- if(!search(root, 8))
- cout<<"8 isn't found in the binary search tree";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement