-
Notifications
You must be signed in to change notification settings - Fork 1
/
q4.c
113 lines (102 loc) · 1.94 KB
/
q4.c
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/***********************************************************
Program to find the minimum and maximum element in a BST
Author: leonatwork(Noel Aby Das)
**********************************************************/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *lptr,*rptr;
};
struct node *root=NULL;
struct node *getNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = item;
temp->lptr = temp->rptr = NULL;
return temp;
}
struct node* insert(struct node* node, int data)
{
if (node == NULL)
return getNode(data);
if (data <= node->data)
node->lptr = insert(node->lptr, data);
else if (data > node->data)
node->rptr = insert(node->rptr, data);
return node;
}
int max(struct node *root)
{
if(root->rptr==NULL)
return root->data;
else
return max(root->rptr);
}
int min(struct node *root)
{
if(root->lptr==NULL)
return root->data;
else
return min(root->lptr);
}
void preorder(struct node *root)
{
if (root != NULL)
{
printf("%d -> ", root->data);
preorder(root->lptr);
preorder(root->rptr);
}
}
int main()
{
int ch;
while(1)
{
printf("\nMenu\n1.Insert\n2.Max element\n3.Min element\n4.Preorder\n5.Exit\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
int n;
printf("\nEnter data : ");
scanf("%d",&n);
if(root==NULL)
root= insert(root,n);
else
insert(root,n);
}
break;
case 2:
{
if(root==NULL)
printf("Empty tree\n");
printf("Maximum element of the tree : %d ",max(root));
}
break;
case 3:
{
if(root==NULL)
printf("Empty tree\n");
printf("Minimum element of the tree : %d ",min(root));
}
break;
case 4:
{if(root==NULL)
printf("Empty tree\n");
else
{
printf("Preorder : \n");
preorder(root);
printf("\n");
}}
break;
case 5:
exit(1);
}
}
return 0;
}