Created
January 19, 2017 03:38
-
-
Save tgvdinesh/f3b5e4e12e9820c24a266d688db68689 to your computer and use it in GitHub Desktop.
Tree DS implementation in C++ language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <list> | |
#include <map> | |
#include <set> | |
#include <queue> | |
#include <deque> | |
#include <stack> | |
#include <bitset> | |
#include <algorithm> | |
#include <functional> | |
#include <numeric> | |
#include <utility> | |
#include <sstream> | |
#include <iostream> | |
#include <iomanip> | |
#include <cstdio> | |
#include <cstring> | |
#include <cmath> | |
#include <cstdlib> | |
#include <ctime> | |
using namespace std; | |
class node | |
{ | |
public: | |
int val; | |
node * right, * left; | |
}; | |
void insert(node ** tree, node * item) | |
{ | |
if(!(*tree)) | |
{ | |
*tree = item; | |
return; | |
} | |
if(item->val<(*tree)->val) | |
{ | |
insert(&(*tree)->left, item); | |
} | |
else if(item->val>(*tree)->val) | |
{ | |
insert(&(*tree)->right, item); | |
} | |
} | |
int isPresent(node* root, int val){ | |
/* | |
The structure of the node is as follows: | |
class node { | |
public: | |
node * left, *right; | |
int val; | |
}; | |
*/ | |
} | |
int main() { | |
node * _root, * root_curr; | |
int root_i=0, root_cnt = 0, root_num = 0; | |
_root = NULL; | |
cin >> root_cnt; | |
for(root_i = 0; root_i < root_cnt; root_i++) | |
{ | |
scanf("%d", &root_num); | |
root_curr = (node *)malloc(sizeof(node)); | |
root_curr->left = root_curr->right = NULL; | |
root_curr->val = root_num; | |
insert(&_root, root_curr); | |
} | |
int q; | |
cin >> q; | |
while (q--) { | |
int _x; | |
cin >> _x; | |
cout << isPresent(_root, _x) << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment