-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinMax.C
More file actions
68 lines (68 loc) · 1.42 KB
/
MinMax.C
File metadata and controls
68 lines (68 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
struct tnode
{
int data;
struct tnode *right, *left;
};
struct tnode *create(struct tnode *root, int data)
{
struct tnode *h = (struct tnode *)malloc(sizeof(struct tnode));
h->data = data;
if (root == NULL)
{
h->left = NULL;
h->right = NULL;
root = h;
return root;
}
else
{
if (data > root->data)
{
root->right = create(root->right, data);
}
else
{
root->left = create(root->left, data);
}
return root;
}
}
int findMin(struct tnode *root)
{
while (root->left != NULL)
{
root = root->left;
}
return root->data;
}
int findMax(struct tnode *root)
{
while (root->right != NULL)
{
root = root->right;
}
return root->data;
}
int main()
{
int t, opt, h1, h2;
struct tnode *root = NULL;
do
{
printf("\nEnter option number: 1. Enter element\t0. Stop adding");
scanf("%d", &opt);
if (opt == 1)
{
printf("\nEnter the element: ");
scanf("%d", &t);
root = create(root, t);
}
} while (opt != 0);
printf("\nThe greatest number is:\t");
printf("%d", findMax(root));
printf("\nThe smallest number is:\t");
printf("%d", findMin(root));
return 0;
}