-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_BFS.c
160 lines (146 loc) · 3.22 KB
/
01_BFS.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
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
/*
------------- BFS IMPLEMENTATION ---------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h> // We have imported inttypes to lessen the memory usage as much as possible
#include <stdint.h>
/*
@param search -> The value to be searched.
@param insertnode -> The values to be inserted.
@param select -> The option to be selected.
@param bfs_found -> Counter variable to increment when found the target.
@param queue -> The array to insert the numbers(Size can vary accordng to your requirements).
@param front,queue -> These are conditions of queue to traverse the array.
*/
int queue[100];
int front=0,rear=-1;
static int8_t n=0;
size_t search;
static int temp=0;
// A linked list implementation
struct node
{
int data;
struct node *left;
struct node *right;
};
// Function declarations
void printNode(struct node *root);
void enqueue(struct node *root);
void dequeue();
void BFS(struct node *root);
struct node *insert(struct node *root, int value) // * Insertion of Nodes value and Construction of Tree *
{
if(root==NULL)
{
root=(struct node*)malloc(sizeof(struct node));
root->left=root->right=NULL;
root->data=value;
return root;
}
else
{
if(value<=root->data)
{
root->left=insert(root->left,value);
}
else
{
if(value>root->data)
{
root->right=insert(root->right,value);
}
}
return root;
}
};
void printNode(struct node *root) // * Prints the values of node *
{
static int bfs_found=0;
if(root!=NULL)
{
printf("%d ",root->data);
if(root->data==search)
{
printf("\nThe goal is found.....");
printf("\nThe program is being terminated.......");
exit(0);
}
bfs_found++;
}
if(bfs_found==temp)
{
printf("\nThe goal is not found.....");
printf("\nYou can try again:)");
bfs_found=0;
}
}
// * Queue *
void enqueue(struct node *root) // * Adding the element into the Queue *
{
if(root!=NULL)
{
rear++;
queue[rear]=(uintptr_t)(root);
}
}
void dequeue() // * Removing Element from the Queue *
{
if(rear>=front)
{
struct node *root=(struct node *)(intptr_t)queue[front];
printNode(root);
front++;
enqueue(root->left);
enqueue(root->right);
}
}
// * BFS Function *
void BFS(struct node *root)
{
if(root!=NULL)
{
enqueue(root);
do
{
dequeue();
}while(front<=rear);
}
}
int main()
{
struct node *root=NULL;
int8_t select,limit;
int i;
size_t insertnode;
printf("Enter your choice\n1.Enter values\n2.BFS SEARCH");
while(1)
{
printf("\n\nPlease enter the option ");
scanf("%d",&select);
switch(select)
{
case 1:
printf("Enter number of nodes in tree \n");
scanf("%d",&n);
temp=temp+n;
for(i=0;i<n;i++)
{
printf("Enter value ");
scanf("%d",&insertnode);
root=insert(root, insertnode);
}
break;
case 2:
printf("Enter the value to be searched with BFS Algorithm....");
scanf("%d",&search);
BFS(root);
break;
default:
printf("* ERROR CASE...... PLEASE TRY AGAIN AND USE YOUR BRAIN *");
break;
}
}
return 0;
}