Advertisement
wacka_facka

Untitled

Jun 21st, 2022
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.70 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<malloc.h>
  4. #include<inttypes.h>
  5.  
  6. struct node {
  7.     int *value1, *value2;
  8.     struct node *left_child;
  9.     struct node *right_child;
  10.     struct node *middle_child;
  11. };
  12.  
  13. struct node *new_node(int value1) {
  14.     struct node *tmp = (struct node *)malloc(sizeof(*tmp);
  15.     tmp->value1 = value1;
  16.     tmp->value2 = NULL;
  17.     tmp->left_child = tmp->right_child = tmp->middle_child = NULL;
  18.     return tmp;
  19. }
  20.  
  21. struct node* insert_node(struct node* node, int value) {
  22.  
  23.     if ((left_child != NULL) & (right_child != NULL)) {
  24.         if (value1 < *node->value1) {
  25.             node->left_child = insert_node(node->left_child, value1);
  26.             node->value1 = malloc(sizeof(*node->value1));
  27.         }
  28.         if (value2 > *node->value2) {
  29.             node->right_child = insert_node(node->right_child, value2);
  30.             node->value2 = malloc(sizeof(*node->value2));
  31.         }
  32.         if ((value1 >= *node->value1) & (value2 <= *node->value2)) {
  33.             if ((value > *node->value1) & (value < *node->value2)) {
  34.                 node->middle_child = insert_node(node->middle_child, value);
  35.                 node->value = malloc(sizeof(*node->value));
  36.             }
  37.         }
  38.     }
  39.     return node;
  40. }
  41.  
  42. struct node* hmk(struct node* node, int value) {  // hmk - how many kids
  43.     uint8_t nbr_ch = !(uintptr_t)node->left_child + !(uintptr_t)node->middle_child + !(uintptr_t)node->right_child)
  44.     if (nbr_ch < 2) {
  45.         if (node->left_child && *node->value1 < value && *node->left_child->value1 > value) {
  46.             int buff = *node->value1;
  47.             *node->value1 = value;
  48.             value = buff;
  49.         }
  50.         new_node(int value);
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement