Advertisement
Dmaxiya

Binary search tree

Feb 21st, 2025
285
0
364 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4.  
  5. inline int maxInt(int a, int b) {
  6.     return a > b ? a : b;
  7. }
  8.  
  9. typedef struct Node {
  10.     int data;
  11.     struct Node *left;
  12.     struct Node *right;
  13. } Node;
  14.  
  15. Node* newNode(int value) {
  16.     Node *node = (Node*) malloc(sizeof(Node));
  17.     node->data = value;
  18.     node->left = NULL;
  19.     node->right = NULL;
  20.     return node;
  21. }
  22.  
  23. typedef struct {
  24.     Node *root;
  25. } Tree;
  26.  
  27. Tree* newTree() {
  28.     Tree *tree = (Tree*) malloc(sizeof(Tree));
  29.     tree->root = NULL;
  30.     return tree;
  31. }
  32.  
  33. void nodePreOrder(Node *root) {
  34.     if (root == NULL) {
  35.         return ;
  36.     }
  37.     printf("%d ", root->data);
  38.     nodePreOrder(root->left);
  39.     nodePreOrder(root->right);
  40. }
  41.  
  42. void treePreOrder(Tree *tree) {
  43.     if (tree == NULL) {
  44.         return ;
  45.     }
  46.     nodePreOrder(tree->root);
  47. }
  48.  
  49. void nodeInOrder(Node *root) {
  50.     if (root == NULL) {
  51.         return ;
  52.     }
  53.     nodeInOrder(root->left);
  54.     printf("%d ", root->data);
  55.     nodeInOrder(root->right);
  56. }
  57.  
  58. void treeInOrder(Tree *tree) {
  59.     if (tree == NULL) {
  60.         return ;
  61.     }
  62.     nodeInOrder(tree->root);
  63. }
  64.  
  65. void nodePostOrder(Node *root) {
  66.     if (root == NULL) {
  67.         return ;
  68.     }
  69.     nodePostOrder(root->left);
  70.     nodePostOrder(root->right);
  71.     printf("%d ", root->data);
  72. }
  73.  
  74. void treePostOrder(Tree *tree) {
  75.     if (tree == NULL) {
  76.         return ;
  77.     }
  78.     nodePostOrder(tree->root);
  79. }
  80.  
  81. void nodeInsert(Node *root, int value) {
  82.     if (value < root->data) {
  83.         if (root->left == NULL) {
  84.             root->left = newNode(value);
  85.             return ;
  86.         }
  87.         nodeInsert(root->left, value);
  88.         return ;
  89.     }
  90.  
  91.     if (root->right == NULL) {
  92.         root->right = newNode(value);
  93.         return ;
  94.     }
  95.     nodeInsert(root->right, value);
  96. }
  97.  
  98. void treeInsert(Tree *tree, int value) {
  99.     if (tree->root == NULL) {
  100.         tree->root = newNode(value);
  101.         return ;
  102.     }
  103.     nodeInsert(tree->root, value);
  104. }
  105.  
  106. void treeBuild(Tree *tree, int *values, int n) {
  107.     int i;
  108.     for (i = 0; i < n; ++i) {
  109.         treeInsert(tree, values[i]);
  110.     }
  111. }
  112.  
  113. int nodeGetHeight(Node *root) {
  114.     if (root == NULL) {
  115.         return 0;
  116.     }
  117.     return maxInt(nodeGetHeight(root->left), nodeGetHeight(root->right)) + 1;
  118. }
  119.  
  120. int treeGetHeight(Tree *tree) {
  121.     if (tree == NULL) {
  122.         return 0;
  123.     }
  124.     return nodeGetHeight(tree->root);
  125. }
  126.  
  127. int nodeMaximum(Node *root) {
  128.     if (root == NULL) {
  129.         return INT_MIN;
  130.     }
  131.     if (root->right == NULL) {
  132.         return root->data;
  133.     }
  134.     return nodeMaximum(root->right);
  135. }
  136.  
  137. int treeMaximum(Tree *tree) {
  138.     if (tree == NULL) {
  139.         return INT_MIN;
  140.     }
  141.     return nodeMaximum(tree->root);
  142. }
  143.  
  144. int main() {
  145.     int arr[10] = {5, 3, 7, 2, 8, 10, 9, 1, 4, 6};
  146.     Tree *tree = newTree();
  147.     treeBuild(tree, arr, 10);
  148.     treePreOrder(tree);
  149.     printf("\n");
  150.     treeInOrder(tree);
  151.     printf("\n");
  152.     treePostOrder(tree);
  153.     printf("\n");
  154.     printf("Height = %d\n", treeGetHeight(tree));
  155.     printf("MAX = %d\n", treeMaximum(tree));
  156.  
  157.     return 0;
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement