Rummeris

bst.c

Nov 17th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include "bst.h"
  7.  
  8. ///*****Functions from bst.c of Exercise 1*****
  9.  
  10. Node* leftMost(Node* rightNode) {
  11.  
  12.     Node* temp = rightNode;
  13.  
  14.     while (temp && temp -> left != NULL)
  15.         temp = temp -> left;
  16.  
  17.     return temp;
  18. }
  19.  
  20. Node* insertNode(Node *N, int value) {
  21.  
  22.     if (N == NULL) {
  23.         N = malloc(sizeof(struct Node));
  24.         N -> data = value;
  25.         N -> left = NULL;
  26.         N -> right = NULL;
  27.     } else if (N -> data < value) {
  28.         N -> right = insertNode(N -> right, value);
  29.     } else if (N -> data > value)
  30.         N -> left = insertNode(N -> left, value);
  31.  
  32.     return N;
  33. }
  34.  
  35. Node* deleteNode(Node* N, int value) {
  36.  
  37.     if (N == NULL) return N;
  38.  
  39.     if (value > N -> data) {
  40.         N -> right = deleteNode(N -> right, value);
  41.     } else if (value < N -> data) {
  42.         N -> left = deleteNode(N -> left, value);
  43.     } else {
  44.         if (N -> left == NULL) {
  45.             Node *temp = N -> right;
  46.             free(N);
  47.             return temp;
  48.         } else if (N -> right == NULL) {
  49.             Node *temp = N -> left;
  50.             free(N);
  51.             return temp;
  52.         }
  53.  
  54.         Node* temp = leftMost(N -> right);
  55.         N -> data = temp -> data;
  56.         N -> right = deleteNode(N -> right, temp -> data);
  57.     }
  58.  
  59.     return N;
  60. }
  61.  
  62. void printSubtree(Node *N) {
  63.  
  64.     if (N != NULL) {
  65.         printSubtree(N -> left);
  66.         printf("%d\n", N -> data);
  67.         printSubtree(N -> right);
  68.     }
  69. }
  70.  
  71. int countNodes(Node *N) {
  72.  
  73.     if (N == NULL)
  74.         return 0;
  75.  
  76.     return countNodes(N -> left) + countNodes(N -> right) + 1;
  77. }
  78.  
  79. Node* freeSubtree(Node *N) {
  80.  
  81.     if (N == NULL) return N;
  82.  
  83.     if (N -> left != NULL) freeSubtree(N -> left);
  84.     if (N -> right != NULL) freeSubtree(N -> right);
  85.  
  86.     free(N);
  87.     return NULL;
  88.  
  89. }
  90.  
  91. ///********************************************
  92.  
  93. Node* makeBalance(Node *N, int arr[], int leftBorder, int rightBorder) {
  94.     if (leftBorder > rightBorder)
  95.         return NULL;
  96.  
  97.     int middle = (leftBorder + rightBorder)/2;
  98.     N = insertNode(N, arr[middle]);
  99.  
  100.     N -> left = makeBalance(N -> left, arr, leftBorder, middle-1);
  101.     N -> right = makeBalance(N -> right, arr, middle+1, rightBorder);
  102.  
  103.     return N;
  104. }
  105.  
  106. int treeArray(Node* N, int arr[], int i) {
  107.     if(N == NULL)
  108.         return i;
  109.  
  110.     if(N -> left != NULL)
  111.           i = treeArray(N -> left, arr, i);
  112.  
  113.     arr[i] = N -> data;
  114.     i++;
  115.  
  116.     if(N -> right != NULL)
  117.         i = treeArray(N -> right, arr, i);
  118.  
  119.     return i;
  120. }
  121.  
  122. int bstLength(Node* N) {
  123.     if (N != NULL)
  124.         return bstLength(N -> left) + 1 + bstLength(N -> right);
  125.  
  126.     return 0;
  127. }
  128.  
  129. int sumSubtree(Node *N) {
  130.     if (N != NULL)
  131.         return sumSubtree(N -> left) + (N -> data) + sumSubtree(N -> right);
  132.  
  133.     return 0;
  134. }
  135.  
  136. Node* balanceTree(Node* N) {
  137.     if (N == NULL)
  138.         return N;
  139.  
  140.     int size = bstLength(N);
  141.     int *arr = NULL;
  142.     arr = malloc(size * sizeof(int));
  143.  
  144.     treeArray(N, arr, 0);
  145.  
  146.     Node* nRoot = NULL;
  147.     nRoot = makeBalance(nRoot, arr, 0, size-1);
  148.     return nRoot;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment