Guest User

Untitled

a guest
Jan 26th, 2023
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <stdint.h>
  5. #include <memory.h>
  6.  
  7. int comp(const int32_t *i, const int32_t *j) {
  8.     return *i - *j;
  9. }
  10.  
  11. struct TreeNode {
  12.     void *val;
  13.     struct TreeNode *left;
  14.     struct TreeNode *right;
  15. };
  16.  
  17. struct TreeNode *Insert(struct TreeNode *root, void *val,
  18.                         int (*compare)(const void *, const void *)) {
  19.     if (root == 0) {
  20.         root = (struct TreeNode *) malloc(sizeof(struct TreeNode));
  21.         root->val = val;
  22.         root->left = 0;
  23.         root->right = 0;
  24.         return root;
  25.     }
  26.  
  27.     if ((*compare)((void *) val, (void *) root->val) <= 0) {
  28.         root->left = Insert(root->left, val, compare);
  29.     } else if ((*compare)((void *) val, (void *) root->val) > 0) {
  30.         root->right = Insert(root->right, val, compare);
  31.     }
  32.  
  33.     return root;
  34. }
  35.  
  36. void Write_Inorder(struct TreeNode *root,
  37.                    void *target,
  38.                    size_t num,
  39.                    size_t size,
  40.                    int *ptr) {
  41.     if (root != 0) {
  42.         Write_Inorder(root->left, target, num, size, ptr);
  43.  
  44.         memcpy((char *) target + (size * (*ptr)++), root->val, size);
  45.  
  46.         Write_Inorder(root->right, target, num, size, ptr);
  47.     }
  48. }
  49.  
  50. void Destroy(struct TreeNode *root) {
  51.     if (root != 0) {
  52.         Destroy(root->left);
  53.         Destroy(root->right);
  54.         free(root);
  55.     }
  56. }
  57.  
  58. int32_t Get_Random_In_Range(/* in */ int32_t lower, /* in */ int32_t upper) {
  59.     return lower + rand() % (upper - lower + 1);;
  60. }
  61.  
  62. int16_t TreeSort( /* in */ void *base,
  63.         /* in */ size_t num,
  64.         /* in */ size_t size,
  65.         /* in */ int (*compare)(const void *, const void *)) {
  66.     struct TreeNode *bst = 0;
  67.     int ptr = 0;
  68.     int i = 0;
  69.  
  70.     if (base == 0 || compare == 0) {
  71.         return -1;
  72.     }
  73.  
  74.     for (i = 0; i < num; ++i) {
  75.         bst = Insert(bst, (char *) base + (size * i), compare);
  76.     }
  77.  
  78.     Write_Inorder(bst, base, num, size, &ptr);
  79.  
  80.     Destroy(bst);
  81.  
  82.     return 0;
  83. }
  84.  
  85. int main(void) {
  86.     int32_t *arr = 0;
  87.     uint32_t i = 0;
  88.  
  89.     srand(time(NULL));
  90.  
  91.     arr = (int32_t *) malloc(sizeof(int32_t) * 12);
  92.  
  93.     for (i = 0; i < 12; ++i) {
  94.         ((int32_t *) arr)[i] = Get_Random_In_Range(-300, 300);
  95.     }
  96.  
  97.     for (i = 0; i < 12; ++i) {
  98.         printf("%5i ", ((int32_t *) arr)[i]);
  99.     }
  100.     printf("\n");
  101.  
  102.     TreeSort(arr, 12, sizeof(int32_t), (int (*)(const void *, const void *)) (comp));
  103.  
  104.     for (i = 0; i < 12; ++i) {
  105.         printf("%5i ", ((int32_t *) arr)[i]);
  106.     }
  107.  
  108.     return 0;
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment