Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. /* TODO: split into files
  6.     RedBlackTree.c & .h
  7.     RedBlackTree Node.c & .h
  8.     main.c
  9. */
  10.  
  11. #ifndef NULL
  12. #define NULL 0
  13. #endif
  14.  
  15. typedef enum COLOR_ {BLACK, RED} COLOR;
  16.  
  17. typedef struct RedBlackTreeNode {
  18.     COLOR color;
  19.     int value;
  20.     struct RedBlackTreeNode *parent, *left, *right;
  21.    
  22. } RBTNode;
  23.  
  24. typedef struct RedBlackTree {
  25.     RBTNode *root;
  26.     RBTNode *median1, *median2;
  27.     bool parity;
  28.    
  29.     // functions pointers
  30.     void (*insert)(struct RedBlackTree *self, RBTNode *node);
  31.     void (*print_median)(struct RedBlackTree *self);
  32.     RBTNode* (*get_successor)(struct RedBlackTree *self, RBTNode *node);
  33.     RBTNode* (*get_predecessor)(struct RedBlackTree *self, RBTNode *node);
  34. } RBT;
  35.  
  36. RBTNode* new_node(int value) {
  37.     // should be freed after usage
  38.  
  39.     RBTNode* node = (RBTNode *)malloc(sizeof(RBTNode));
  40.     node->value = value;
  41.     node->color = BLACK;
  42.     node->parent = node->left = node->right = NULL;
  43.     return node;
  44. }
  45.  
  46. void free_node(RBTNode *node) {
  47.     free(node);
  48.     node = NULL;
  49. }
  50.  
  51. void in_order_traversal(RBTNode *node, void (*callback_function)(RBTNode *node)) {
  52.     // scans the tree in O(n)
  53.     // execute callback function on each node
  54.     if (node == NULL)
  55.         return;
  56.     in_order_traversal(node->left, callback_function);
  57.     callback_function(node);
  58.     in_order_traversal(node->right, callback_function);
  59. }
  60.  
  61. void RBT_print_median(RBT* self) {
  62.     RBTNode* median;
  63.     if (self->parity == 0)
  64.         median = self->median1;
  65.     else
  66.         median = self->median1 < self->median2 ? self->median1 : self->median2;
  67.    
  68.     printf("The median is: %d\n", median->value);
  69. }
  70.  
  71. RBT* RedBlackTreeConstruct() {
  72.     // creates a tree and initiallize the struct's function
  73.     // should be freed after_usage
  74.    
  75.     RBT *rbt = (RBT *)malloc(sizeof(RBT));
  76.     rbt->root = NULL;
  77.     rbt->parity = 0;
  78.    
  79.     // TODO: change to actual function
  80.     rbt->insert = NULL;
  81.     rbt->print_median = RBT_print_median;
  82.     rbt->get_successor = NULL;
  83.     rbt->get_predecessor = NULL;
  84.     return rbt;
  85. }
  86.  
  87. void RedBlackTreeDestruct(RBT* tree) {
  88.     // frees all elements
  89.     in_order_traversal(tree->root, free_node);
  90.     free(tree);
  91.     tree = NULL;
  92. }
  93.  
  94. void fill_array_with_random(int *arr, int length) {
  95.     for (int i = 0; i < length; i++)
  96.         arr[i] = rand() % 1024;
  97. }
  98.  
  99. void the_algorithm(int *arr, int n1, int n2, int n3) {
  100.    
  101.     RBT *tree = RedBlackTreeConstruct();
  102.     for (int i = 0; i < n1; i++)
  103.         tree->insert(tree, new_node(arr[i]));
  104.     tree->print_median(tree);
  105.     for (int i = n1; i < n2; i++)
  106.         tree->insert(tree, new_node(arr[i]));
  107.     tree->print_median(tree);
  108.     for (int i = n2; i < n3; i++)
  109.         tree->insert(tree, new_node(arr[i]));
  110.     tree->print_median(tree);
  111.    
  112.     RedBlackTreeDestruct(tree);
  113. }
  114.  
  115. int main(int argc, char *argv[]) {
  116.     srand(time(NULL));
  117.    
  118.     RBTNode *node = new_node(1);   
  119.     int A[200], B[400], C[800];
  120.     int a_n1, a_n2, a_n3;
  121.     int b_n1, b_n2, b_n3;
  122.     int c_n1, c_n2, c_n3;
  123.  
  124.     a_n1 = (sizeof(A) / sizeof(*A)) / 4;
  125.     a_n2 = a_n1 * 2;
  126.     a_n3 = a_n1 * 3;
  127.  
  128.     b_n1 = (sizeof(A) / sizeof(*A)) / 4;
  129.     b_n2 = b_n1 * 2;
  130.     b_n3 = b_n1 * 3;
  131.  
  132.     c_n1 = (sizeof(A) / sizeof(*A)) / 4;
  133.     c_n2 = c_n1 * 2;
  134.     c_n3 = c_n2 * 3;
  135.    
  136.     fill_array_with_random(A, sizeof(A) / sizeof(*A));
  137.     fill_array_with_random(B, sizeof(B) / sizeof(*B));
  138.     fill_array_with_random(C, sizeof(C) / sizeof(*C));
  139.  
  140.  
  141.     the_algorithm(A, a_n1, a_n2, a_n3);
  142.     the_algorithm(B, b_n1, b_n2, b_n3);
  143.     the_algorithm(B, c_n1, c_n2, c_n3);
  144.    
  145.     free_node(node);
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement