Advertisement
mickypinata

SMMR-T083: Binary Search Tree

Mar 27th, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. typedef struct node{
  6.     int data;
  7.     node *left;
  8.     node *right;
  9.     node(int item){
  10.         data = item;
  11.         left = NULL;
  12.         right = NULL;
  13.     }
  14.     node(int item, node *r, node *l){
  15.         data = item;
  16.         left = l;
  17.         right = r;
  18.     }
  19. }node;
  20.  
  21. vector<int> tmp;
  22. vector<int> ans;
  23. int nn, ql;
  24. bool found;
  25. node *root;
  26.  
  27. void PrintPreorder(node *root){
  28.     if(root == NULL){
  29.         return;
  30.     } else {
  31.         cout << root -> data << " ";
  32.         PrintPreorder(root -> left);
  33.         PrintPreorder(root -> right);
  34.         return;
  35.     }
  36. }
  37.  
  38. node *InsertNode(node *root, int data){
  39.     if(root == NULL){
  40.         return new node(data);
  41.     } else {
  42.         node *curr = root;
  43.         node *prev;
  44.         while(curr != NULL){
  45.             prev = curr;
  46.             if(data < curr -> data){
  47.                 curr = curr -> left;
  48.             } else {
  49.                 curr = curr -> right;
  50.             }
  51.         }
  52.         if(data < prev -> data){
  53.             prev -> left = new node(data);
  54.         } else {
  55.             prev -> right = new node(data);
  56.         }
  57.         return root;
  58.     }
  59. }
  60.  
  61. void FindNode(node *root, int tr){
  62.     if(tr == root -> data){
  63.         tmp.push_back(root -> data);
  64.         ans = tmp;
  65.         found = true;
  66.         if(root -> right != NULL){
  67.             FindNode(root -> right, tr);
  68.         }
  69.         return;
  70.     } else if(tr < root -> data){
  71.         tmp.push_back(root -> data);
  72.         if(root -> left != NULL){
  73.             FindNode(root -> left, tr);
  74.         } else {
  75.             tmp.push_back(0);
  76.             if(!found){
  77.                 ans = tmp;
  78.             }
  79.         }
  80.         return;
  81.     } else {
  82.         tmp.push_back(root -> data);
  83.         if(root -> right != NULL){
  84.             FindNode(root -> right, tr);
  85.         } else {
  86.             tmp.push_back(0);
  87.             if(!found){
  88.                 ans = tmp;
  89.             }
  90.         }
  91.         return;
  92.     }
  93. }
  94.  
  95. int main(){
  96.  
  97.     int x;
  98.  
  99.     scanf("%d", &nn);
  100.     for(int i = 0; i < nn; ++i){
  101.         scanf("%d", &x);
  102.         root = InsertNode(root, x);
  103.     }
  104.     scanf("%d", &ql);
  105.     for(int i = 0; i < ql; ++i){
  106.         scanf("%d", &x);
  107.         tmp.clear();
  108.         found = false;
  109.         FindNode(root, x);
  110.         for(auto x : ans){
  111.             cout << x << " ";
  112.         }
  113.         cout << "\n";
  114.     }
  115.  
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement