-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathBinaryFirstSearch.c
96 lines (92 loc) · 1.7 KB
/
BinaryFirstSearch.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
#include <stdio.h>
#include <stdlib.h>
//tree declaration
struct Node
{
int data;
struct Node* left;
struct Node* right;
};
typedef struct Node node;
node* createnode(int data)
{
node* new_node = ((node*)malloc(sizeof(node)));
new_node->data=data;
new_node->left=new_node->right=NULL;
return new_node;
}
//queue declaration
node* queue[100]={};
int l=-1,r=-1;
void push(node* data)
{
if(r==100){
printf("queue is already full.");
}else if(r==-1){
l=0;
r=r+1;
queue[r]=data;
}else{
r=r+1;
queue[r]=data;
}
}
void pop()
{
if(l==-1 && r==-1){
printf("queue is already empty.\n");
}
else if (l<r){
l++;
}else{
l=r=-1;
}
}
void display()
{
for (int i = l; i <= r; i++)
{
printf("%d ",queue[i]->data);
}
}
//BFS
void BFS(node *root){
if (root==NULL){
printf("Tree is empty!\n");
return;
}
if (root->left==NULL && root->right==NULL)
{
printf("%d\n",root->data);
return;
}
node *temp;
push(root);
int size=r+1;
while (size!=0)
{
temp=queue[l];
pop();
printf("%d ",temp->data);
if(temp->left!=NULL){
push(temp->left);
}
if (temp->right!=NULL)
{
push(temp->right);
}
if (l==-1 && r==-1)
break;
size=r-l+1;
}
}
int main(){
node* root = createnode(10);
root->left = createnode(11);
root->left->left = createnode(7);
root->left->right = createnode(12);
root->right = createnode(9);
root->right->left = createnode(15);
root->right->right = createnode(8);
BFS(root);
}