Advertisement
mbah_bejo

tugas kelas

Apr 18th, 2020
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.03 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. typedef struct AVLNode_t
  7. {
  8.     int data;
  9.     struct AVLNode_t *left,*right;
  10.     int height;
  11. }AVLNode;
  12.  
  13. typedef struct AVL_t
  14. {
  15.     AVLNode *_root;
  16.     unsigned int _size;
  17. }AVL;
  18.  
  19.  
  20. AVLNode* _avl_createNode(int value) {
  21.     AVLNode *newNode = (AVLNode*) malloc(sizeof(AVLNode));
  22.     newNode->data = value;
  23.     newNode->height=1;
  24.     newNode->left = newNode->right = NULL;
  25.     return newNode;
  26. }
  27.  
  28. AVLNode* _search(AVLNode *root, int value) {
  29.     while (root != NULL) {
  30.         if (value < root->data)
  31.             root = root->left;
  32.         else if (value > root->data)
  33.             root = root->right;
  34.         else
  35.             return root;
  36.     }
  37.     return root;
  38. }
  39.  
  40. int _getHeight(AVLNode* node){
  41.     if(node==NULL)
  42.         return 0;
  43.     return node->height;
  44. }
  45.  
  46. int _max(int a,int b){
  47.     return (a > b)? a : b;
  48. }
  49.  
  50. AVLNode* _rightRotate(AVLNode* pivotNode){
  51.  
  52.     AVLNode* newParrent=pivotNode->left;
  53.     pivotNode->left=newParrent->right;
  54.     newParrent->right=pivotNode;
  55.  
  56.     pivotNode->height=_max(_getHeight(pivotNode->left),
  57.                       _getHeight(pivotNode->right))+1;
  58.     newParrent->height=_max(_getHeight(newParrent->left),
  59.                        _getHeight(newParrent->right))+1;
  60.    
  61.     return newParrent;
  62. }
  63.  
  64. AVLNode* _leftRotate(AVLNode* pivotNode) {
  65.  
  66.     AVLNode* newParrent=pivotNode->right;
  67.     pivotNode->right=newParrent->left;
  68.     newParrent->left=pivotNode;
  69.  
  70.     pivotNode->height=_max(_getHeight(pivotNode->left),
  71.                       _getHeight(pivotNode->right))+1;
  72.     newParrent->height=_max(_getHeight(newParrent->left),
  73.                        _getHeight(newParrent->right))+1;
  74.    
  75.     return newParrent;
  76. }
  77.  
  78. AVLNode* _leftCaseRotate(AVLNode* node){
  79.     return _rightRotate(node);
  80. }
  81.  
  82. AVLNode* _rightCaseRotate(AVLNode* node){
  83.     return _leftRotate(node);
  84. }
  85.  
  86. AVLNode* _leftRightCaseRotate(AVLNode* node){
  87.     node->left=_leftRotate(node->left);
  88.     return _rightRotate(node);
  89. }
  90.  
  91. AVLNode* _rightLeftCaseRotate(AVLNode* node){
  92.     node->right=_rightRotate(node->right);
  93.     return _leftRotate(node);
  94. }
  95.  
  96. int _getBalanceFactor(AVLNode* node){
  97.     if(node==NULL)
  98.         return 0;
  99.     return _getHeight(node->left)-_getHeight(node->right);
  100. }
  101.  
  102. AVLNode* _insert_AVL(AVL *avl,AVLNode* node,int value){
  103.    
  104.     if(node==NULL)
  105.         return _avl_createNode(value);
  106.     if(value < node->data)
  107.         node->left = _insert_AVL(avl,node->left,value);
  108.     else if(value > node->data)
  109.         node->right = _insert_AVL(avl,node->right,value);
  110.    
  111.     node->height= 1 + _max(_getHeight(node->left),_getHeight(node->right));
  112.  
  113.     int balanceFactor=_getBalanceFactor(node);
  114.    
  115.     if(balanceFactor > 1 && value < node->left->data)
  116.         return _leftCaseRotate(node);
  117.     if(balanceFactor > 1 && value > node->left->data)
  118.         return _leftRightCaseRotate(node);
  119.     if(balanceFactor < -1 && value > node->right->data)
  120.         return _rightCaseRotate(node);
  121.     if(balanceFactor < -1 && value < node->right->data)
  122.         return _rightLeftCaseRotate(node);
  123.    
  124.     return node;
  125. }
  126.  
  127. AVLNode* _findMinNode(AVLNode *node) {
  128.     AVLNode *currNode = node;
  129.     while (currNode && currNode->left != NULL)
  130.         currNode = currNode->left;
  131.     return currNode;
  132. }
  133.  
  134. AVLNode* _remove_AVL(AVLNode* node,int value){
  135.     if(node==NULL)
  136.         return node;
  137.     if(value > node->data)
  138.         node->right=_remove_AVL(node->right,value);
  139.     else if(value < node->data)
  140.         node->left=_remove_AVL(node->left,value);
  141.     else{
  142.         AVLNode *temp;
  143.         if((node->left==NULL)||(node->right==NULL)){
  144.             temp=NULL;
  145.             if(node->left==NULL) temp=node->right;  
  146.             else if(node->right==NULL) temp=node->left;
  147.            
  148.             if(temp==NULL){
  149.                 temp=node;
  150.                 node=NULL;
  151.             }
  152.             else
  153.                 *node=*temp;  
  154.            
  155.             free(temp);
  156.         }
  157.         else{
  158.             temp = _findMinNode(node->right);
  159.             node->data=temp->data;
  160.             node->right=_remove_AVL(node->right,temp->data);
  161.         }    
  162.     }
  163.  
  164.     if(node==NULL) return node;
  165.    
  166.     node->height=_max(_getHeight(node->left),_getHeight(node->right))+1;
  167.  
  168.     int balanceFactor= _getBalanceFactor(node);
  169.    
  170.     if(balanceFactor>1 && _getBalanceFactor(node->left)>=0)
  171.         return _leftCaseRotate(node);
  172.  
  173.     if(balanceFactor>1 && _getBalanceFactor(node->left)<0)
  174.         return _leftRightCaseRotate(node);
  175.  
  176.     if(balanceFactor<-1 && _getBalanceFactor(node->right)<=0)
  177.         return _rightCaseRotate(node);
  178.  
  179.     if(balanceFactor<-1 && _getBalanceFactor(node->right)>0)
  180.         return _rightLeftCaseRotate(node);
  181.    
  182.     return node;
  183. }
  184.  
  185. void avl_init(AVL *avl) {
  186.     avl->_root = NULL;
  187.     avl->_size = 0u;
  188. }
  189.  
  190. bool avl_isEmpty(AVL *avl) {
  191.     return avl->_root == NULL;
  192. }
  193.  
  194. bool avl_find(AVL *avl, int value) {
  195.     AVLNode *temp = _search(avl->_root, value);
  196.     if (temp == NULL)
  197.         return false;
  198.    
  199.     if (temp->data == value)
  200.         return true;
  201.     else
  202.         return false;
  203. }
  204.  
  205. void avl_insert(AVL *avl,int value){
  206.     if(!avl_find(avl,value)){
  207.         avl->_root=_insert_AVL(avl,avl->_root,value);
  208.         avl->_size++;
  209.     }
  210.  
  211. }
  212.  
  213. void avl_remove(AVL *avl,int value){
  214.     if(avl_find(avl,value)){
  215.         avl->_root=_remove_AVL(avl->_root,value);
  216.         avl->_size--;
  217.     }
  218.  
  219. }
  220.  
  221. void preorder(AVLNode *root) {
  222.     if (root) {
  223.         preorder(root->left);
  224.         printf("%d ", root->data);
  225.         preorder(root->right);
  226.     }
  227. }
  228. int max, min, mid;
  229.  
  230. void tambahPanjang(AVLNode* root, int maximum, int minimum, int posisi)
  231. {
  232.     if (root == NULL)
  233.         return;
  234.  
  235.     tambahPanjang(root->left, maximum, minimum,  posisi - 1);
  236.  
  237.     if (minimum >  posisi) min = posisi;
  238.     if (maximum <  posisi) max = posisi;
  239.  
  240.     tambahPanjang(root->right, maximum, minimum,   posisi + 1);
  241.  
  242. }
  243.  
  244. int sumver[]={0};
  245.  
  246. void TotVertikal(AVLNode *tree, int posisi)
  247. {
  248.  
  249.     if (tree == NULL) {
  250.         return;
  251.     }
  252.  
  253.    
  254.     sumver[posisi] += tree->data;
  255.  
  256.     if (tree->left != NULL) {
  257.         TotVertikal(tree->left, posisi - 1);
  258.     }
  259.     if (tree->right != NULL) {
  260.         TotVertikal(tree->right, posisi + 1);
  261.     }
  262. }
  263. int main(){
  264.         AVL avlku;
  265.     avl_init(&avlku);
  266.     char com[10];
  267.    long long Q,i,num,j;
  268.     scanf("%lld",&Q);
  269.     for(i=0;i<Q;i++)
  270.     {
  271.         scanf("%s",&com);
  272.         if(strcmp(com,"insert")==0)
  273.     {
  274.         scanf("%lld",&num);
  275.         avl_insert(&avlku,num);
  276.     }
  277.         else
  278.         {
  279.             min =0;
  280.             max =0;
  281.             tambahPanjang(avlku._root,max, min,0);
  282.             mid = abs(min) + max +1;
  283.             printf("%d_", mid);
  284.             sumver[mid]={0};
  285.             if(abs(min)<max) mid=(mid+1)/2;
  286.             else if(abs(min)max)  mid/=2;
  287.             printf("%d_", mid);
  288.  
  289.             TotVertikal(avlku._root,mid);
  290.             j=0;
  291.            
  292.             while(sumver[j]!=0)
  293.             {
  294.                 printf("%lld ", sumver[j]);
  295.                 j++;
  296.             } printf("\n");
  297.     }
  298.        
  299.     }
  300.     return 0;
  301.  
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement