Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- using namespace std;
- typedef struct node{
- int data;
- struct node *left;
- struct node *right;
- }nodeT;
- nodeT* newnode(int data){
- nodeT* newNode = new(nodeT);
- newNode->data = data;
- newNode->left = NULL;
- newNode->right = NULL;
- return newNode;
- }
- nodeT* addBST(nodeT* root,int data){
- nodeT* Nnode = newnode(data);
- if(root == NULL){
- return Nnode;
- }
- if(data >= root->data){
- root->right = addBST(root->right,data);
- }else{
- root->left = addBST(root->left,data);
- }
- return root;
- }
- typedef struct{
- int id;
- int count;
- }ct;
- int index(vector<ct> table,int key){
- for(int i=0;i<table.size();i++){
- if(table[i].id == key){
- return i;
- }
- }
- return -1;
- }
- void searchBST(nodeT *root,int key,vector<ct> table){
- if(root == NULL){
- cout << "0";
- return;
- }
- cout << root->data << " ";
- if(root->data < key){
- searchBST(root->right,key,table);
- }else if(root->data > key){
- searchBST(root->left,key,table);
- }else if(root->data == key){
- if(--table[index(table,key)].count > 0){ // has left
- searchBST(root->right,key,table);
- }else{
- return;
- }
- }
- }
- int main()
- {
- nodeT *root;
- root = NULL;
- int n;
- cin>>n;
- vector<ct> table;
- for(int i=0;i<n;i++){
- int x;
- cin>>x;
- root = addBST(root,x);
- int j;
- for(j=0;j<table.size();j++){
- if(table[j].id == x){
- table[j].count++;
- break;
- }
- }
- if(j == table.size()){
- table.push_back({x,1});
- }
- }
- int m;
- cin>>m;
- for(int i=0;i<m;i++){
- int x;
- cin>>x;
- int temp = table[index(table,x)].count;
- searchBST(root,x,table);
- table[index(table,x)].count = temp;
- cout<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment