-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.hpp
More file actions
377 lines (346 loc) · 8.86 KB
/
Copy pathGraph.hpp
File metadata and controls
377 lines (346 loc) · 8.86 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#pragma once
#include <iostream>
#include "Stack.hpp"
#include "Queue.hpp"
#include "LinkedList.hpp"
#include "Vector.hpp"
/**
*
*
*/
template<class T>
class Graph{
private:
class Node{
private:
Node* parentNode;
bool m_hasChildren;
bool m_visited;
T m_data;
LinkedList<Node*> m_children;
friend class Graph;
};
Node* m_pRoot = nullptr;
// Accesors
size_t m_size;
Node* dataBFS(T t_data);
Node* dataDFS(T t_data);
// Modifieres
Node* generateNode(T t_data);
public:
// Constructor
Graph();
Graph(T t_data);
Graph(T t_data, T t_children[], size_t t_sizeArray);
// Accesors
bool empty();
bool BFS(T t_data);
bool DFS(T t_data);
void traverse();
// Modifiers
void add(T t_data, T t_parent);
void clear();
};
/**
* @brief Default constructor that creates
* a Graph with nullptr in m_pRoot and m_pLast and m_size = 0;
*/
template<class T>
Graph<T>::Graph(){
m_pRoot = nullptr;
m_size = 0;
}
/**
* @brief Constructor that creates
* a Graph with size = 1
* @param t_data is the value stored in the Graph.
*/
template<class T>
Graph<T>::Graph(T t_data){
m_pRoot = generateNode(t_data);
}
/**
* @brief Constructor that creates
* a Graph with copies of the array passed as a parameter
* @param t_array is the array used to create the graph with the given values
* @param t_capacity sets the capacity of the graph to match the array's parameter size
*/
template<class T>
Graph<T>::Graph(T t_data, T t_children[], size_t t_sizeArray){
m_pRoot = generateNode(t_data);
m_pRoot->m_hasChildren = true;
// m_pRoot->m_children.push_back(generateNode(t_children[0]));
// m_size = 2;
for(size_t i = 0; i < t_sizeArray; i++){
add(t_children[i], m_pRoot->m_data);
}
}
/**
* @brief Generates a Node for new data.
* @param t_data is used to give value to the data in the new Node.
*/
template<class T>
typename Graph<T>::Node*
Graph<T>::generateNode(T t_data){
Node* newNode = new Node();
newNode->m_data = t_data;
m_size++;
return newNode;
}
/*
* @brief Return true if the Tree is empty.
*
*/
template<class T>
bool Graph<T>::empty(){
return !m_pRoot;
}
/*
* @brief Returns true if the data passed in is found in the graph using BFS.
* @param t_data is the data to look for in the graph.
*/
template<class T>
bool Graph<T>::BFS(T t_data){
if(!m_pRoot){
return false;
}
if(m_pRoot->m_data == t_data){
return true;
}
Node* pTemp = m_pRoot;
Queue<Node*> queue_Temp(pTemp);
LinkedList<Node*> visitedNodes;
while(!queue_Temp.empty()){
Node* pCurrent = queue_Temp.dequeue();
pCurrent->m_visited = true;
visitedNodes.push_back(pCurrent);
if(pCurrent->m_data == t_data){
for(size_t i = 0; i < visitedNodes.size(); i++){
visitedNodes.at(i)->m_visited = false;
}
return true;
}
if(pCurrent->m_hasChildren){
for(size_t i = 0; i < pCurrent->m_children.size(); i++){
//pCurrent->m_children.at(i)->m_visited = true;
if(!pCurrent->m_children.at(i)->m_visited){
queue_Temp.enqueue(pCurrent->m_children.at(i));
}
}
// check out the visited problem
}
}
for(size_t i = 0; i < visitedNodes.size(); i++){
visitedNodes.at(i)->m_visited = false;
}
return false;
}
/*
* @brief Returns a Node pointer if the data passed in is found in the graph using BFS.
* @param t_data is the data to look for in the tree.
*/
template<class T>
typename Graph<T>::Node*
Graph<T>::dataBFS(T t_data){
if(!m_pRoot){
return nullptr;
}
if(m_pRoot->m_data == t_data){
return m_pRoot;
}
Queue<Node*> queue_Temp(m_pRoot);
LinkedList<Node*> visitedNodes;
while(!queue_Temp.empty()){
Node* pCurrent = queue_Temp.dequeue();
pCurrent->m_visited = true;
visitedNodes.push_back(pCurrent);
if(pCurrent->m_data == t_data){
for(size_t i = 0; i < visitedNodes.size(); i++){
visitedNodes.at(i)->m_visited = false;
}
return pCurrent;
}
if(pCurrent->m_hasChildren){
for(size_t i = 0; i < pCurrent->m_children.size(); i++){
//pCurrent->m_children.at(i)->m_visited = true;
if(!pCurrent->m_children.at(i)->m_visited){
queue_Temp.enqueue(pCurrent->m_children.at(i));
}
}
}
}
for(size_t i = 0; i < visitedNodes.size(); i++){
visitedNodes.at(i)->m_visited = false;
}
return nullptr;
}
/*
* @brief Return true if t_data is found in the Graph using DFS.
* @param t_data is the value fo be found in the true.
*/
template<class T>
bool Graph<T>::DFS(T t_data){
if(!m_pRoot){
return false;
}
if(m_pRoot->m_data == t_data){
return true;
}
Node* pTemp = m_pRoot;
Stack<Node*> stack_Temp(pTemp);
LinkedList<Node*> visitedNodes;
//stack_Temp.printList();
while(!stack_Temp.empty()){
Node* pCurrent = stack_Temp.pop_front();
pCurrent->m_visited = true;
visitedNodes.push_back(pCurrent);
if(pCurrent->m_data == t_data){
for(size_t i = 0; i < visitedNodes.size(); i++){
visitedNodes.at(i)->m_visited = false;
}
return true;
}
if(pCurrent->m_hasChildren){ //
for(size_t i = 0; i < pCurrent->m_children.size(); i++){
// pCurrent->m_children.at(i)->m_visited = true;
if(!pCurrent->m_children.at(i)->m_visited){
stack_Temp.push_front(pCurrent->m_children.at(i));
}
}
}
}
for(size_t i = 0; i < visitedNodes.size(); i++){
visitedNodes.at(i)->m_visited = false;
}
//pTemp->m_visited = false;
return false;
}
/*
* @brief Return a the Node pointer if t_data is found in the Graph using DFS.
* @param t_data is the value fo be found in the true.
*/
template<class T>
typename Graph<T>::Node*
Graph<T>::dataDFS(T t_data){
if(!m_pRoot){
return nullptr;
}
if(m_pRoot->m_data == t_data){
return m_pRoot;
}
Node* pTemp = m_pRoot;
Stack<Node*> stack_Temp(pTemp);
LinkedList<Node*> visitedNodes;
//stack_Temp.printList();
while(!stack_Temp.empty()){
Node* pCurrent = stack_Temp.pop_front();
pCurrent->m_visited = true;
visitedNodes.push_back(pCurrent);
if(pCurrent->m_data == t_data){
for(size_t i = 0; i < visitedNodes.size(); i++){
visitedNodes.at(i)->m_visited = false;
}
return pCurrent;
}
if(pCurrent->m_hasChildren){ // or ->m_children[0]
for(size_t i = 0; i < pCurrent->m_children.size(); i++){
if(!pCurrent->m_children.at(i)->m_visited){
stack_Temp.push_front(pCurrent->m_children.at(i));
}
}
}
}
for(size_t i = 0; i < visitedNodes.size(); i++){
visitedNodes.at(i)->m_visited = false;
}
return nullptr;
}
/*
* @brief Prints all the values in the graph.
*/
template<class T>
void Graph<T>::traverse(){
if(!m_pRoot){
return nullptr;
}
Node* pTemp = m_pRoot;
Queue<Node*> queue_Temp(pTemp);
LinkedList<Node*> visitedNodes;
while(!queue_Temp.empty()){
Node* pCurrent = queue_Temp.dequeue();
pCurrent->m_visited = true;
visitedNodes.push_back(pCurrent);
std::cout<<pCurrent->m_data<<"{";
if(pCurrent->m_hasChildren){
for(size_t i = 0; i < pCurrent->m_children.size(); i++){
if(!pCurrent->m_children.at(i)->m_visited){
//std::cout<<"ssss ";
//pCurrent->m_children.at(i)->m_visited = true;
queue_Temp.enqueue(pCurrent->m_children.at(i));
}
std::cout<<pCurrent->m_children.at(i)->m_data<<",";
}
}
std::cout<<"}\n";
}
for(size_t i = 0; i < visitedNodes.size(); i++){
visitedNodes.at(i)->m_visited = false;
}
//pTemp->m_visited = false;
}
/*
* @brief Adds new data to the graph.
* @param t_data is the number to be added to the graph.
* @param t_parent is the parent Data of the t_data to be added.
*/
template<class T>
void Graph<T>::add(T t_data, T t_parent){
if(!m_pRoot){
m_pRoot = generateNode(t_data);
return;
}
if(t_data == t_parent){
return;
}
if(DFS(t_parent)){
Node* pTempParent = dataDFS(t_parent);
pTempParent->m_hasChildren = true;
if(BFS(t_data)){
pTempParent->m_children.push_back(dataBFS(t_data));
return;
}
pTempParent->m_children.push_back(generateNode(t_data));
}
}
/*
* @brief Deletes all the nodes and clears the graph.
*/
template<class T>
void Graph<T>::clear(){
if(!m_pRoot){
return;
}
//Node* pTemp = m_pRoot;
Stack<Node*> stack_Temp(m_pRoot);
//stack_Temp.push_front(m_pRoot);
Node* deleteNode = nullptr;
while(!stack_Temp.empty()){
deleteNode = stack_Temp.pop_front();
deleteNode->m_visited = true;
if(deleteNode->m_hasChildren){
for(size_t i = 0; i < deleteNode->m_children.size(); i++){
if(!deleteNode->m_children.at(i)->m_visited){
stack_Temp.push_front(deleteNode->m_children.at(i));
}
}
//deleteNode->m_children.clear();
}
deleteNode->m_visited = false;
delete [] deleteNode;
deleteNode = nullptr;
}
m_pRoot->parentNode = nullptr;
m_pRoot->m_hasChildren = false;
m_size = 0;
m_pRoot = nullptr;
}