Advertisement
Vladpepe

Untitled

Oct 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. #include "stdbool.h"
  5.  
  6. struct Node {
  7. struct Node* left;
  8. int data;
  9. struct Node* right;
  10. };
  11.  
  12. bool IsBstUtil(struct Node* root, int minValue, int maxValue) {
  13. if (root == NULL) return true;
  14.  
  15. if (root->data > minValue && root->data < maxValue
  16. && IsBstUtil(root->left, minValue, root->data)
  17. && IsBstUtil(root->right, root->data, maxValue))
  18. return true;
  19. else
  20. return false;
  21. }
  22.  
  23. bool IsBinarySearchTree(struct Node* root) { //return true if BTS
  24. return IsBstUtil(root, INT_MIN, INT_MAX); //aditional function
  25. }
  26.  
  27. struct Node *GetNewNode(int data) {
  28. struct Node* newNode = malloc(sizeof(struct Node));
  29. newNode->data = data;
  30. newNode->left = NULL;
  31. newNode->right = NULL;
  32. return newNode;
  33. }
  34.  
  35. struct Node* Insert(struct Node *root, int data) {
  36. if (root == NULL) {
  37. root = GetNewNode(data);
  38. return root;
  39. }
  40. else if (data <= root->data) {
  41. root->left = Insert(root->left, data);
  42. }
  43. else {
  44. root->right = Insert(root->right, data);
  45. }
  46. return root;
  47. }
  48.  
  49. bool Search(struct Node* root, int data) {
  50. if (root == NULL)return false;
  51. if (root->data == data)return true;
  52. else if (data <= root->data)return Search(root->left, data);
  53. else return Search(root->right, data);
  54. }
  55.  
  56. int FindMin(struct Node* root) {
  57. if (root == NULL) {
  58. printf("ERROR, THE TREE IS EMPTY");
  59. return -1;
  60. }
  61. if (root->left == NULL) {
  62. return root->data;
  63. }
  64.  
  65. return FindMin(root->left);
  66. }
  67.  
  68. int FindMax(struct Node* root) {
  69. if (root == NULL) {
  70. printf("ERROR;THE TREE IS EMPTY");
  71. return -1;
  72. }
  73. if (root->right == NULL) {
  74. return root->data;
  75. }
  76.  
  77. return FindMax(root->right);
  78. }
  79.  
  80. int height(struct Node *root) {
  81. if (root == NULL) {
  82. return -1;
  83. }
  84. int leftHeight = height(root->left);
  85. int rightHeight = height(root->right);
  86.  
  87. if (leftHeight > rightHeight) {
  88. return leftHeight + 1 ;
  89. }
  90. else {
  91. return rightHeight + 1;
  92. }
  93. }
  94.  
  95. void InOrderPrint(struct Node* root) {
  96. if (root == NULL) {
  97. return;
  98. }
  99. if (root->left == NULL && root->right == NULL) {
  100. printf("%d ", root->data);
  101. return;
  102. }
  103. if (root->left != NULL) {
  104. InOrderPrint(root->left);
  105. }
  106. printf("%d ", root->data);
  107. if (root->right != NULL) {
  108. InOrderPrint(root->right);
  109. }
  110. }
  111.  
  112. struct Node* MinAdress(struct Node* root) {
  113. if (root == NULL) {
  114. return NULL;
  115. }
  116. while (root->left != NULL) {
  117. root = root->left;
  118. }
  119. return root;
  120. }
  121.  
  122. struct Node* Delete(struct Node* root, int data) {
  123. if (root == NULL) {
  124. return NULL;
  125. }
  126. if (data < root->data) {
  127. root->left = Delete(root->left, data);
  128. }
  129. if (data > root->data) {
  130. root->right = Delete(root->right, data);
  131. }
  132. else { // i found it =D
  133. // case 1 : no child
  134. if (root->left == NULL && root->right == NULL) {
  135. free(root);
  136. root = NULL;
  137. }
  138. // Case 2 : 1 child
  139. else if (root->left != NULL && root->right == NULL) {
  140. struct Node* temp = root;
  141. root = root->left;
  142. free(temp);
  143. }
  144. else if (root->right != NULL && root->left == NULL) {
  145. struct Node* temp = root;
  146. root = root->right;
  147. free(temp);
  148. }
  149. // case 3 : 2 children
  150. else{
  151. struct Node *temp = MinAdress(root->right);
  152. root->data = temp->data;
  153. root->right = Delete(root->right, temp->data);
  154. }
  155. }
  156. return root;
  157. }
  158.  
  159. struct Node* Find(struct Node* root,int num) {
  160. if (root == NULL) return NULL;
  161. while (root->left != NULL && root->right != NULL) {
  162.  
  163. if (num< root->data) root = root->left;
  164. if (num> root->data) root = root->right;
  165. if (num == root->data) return root;
  166. }
  167. return NULL;
  168. }
  169.  
  170. struct Node* GetSuccessor(struct Node* root, int data) {
  171. // Search the node - o(h)
  172. struct Node* current = Find(root,data);
  173. if (current == NULL) return NULL;
  174. // Case 1 : Node has right subtree
  175. if (current->right != NULL) {
  176. struct Node *temp = current->right;
  177. while (temp->left != NULL) temp = temp->left;
  178. return temp;
  179. }
  180. // Case 2 : No right subtree
  181. else {
  182. struct Node* successor = NULL;
  183. struct Node* ancestor = root;
  184. while (ancestor != current) {
  185. if (current->data < ancestor->data) {
  186. successor = ancestor;
  187. ancestor = ancestor->left;
  188. }
  189. else {
  190. ancestor = ancestor->right;
  191. }
  192. }
  193. return successor;
  194. }
  195. }
  196.  
  197. int main() {
  198.  
  199. struct Node *root = NULL;
  200. root = Insert(root, 15);
  201. root = Insert(root, 10);
  202. root = Insert(root, 20);
  203. root = Insert(root, 25);
  204. root = Insert(root, 8);
  205. root = Insert(root, 12);
  206.  
  207. bool Bst = IsBinarySearchTree(root);
  208. if (Bst == false) {
  209. printf("This is not a Binary Search Tree \n \n");
  210. }
  211. else {
  212. printf("This is a Binary Search Tree \n \n");
  213. }
  214.  
  215. int number;
  216. printf("Enter a number to search \n");
  217. scanf("%d", &number);
  218. if (Search(root, number) == true) {
  219. printf("found it \n \n");
  220. }
  221. else {
  222. printf("not found \n \n");
  223. }
  224.  
  225. int Minimo = FindMin(root);
  226. printf("The Min of the tree is %d \n \n", Minimo);
  227.  
  228. int Maximo = FindMax(root);
  229. printf("The Max of the tree is %d \n \n", Maximo);
  230.  
  231. int TreeHeight = height(root);
  232. printf("the Height of the tree is %d \n \n", TreeHeight);
  233.  
  234. printf("In Order Visit of the Tree is : ");
  235. InOrderPrint(root);
  236.  
  237. int pred = 0;
  238. printf("\nChose a number and i will give you it's successor \n");
  239. scanf("%d", &pred);
  240. struct Node* successor = GetSuccessor(root, pred);
  241. printf("%d \n", successor->data);
  242.  
  243. printf(" \nDelete a number \n");
  244. int k = 0;
  245. scanf("%d", &k);
  246. root = Delete(root, k);
  247. InOrderPrint(root);
  248.  
  249.  
  250.  
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement