-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.cpp
138 lines (120 loc) · 3.1 KB
/
bfs.cpp
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
#include <iostream>
#include <unordered_set>
using namespace std;
#define MAX 6 //Max node
struct Tree {
int data;
Tree* left;
Tree* right;
};
struct Antrian {
Tree* data[MAX];
int head;
int tail;
};
Tree* newNode, * current, * leaf, * root = NULL;
void addRight(Tree* current, int data) {
if (current->right != NULL) {
cout << "Kanan tidak kosong!\n";
return;
}
newNode = new Tree();
newNode->left = NULL;
newNode->right = NULL;
newNode->data = data;
current->right = newNode;
}
void addLeft(Tree* current, int data) {
if (current->left != NULL) {
cout << "Kiri tidak kosong!\n";
return;
}
newNode = new Tree();
newNode->left = NULL;
newNode->right = NULL;
newNode->data = data;
current->left = newNode;
}
Tree* createTree(int data) {
newNode = new Tree();
newNode->left = NULL;
newNode->right = NULL;
newNode->data = data;
return newNode;
}
Antrian antre;
void createQueue() {
antre.head = antre.tail = -1;
}
bool isEmpty() {
return (antre.tail == -1) ? true : false;
}
bool isFull() {
return (antre.tail == MAX - 1) ? true : false;
}
void enqueue(Tree *data) {
if (isEmpty()) {
antre.head = antre.tail = 0;
}
else if (isFull()) {
cout << "queue penuh!\n";
return;
}
else {
antre.tail++;
}
antre.data[antre.tail] = data;
}
void dequeue() {
for (int i = antre.head; i < antre.tail; i++) {
antre.data[i] = antre.data[i + 1];
}
antre.tail--;
}
void bfs(Tree* current) {
//Inisialisasi queue
createQueue();
//Masukan root kedalam queue
enqueue(current);
//Buat variabel boolean untuk visited
bool visited[MAX];
for (int i = 0; i < MAX; i++) visited[i] = false;
//Masukan data pertama menjadi visited
visited[current->data] = true;
cout << "BFS Spanning Tree : ";
// While queue belum kosong
while (!isEmpty()) {
Tree* current = antre.data[antre.head];
// Dequeue paling depan
dequeue();
// Jika node kiri bukan NULL dan belum dikunjungi
if (current->left != NULL && !visited[current->left->data]) {
//Print data kiri
cout << current->left->data << " ";
//Masukan data kiri kedalam queue
enqueue(current->left);
//Masukan data kiri sebagai sudah dikunjungi/visited
visited[current->left->data] = true;
}
// Jika node kanan bukan NULL dan belum dikunjungi
if (current->right != NULL && !visited[current->right->data]) {
//Print data kanan
cout << current->right->data << " ";
//Masukan node kedalam queue
enqueue(current->right);
//Masukan data kanan sebagai sudah dikunjungi/visited
visited[current->right->data] = true;
}
}
}
int main() {
leaf = createTree(0);
addLeft(leaf, 1);
addLeft(leaf->left, 4);
addRight(leaf, 2);
addRight(leaf->right, 3);
addLeft(leaf->right, 5);
addLeft(leaf->right->left, 6);
bfs(leaf);
return 0;
}