Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std ;
- struct node
- {
- int data ;
- node *left ;
- node *right ;
- node (int _data)
- {
- data = _data ;
- left = right = NULL ;
- }
- };
- void printinorder(node *cur)
- {
- if(cur == NULL) return ;
- printinorder(cur->left) ;
- cout << cur->data << " " ;
- printinorder(cur->right) ;
- }
- void printpreorder(node *cur)
- {
- if(cur == NULL) return ;
- cout << cur->data << " " ;
- printpreorder(cur->left) ;
- printpreorder(cur->right) ;
- }
- void printpostorder(node *cur)
- {
- if(cur == NULL) return ;
- printpostorder(cur->left) ;
- printpostorder(cur->right) ;
- cout << cur->data << " " ;
- }
- int search(node *cur, int data)
- {
- if(cur==NULL) return 0;
- if(cur->data == data) return 1 ;
- else if(data <= cur->data) return search(cur->left, data) ;
- else if(data > cur->data) return search(cur->right, data) ;
- return 0 ;
- }
- void insert(node *cur, int _data)
- {
- if(_data <= cur->data)
- {
- if(cur->left == NULL)
- cur->left = new node(_data) ;
- else insert(cur->left,_data) ;
- }
- else if(_data > cur->data)
- {
- if(cur->right == NULL)
- cur->right = new node(_data) ;
- else insert(cur->right,_data) ;
- }
- }
- int UpperBound(node *cur, int _data)
- {
- if(_data < cur->data)
- {
- int store = cur->data ;
- if(_data >= cur->left->data || cur->left == NULL) return store ;
- else UpperBound(cur->left,_data) ;
- }
- else if(_data >= cur->data)
- {
- if(_data < cur->right->data)
- {
- int store = cur->right->data ;
- return store ;
- }
- else UpperBound(cur->right,_data) ;
- }
- }
- int LowerBound(node *cur, int _data)
- {
- if(_data <= cur->data)
- {
- if(_data > cur->left->data)
- {
- cur = cur->left ;
- if(cur->right == NULL) return cur->data ;
- while(cur != NULL) {
- cur = cur->right ;
- if(cur->right == NULL)
- {
- int store = cur->data ;
- return store ;
- }
- }
- }
- else LowerBound(cur->left,_data) ;
- }
- else if(_data > cur->data)
- {
- if(_data < cur->right->data)
- {
- return cur->data ;
- }
- else if(_data == cur->right->data)
- {
- int store = cur->data ;
- cur = cur->right ;
- int store1 = cur->left->data ;
- if(store <= store1) return store ;
- else return store1 ;
- }
- else LowerBound(cur->right,_data) ;
- }
- }
- int main(int argc, char const *argv[])
- {
- struct node *root =new node(4) ;
- root->left = new node(2) ;
- root->right = new node(6) ;
- root->left->left = new node(1) ;
- root->left->right = new node(3) ;
- root->right->left = new node(5) ;
- root->right->right = new node(9) ;
- printpreorder(root) ;
- cout << endl ;
- printinorder(root) ;
- cout << endl ;
- printpostorder(root) ;
- cout << endl ;
- int val = search(root,9) ;
- if(val) cout << "Found" << endl ;
- else cout << "Not Found" << endl ;
- insert(root,9) ;
- printinorder(root) ;
- cout << endl ;
- insert(root,-3) ;
- printinorder(root) ;
- cout << endl ;
- insert(root,2) ;
- insert(root,3) ;
- printinorder(root) ;
- cout << endl ;
- cout << UpperBound(root,8) << endl ;
- cout << LowerBound(root,9) << endl ;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement