Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define pi acos(-1.0)
- #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
- #define ll long long
- #define pb push_back
- #define fi first
- #define se second
- #define in insert
- struct bst {
- ll data;
- ll left_subtree_size;
- ll right_subtree_size;
- struct bst* left;
- struct bst* right;
- struct bst* parent;
- };
- struct bst* root=NULL;
- void insert_bst(ll x)
- {
- struct bst*temp=new bst();
- temp->data=x;
- temp->left=NULL;
- temp->right=NULL;
- temp->parent=NULL;
- temp->left_subtree_size=temp->right_subtree_size=0;
- if(root==NULL)
- {
- root=temp;
- return;
- }
- struct bst * cur=root;
- while(1)
- {
- if(x>=cur->data)
- {
- cur->right_subtree_size++;
- if((cur->right)==NULL)
- {
- cur->right=temp;
- temp->parent=cur;
- return ;
- }
- else
- cur=cur->right;
- }
- else if(x<cur->data)
- {
- cur->left_subtree_size++;
- if((cur->left)==NULL)
- {
- cur->left=temp;
- temp->parent=cur;
- return ;
- }
- else
- cur=cur->left;
- }
- }
- }
- bool search(bst *root,int data)
- {
- if(root==NULL)
- return false;
- else if(root->data==data)
- return true;
- else if(root->data >data)
- return search(root->left,data);
- else
- return search(root->right,data);
- }
- int main()
- {
- insert_bst(8);
- insert_bst(10);
- insert_bst(14);
- insert_bst(13);
- insert_bst(3);
- insert_bst(6);
- insert_bst(1);
- insert_bst(4);
- insert_bst(7);
- cout<<"Search 6 : ";
- if(search(root,6))
- cout<<"Found"<<endl;
- else
- cout<<"Not Found"<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement