Advertisement
mbah_bejo

SDITSBST SPOJ

Mar 11th, 2020
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.90 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4.  
  5. typedef struct bstnode_t {
  6.     long long key,pos;
  7.     struct bstnode_t \
  8.         *left, *right;
  9. } BSTNode;
  10.  
  11. typedef struct bst_t {
  12.     BSTNode *_root;
  13.     unsigned int _size;
  14. } BST;
  15.  
  16.  
  17. BSTNode* __bst__createNode(long long value) {
  18.     BSTNode *newNode = (BSTNode*) malloc(sizeof(BSTNode));
  19.     newNode->key = value;
  20.     newNode->pos= 0;
  21.     newNode->left = newNode->right = NULL;
  22.     return newNode;
  23. }
  24.  
  25. //long long __bst__position(BSTNode *root)
  26. //
  27. //{
  28. //  if(root == NULL) return 0;
  29. //  else
  30. //  return (__bst__position(root->left) + 1 + __bst__position(root->right));
  31. //}
  32. BSTNode* __bst__insert(BSTNode *root, long long value) {
  33.     BSTNode *temp = NULL;
  34.     if (root == NULL)
  35.     {
  36.         temp = __bst__createNode(value);
  37.     //  temp->pos = __bst__position(temp);
  38.         return temp;   
  39.     }
  40.  
  41.     if (value < root->key)
  42.     {
  43.         root->left = __bst__insert(root->left, value);
  44.     }
  45.     else if (value > root->key)
  46.     {
  47.         root->pos++;
  48.         root->right = __bst__insert(root->right, value);
  49.     }
  50.    
  51.     return root;
  52. }
  53.  
  54. BSTNode* __bst__search(BSTNode *root, int value) {
  55.     while (root != NULL) {
  56.         if (value < root->key)
  57.             root = root->left;
  58.         else if (value > root->key)
  59.             root = root->right;
  60.         else
  61.             return root;
  62.     }
  63.     return root;
  64. }
  65.  
  66. /**
  67.  * PRIMARY FUNCTION
  68.  * ---------------------------
  69.  * Accessible and safe to use.
  70.  */
  71.  
  72.  
  73. void bst_init(BST *bst) {
  74.     bst->_root = NULL;
  75.     bst->_size = 0u;
  76. }
  77.  
  78. bool bst_isEmpty(BST *bst) {
  79.     return bst->_root == NULL;
  80. }
  81.  
  82. long long bst_post(BSTNode *bst, long long value) {
  83.     BSTNode *temp = bst;
  84.     if (temp == NULL)
  85.         return -1;
  86.     if( temp->key > value)
  87.     {
  88.         long long index = bst_post(temp->left, value);
  89.         if(index == -1) return -1;
  90.         else return index + temp->pos +1;
  91.     } else if (temp->key<value)
  92.     {
  93.         return bst_post(temp->right, value);
  94.     }
  95.     else
  96.         return temp->pos;
  97. }
  98. bool bst_find(BST *bst, int value) {
  99.     BSTNode *temp = __bst__search(bst->_root, value);
  100.     if (temp == NULL)
  101.         return false;
  102.    
  103.     if (temp->key == value)
  104.         return true;
  105.     else
  106.         return false;
  107. }
  108. void bst_insert(BST *bst, long long value) {
  109.     if (!bst_find(bst, value)) {
  110.         bst->_root = __bst__insert(bst->_root, value);
  111.         bst->_size++;
  112.     }
  113. }
  114.  
  115. int main()
  116. {
  117.     BST set;
  118.     bst_init(&set);
  119.    
  120.     int i,j;
  121.     long long com, num, tanda=0;
  122.     scanf("%d",&i);
  123.     for(j=0;j<i;j++)
  124.     {
  125.         scanf("%lld",&com);
  126.         if(com==1)
  127.         {
  128.             scanf("%lld",&num);
  129.             bst_insert(&set,num);
  130.             //printf("masuk %lld\n", set._root->pos);
  131.         } else
  132.         {
  133.             scanf("%lld",&num);
  134.             tanda  = bst_post(set._root,num) ;
  135.             if(tanda == -1) printf("Data tidak ada\n");
  136.             else printf("%lld\n", tanda+1);
  137.            
  138.         }
  139. } return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement