-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab10.c
More file actions
177 lines (168 loc) · 3.24 KB
/
Copy pathLab10.c
File metadata and controls
177 lines (168 loc) · 3.24 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*Design , Develop and Implement a menu driven Program in C for the following
operations on
Binary Search Tree (BST) of Integers
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
e. Exit
*/
/*
InOrder --> Left Root Right
Algorithm
---------
1. Traverse the left subtree ( call left subtree)
2. Visit the root
3. Traverse the right subtree ( call right subtree)
PreOrder --> Root Left RIght
Algorithms
----------
1. Visit the root
2. Traverse the left subtree
3. Traverse right subtree
PostOrder --> Left Right Root
Algorithms
----------
1. Traverse the left subtree
2. Traverse right subtree
3. Visit the root
*/
# include<stdio.h>
# include<stdlib.h>
int flag, i;
struct node
{
int data;
struct node* leftChild, *rightChild;
};
typedef struct node *NODE;
NODE createnode(int item)
{
NODE temp;
temp = (NODE)malloc(sizeof(struct node));
temp->data = item;
temp->leftChild = NULL;
temp->rightChild = NULL;
return temp;
}
void insertBST(NODE root, NODE newNode)
{
if(newNode->data == root->data)
{
printf("Key already exists\n");
i--;
return;
}
else if (newNode->data < root->data)
{
if (root->leftChild == NULL)
root->leftChild = newNode;
else
insertBST(root->leftChild, newNode);
}
else
{
if (root->rightChild == NULL)
root->rightChild = newNode;
else
insertBST(root->rightChild, newNode);
}
}
int search(NODE root, int key)
{
if(!root)
return -1;
if(key == root->data)
return 1;
if(key < root->data)
return search(root->leftChild, key);
else
return search(root->rightChild,key);
}
void inorder(NODE temp)
{
if (temp != NULL)
{
inorder(temp->leftChild);
printf("%d\t", temp->data);
inorder(temp->rightChild);
}
}
void preorder(NODE temp)
{
if (temp != NULL)
{
printf("%d\t", temp->data);
preorder(temp->leftChild);
preorder(temp->rightChild);
}
}
void postorder(NODE temp)
{
if (temp != NULL)
{
postorder(temp->leftChild);
postorder(temp->rightChild);
printf("%d\t", temp->data);
}
}
int main()
{
int choice,n,item;
int key,keyFound = 0;
NODE root=NULL,newNode;
while(1)
{
choice=0;
printf("\n-----------------MENU----------------------\n");
printf("1. Create a BST\n");
printf("2. Traverse a BST\n");
printf("3. Search a BST\n");
printf("4. Exit\n");
printf("-------------------------------------------\n");
printf("Enter choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1: root= NULL;
printf("Enter the number of elements in the BST:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the integer:");
scanf("%d",&item);
newNode = createnode(item);
if(root == NULL)
root = newNode;
else
insertBST(root,newNode);
}
break;
case 2: if (root == NULL)
{
printf("Tree is empty\n");
break;
}
else
{
printf("BST Preorder travsersal\n");
preorder(root);
printf("\nBST Inorder travsersal\n");
inorder(root);
printf("\nBST Postorder travsersal\n");
postorder(root);
break;
}
case 3: printf("Enter the search key:");
scanf("%d",&key);
keyFound = search(root,key);
if(keyFound == 1)
printf("Element %d is found in the BST",key);
else
printf("Element %d is not found in the BST",key);
break;
case 4: return;
default:printf("Wrong choice\n");
return;
}
}
}