Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "tree.h"
  4.  
  5. int main()
  6. {
  7.     struct player arian;
  8.     arian.name = "arian";
  9.     insert_player(&arian);
  10.     if(&arian == search_player("arian"))
  11.     {
  12.         printf("%s", "yes they are equal");
  13.     }
  14.     destroy();
  15.     return 0;
  16. }
  17.  
  18.  
  19.  
  20. #ifndef TREE_H_INCLUDED
  21. #define TREE_H_INCLUDED
  22. typedef long int name_t;
  23. typedef struct player
  24. {
  25.    char* name;
  26. } *player_t;
  27. typedef struct node node;
  28. struct node
  29. {
  30.     void*  key;
  31.     player_t player;
  32.     node* left;
  33.     node* right;
  34. };
  35.  
  36.  
  37.  
  38. void destroy();
  39. void insert_player(player_t player);
  40. void insert(void* key, player_t player);
  41. long name_to_long(const char* s);
  42. node* search(void* key);
  43. player_t search_player(char* name);
  44. void _destroy(node * leaf);
  45. void _insert(void* key, node * leaf);
  46. node* _search(void* key, node * leaf);
  47.  
  48. extern node*      root;
  49. #endif // TREE_H_INCLUDED
  50.  
  51.  
  52.  
  53.  
  54. #include "tree.h"
  55. #include <stdlib.h>
  56. #include <stdio.h>
  57. #include <string.h>
  58. node* root = NULL;
  59. long name_to_long(const char* s)
  60. {
  61.     long l = 0L;
  62.     int i;
  63.     for(i = 0; i < strlen(s) && i < 12; ++i)
  64.     {
  65.         char c = s[i];
  66.         l *= 37L;
  67.         if(c >= 'A' && c <= 'Z')
  68.         {
  69.             l += (1+c) - 65;
  70.         }
  71.         else if(c>= 'a' && c <= 'z')
  72.         {
  73.             l += (1 + c) - 97;
  74.         }
  75.         else if(c >= '0' && c <= '9')
  76.         {
  77.             l += (27 + c) - 48;
  78.         }
  79.         while(l % 37L == 0L && l != 0L)
  80.         {
  81.             l /= 37L;
  82.         }
  83.     }
  84.     return l;
  85. }
  86.  
  87. void destroy()
  88. {
  89.     _destroy(root);
  90. }
  91. void insert(void* key, player_t player)
  92. {
  93.     if(root != NULL)
  94.     {
  95.         _insert(key, root);
  96.     }
  97.     else
  98.     {
  99.         root = malloc(sizeof(node));
  100.         root->key = key;
  101.         root->player = player;
  102.         root->left = NULL;
  103.         root->right = NULL;
  104.     }
  105. }
  106.  
  107. node* search(void* key)
  108. {
  109.     return _search(key, root);
  110. }
  111.  
  112. player_t search_player(char* name)
  113. {
  114.     return _search((void*)name_to_long(name), root)->player;
  115. }
  116.  
  117. void insert_player(player_t player)
  118. {
  119.     insert((void*)name_to_long(player->name), player);
  120. }
  121.  
  122. void _destroy(node * leaf)
  123. {
  124.     if(leaf != NULL)
  125.     {
  126.         _destroy(leaf->left);
  127.         _destroy(leaf->right);
  128.         free(leaf);
  129.     }
  130.  
  131. }
  132. void _insert(void* key, node * leaf)
  133. {
  134.     if(key < leaf->key)
  135.     {
  136.         if(leaf->left != NULL)
  137.         {
  138.             _insert(key, leaf->left);
  139.         }
  140.         else
  141.         {
  142.             leaf->left = malloc(sizeof(node));
  143.             leaf->left->key = key;
  144.             leaf->left->left = NULL;
  145.             leaf->left->right = NULL;
  146.         }
  147.     }
  148.     else if(key >= leaf->key)
  149.     {
  150.         if(leaf->right != NULL)
  151.         {
  152.             _insert(key, leaf->right);
  153.         }
  154.         else
  155.         {
  156.             leaf->right = malloc(sizeof(node));
  157.             leaf->right->key = key;
  158.             leaf->right->left = NULL;
  159.             leaf->right->right = NULL;
  160.         }
  161.     }
  162. }
  163. node* _search(void* key, node * leaf)
  164. {
  165.     if(leaf != NULL)
  166.     {
  167.         if(key == leaf->key)
  168.         {
  169.             return leaf;
  170.         }
  171.         if(key < leaf->key)
  172.         {
  173.             return _search(key, leaf->left);
  174.         }
  175.         else
  176.         {
  177.             return _search(key, leaf->right);
  178.         }
  179.     }
  180.     else
  181.     {
  182.         return NULL;
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement