-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLAB_10_CODE.C
106 lines (85 loc) · 1.73 KB
/
LAB_10_CODE.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
//Binary Search tree
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
struct Node *left;
int data;
struct Node *right;
} * node;
node getnode(int item)
{
node temp = (node)malloc(sizeof(struct Node));
temp->left = NULL;
temp->data = item;
temp->right = NULL;
return temp;
}
node insert(node root, int ele)
{
if (root == NULL)
return getnode(ele);
else if (ele < root->data)
root->left = insert(root->left, ele);
else if (ele > root->data)
root->right = insert(root->right, ele);
return root;
}
void inorder(node root)
{
if (root == NULL)
return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
void preorder(node root)
{
if (root == NULL)
return;
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
void postorder(node root)
{
if (root == NULL)
return;
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
int main()
{
node root = NULL;
int e, ch = 1;
while (ch != 5)
{
printf("\n\n1.Insert\n2.PreOrder\n3.InOrder\n4.PostOrder\n");
printf("5.Exit\n");
scanf("%d", &ch);
printf("\n");
switch (ch)
{
case 1:
printf("Element:");
scanf("%d", &e);
root = insert(root, e);
break;
case 2:
preorder(root);
break;
case 3:
inorder(root);
break;
case 4:
postorder(root);
break;
case 5:
printf("Exiting.");
exit(1);
default:
printf("Wrong input!");
}
}
}