Advertisement
TAHMID37

BST_BY_TAHMID

Jun 28th, 2021 (edited)
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.74 KB | None | 0 0
  1. /*  TAHMID RAHMAN
  2.     DAMIAN FOREVER
  3.      MATH LOVER
  4.     NEVER GIVE UP
  5. */
  6. #include<bits/stdc++.h>
  7. using namespace std;
  8. #define pi acos(-1.0)
  9. #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  10. #define ll long long
  11. #define pb push_back
  12. #define fi first
  13. #define se second
  14. #define in insert
  15. #define mp make_pair
  16. #define GCD(a,b) __gcd(a,b);
  17. #define endl "\n"
  18. #define FRU freopen("out.txt","w",stdout)
  19. #define FRO freopen("in.txt","r",stdin)
  20. #define INFLL 9223372036854775807
  21. #define all(x) (x).begin(),(x).end()
  22. #define debug 0
  23. #define MAXN   100001
  24. #define ar array
  25. #define lb lower_bound
  26. #define ub upper_bound
  27. #define minpq priority_queue<ll, vector<ll>, greater<ll>>
  28. #define maxpq priority_queue<ll>
  29. const int mxN=2e5;
  30. const int MOD=1e9+7;
  31. template<typename ForwardIterator, typename T>
  32. ForwardIterator first_less_than (ForwardIterator first, ForwardIterator last, T value)
  33. {auto it = std::lower_bound (first, last, value);
  34. return (it == first ? last : --it);}
  35. bool sortbysec(const pair<int,int> &a,const pair<int,int> &b)
  36. {
  37.     return (a.second < b.second);
  38. }
  39. #define debugxx(v) {for(auto x:v){cout<<x.fi<<" "<<x.se<<endl;}cout<<endl;}
  40. #define debugx(v){for(auto y:v) {cout<<y<<" ";}cout<<endl;}
  41. //Don't hesitate to ask me if you don't understand my code.......Happy coding,Tahmid...;
  42.  
  43. struct bst {
  44.  
  45.     ll data;
  46.     ll left_subtree_size;
  47.     ll right_subtree_size;
  48.     struct bst* left;
  49.     struct bst* right;
  50.     struct bst* parent;
  51.  
  52.  
  53. };
  54.  
  55. struct bst* root=NULL;
  56.  
  57. void insert_bst(ll x)
  58. {
  59.     struct bst*temp=new bst();
  60.  
  61.     temp->data=x;
  62.     temp->left=NULL;
  63.     temp->right=NULL;
  64.     temp->parent=NULL;
  65.     temp->left_subtree_size=temp->right_subtree_size=0;
  66.  
  67.  
  68.     if(root==NULL)
  69.     {
  70.         root=temp;
  71.         return;
  72.     }
  73.  
  74.     struct bst * cur=root;
  75.  
  76.     while(1)
  77.     {
  78.         if(x>=cur->data)
  79.         {
  80.             cur->right_subtree_size++;
  81.  
  82.             if((cur->right)==NULL)
  83.             {
  84.                 cur->right=temp;
  85.                 temp->parent=cur;
  86.                 return ;
  87.  
  88.             }
  89.             else
  90.                 cur=cur->right;
  91.         }
  92.         else if(x<cur->data)
  93.         {
  94.             cur->left_subtree_size++;
  95.  
  96.             if((cur->left)==NULL)
  97.             {
  98.                 cur->left=temp;
  99.                 temp->parent=cur;
  100.                 return ;
  101.  
  102.             }
  103.             else
  104.                 cur=cur->left;
  105.         }
  106.     }
  107.  
  108.  
  109. }
  110.  
  111. bool search(bst *root,int data)
  112. {
  113.     if(root==NULL)
  114.         return false;
  115.     else if(root->data==data)
  116.         return true;
  117.     else if(root->data >data)
  118.         return search(root->left,data);
  119.     else
  120.         return search(root->right,data);
  121. }
  122.  
  123.  
  124. void inorder(bst *root)
  125. {
  126.  
  127.     if(!root)
  128.         return ;
  129.  
  130.     inorder(root->left);
  131.     cout<<root->data<<" ";
  132.     inorder(root->right);
  133. }
  134.  
  135.  
  136.  
  137. void preorder(bst *root)
  138. {
  139.  
  140.     if(!root)
  141.         return ;
  142.  
  143.     cout<<root->data<<" ";
  144.     preorder(root->left);
  145.     preorder(root->right);
  146. }
  147.  
  148.  
  149. void postorder(bst *root)
  150. {
  151.  
  152.     if(!root)
  153.         return ;
  154.  
  155.     postorder(root->left);
  156.     postorder(root->right);
  157.     cout<<root->data<<" ";
  158.  
  159. }
  160.  
  161.  
  162. int tree_max(bst *root)
  163. {
  164.     while(root->right!=NULL)
  165.         root=root->right;
  166.  
  167.     return root->data;
  168. }
  169.  
  170.  
  171.  
  172. int tree_min(bst *root)
  173. {
  174.     while(root->left!=NULL)
  175.         root=root->left;
  176.  
  177.     return root->data;
  178. }
  179.  
  180. int find_height(bst * root)
  181. {
  182.     if(root==NULL)
  183.         return -1;
  184.  
  185.  
  186.  
  187.     ll x1=find_height(root->left);
  188.  
  189.     ll x2=find_height(root->right);
  190.  
  191.  
  192.     return(max(x1,x2)+1);
  193. }
  194.  
  195.  
  196. ll min_depth(bst * root)
  197. {
  198.  
  199.     if(root==NULL)
  200.         return 0;
  201.     queue<bst*>q;
  202.  
  203.     ll level=0;
  204.  
  205.     q.push(root);
  206.  
  207.  
  208.     while(q.size())
  209.     {
  210.         level++;
  211.  
  212.         ll x=q.size();
  213.  
  214.         for(ll i=0;i<x;i++)
  215.         {
  216.            bst* cur=q.front();
  217.  
  218.            q.pop();
  219.  
  220.            if(cur->left==NULL&&cur->right==NULL)
  221.              return level;
  222.  
  223.            if(cur->left!=NULL)
  224.              q.push(cur->left);
  225.            if(cur->right!=NULL)
  226.              q.push(cur->right);
  227.         }
  228.  
  229.  
  230.     }
  231.  
  232. }
  233.  
  234. struct bst* find_node(ll x)
  235. {
  236.     struct bst* cur=root;
  237.  
  238.     while(1)
  239.     {
  240.         if(cur==NULL)
  241.             break;
  242.  
  243.         if(x==cur->data)
  244.             return cur;
  245.         if(x>cur->data)
  246.                 cur=cur->right;
  247.         else if(x<cur->data)
  248.                 cur=cur->left;
  249.     }
  250.  
  251.     return NULL;
  252.  
  253. };
  254.  
  255.  
  256. void print_ancestor(ll x)
  257. {
  258.     struct bst* cur=find_node(x);
  259.  
  260.     if(x==NULL)
  261.         return;
  262.  
  263.     while(1)
  264.     {
  265.         cout<<cur->data<<" ";
  266.         if(cur->parent==NULL)
  267.             return;
  268.         cur=cur->parent;
  269.     }
  270.  
  271. }
  272.  
  273.  
  274.  
  275. ll count_all_smaller_node(ll x)
  276. {
  277.     ll c=0;
  278.  
  279.     struct bst* temp=find_node(x); //log(height)
  280.  
  281.     if(temp==NULL) //if node not found;
  282.         return c;
  283.  
  284.     c+=temp->left_subtree_size;
  285.  
  286.     temp=temp->parent;
  287.  
  288.     if(temp==NULL)
  289.         return c;
  290.  
  291.     while(1)
  292.     {
  293.          if(x>temp->data)
  294.             c+=(temp->left_subtree_size+1);  // +1 for the parent node!
  295.  
  296.          temp=temp->parent;                  //log(height)
  297.  
  298.          if(temp==NULL)
  299.              return c;
  300.     }
  301.  
  302. }
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311. int main()
  312. {
  313.     insert_bst(50);
  314.  
  315.     insert_bst(70);
  316.  
  317.     insert_bst(80);
  318.  
  319.     insert_bst(75);
  320.  
  321.     insert_bst(85);
  322.  
  323.     insert_bst(40);
  324.  
  325.     insert_bst(45);
  326.  
  327.  
  328.  
  329.     cout<<search(root,40)<<endl;
  330.  
  331.     cout<<"inorder: ";
  332.     inorder(root);
  333.  
  334.     cout<<endl;
  335.  
  336.     cout<<"preorder: ";
  337.  
  338.     preorder(root);
  339.  
  340.     cout<<endl;
  341.  
  342.     cout<<"Postorder: ";
  343.  
  344.     postorder(root);
  345.  
  346.     cout<<endl;
  347.  
  348.  
  349.     cout<<tree_max(root)<<endl;
  350.  
  351.     cout<<tree_min(root)<<endl;
  352.  
  353.     cout<<find_height(root);
  354.    
  355.    
  356.  
  357. }
  358.  
  359.  
  360.  
  361.  
  362.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement