Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- define _CRT_SECURE_NO_WARNINGS
- #include "stdio.h"
- #include "stdlib.h"
- #include "stdbool.h"
- struct Node {
- struct Node* left;
- int data;
- struct Node* right;
- };
- struct Node *GetNewNode(int data) {
- struct Node* newNode = malloc(sizeof(struct Node));
- newNode->data = data;
- newNode->left = NULL;
- newNode->right = NULL;
- return newNode;
- }
- struct Node* Insert(struct Node *root, int data) {
- if (root == NULL) {
- root = GetNewNode(data);
- return root;
- }
- else if (data <= root->data) {
- root->left = Insert(root->left, data);
- }
- else {
- root->right = Insert(root->right, data);
- }
- return root;
- }
- bool Search(struct Node* root, int data) {
- if (root == NULL)return false;
- if (root->data == data)return true;
- else if (data <= root->data)return Search(root->left, data);
- else return Search(root->right, data);
- }
- int FindMin(struct Node* root) {
- if (root == NULL) {
- printf("ERROR, THE TREE IS EMPTY");
- return -1;
- }
- if (root->left == NULL) {
- return root->data;
- }
- return FindMin(root->left);
- }
- int FindMax(struct Node* root) {
- if (root == NULL) {
- printf("ERROR;THE TREE IS EMPTY");
- return -1;
- }
- if (root->right == NULL) {
- return root->data;
- }
- return FindMax(root->right);
- }
- int main() {
- struct Node *root = NULL;
- root = Insert(root, 15);
- root = Insert(root, 10);
- root = Insert(root, 20);
- root = Insert(root, 25);
- root = Insert(root, 8);
- root = Insert(root, 12);
- int number;
- printf("Enter a number \n");
- scanf("%d", &number);
- if (Search(root, number) == true) {
- printf("found it \n");
- }
- else {
- printf("not found \n");
- }
- int Minimo = FindMin(root);
- printf("The Min of the tree is %d \n", Minimo);
- int Maximo = FindMax(root);
- printf("The Max of the tree is %d \n", Maximo);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement