Advertisement
Vladpepe

Untitled

Oct 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.78 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. struct Node *GetNewNode(int data) {
  13.  
  14.     struct Node* newNode = malloc(sizeof(struct Node));
  15.     newNode->data = data;
  16.     newNode->left = NULL;
  17.     newNode->right = NULL;
  18.     return newNode;
  19. }
  20.  
  21.  
  22. struct Node* Insert(struct Node *root, int data) {
  23.     if (root == NULL) {
  24.         root = GetNewNode(data);
  25.         return root;
  26.     }
  27.     else if (data <= root->data) {
  28.         root->left = Insert(root->left, data);
  29.     }
  30.     else {
  31.         root->right = Insert(root->right, data);
  32.     }
  33.     return root;
  34.  
  35. }
  36.  
  37. bool Search(struct Node* root, int data) {
  38.     if (root == NULL)return false;
  39.     if (root->data == data)return true;
  40.     else if (data <= root->data)return Search(root->left, data);
  41.     else return Search(root->right, data);
  42. }
  43.  
  44. int FindMin(struct Node* root) {
  45.     if (root == NULL) {
  46.         printf("ERROR, THE TREE IS EMPTY");
  47.         return -1;
  48.     }
  49.     if (root->left == NULL) {
  50.         return root->data;
  51.     }
  52.  
  53.     return FindMin(root->left);
  54. }
  55.  
  56. int FindMax(struct Node* root) {
  57.     if (root == NULL) {
  58.         printf("ERROR;THE TREE IS EMPTY");
  59.         return -1;
  60.     }
  61.     if (root->right == NULL) {
  62.         return root->data;
  63.     }
  64.  
  65.     return FindMax(root->right);
  66. }
  67. int main() {
  68.     struct  Node *root = NULL;
  69.     root = Insert(root, 15);
  70.     root = Insert(root, 10);
  71.     root = Insert(root, 20);
  72.     root = Insert(root, 25);
  73.     root = Insert(root, 8);
  74.     root = Insert(root, 12);
  75.  
  76.     int number;
  77.     printf("Enter a number \n");
  78.     scanf("%d", &number);
  79.     if (Search(root, number) == true) {
  80.         printf("found it \n");
  81.     }
  82.     else {
  83.         printf("not found \n");
  84.    
  85.     }
  86.  
  87.     int Minimo = FindMin(root);
  88.     printf("The Min of the tree is %d \n", Minimo);
  89.     int Maximo = FindMax(root);
  90.     printf("The Max of the tree is %d \n", Maximo);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement