-
Notifications
You must be signed in to change notification settings - Fork 0
/
DepthFirstSearch.h
126 lines (113 loc) · 2.92 KB
/
DepthFirstSearch.h
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
//
// Created by tomer on 1/15/19.
//
#ifndef FLIGTSIMPROJ2_DEPTHFIRSTSEARCH_H
#define FLIGTSIMPROJ2_DEPTHFIRSTSEARCH_H
#include <stack>
#include "Searcher.h"
template <class T>
class DepthFirstSearch : public Searcher<State<T>>{
private:
stack<State<T>*> openList1;
stack<State<T>*> openList2;
void reverseBackStack();
public:
void addToOpenList(State<T>* curr_state);
bool isOpenListEmpty();
void clearAllStates();
State<T*> popOpenList();
};
template<class T>
void DepthFirstSearch<T>::addToOpenList(State<T> *curr_state) {
//meaning openList2 is the empty stack
State<T>* temp_state;
bool is_inStack1=false;
if(!openList1.empty()){
while(!openList1.empty()){
is_inStack1=true;
temp_state=openList1.top();
if(temp_state==curr_state){
delete curr_state;
return;
}
openList1.pop();
openList2.push(temp_state);
}
}
if(!is_inStack1){
while(!openList2.empty()){
temp_state=openList2.top();
if(temp_state==curr_state){
delete curr_state;
return;
}
openList2.pop();
openList1.push(temp_state);
}
}
if(is_inStack1)
openList2.push(curr_state);
else{
openList1.push(curr_state);
}
reverseBackStack();
}
template<class T>
bool DepthFirstSearch<T>::isOpenListEmpty() {
return (openList1.empty() || openList2.empty());
}
template<class T>
State<T *> DepthFirstSearch<T>::popOpenList() {
Searcher<T> :: evaluatedNodes++;
State<T>* toReturn;
if(!openList1.empty()){
toReturn=openList1.top();
openList1.pop();
}else{
toReturn=openList2.top();
openList2.pop();
}
return toReturn;
}
template<class T>
void DepthFirstSearch<T>::clearAllStates() {
Searcher<T> ::clearAllStates();
State<T>* temp_state;
bool is_inStack1=false;
if(!openList1.empty()){
while(!openList1.empty()){
is_inStack1=true;
temp_state=openList1.top();
if(temp_state->appearInSolution()){
delete temp_state;
}
openList1.pop();
openList2.push(temp_state);
}
}
if(!is_inStack1){
while(!openList2.empty()){
temp_state=openList2.top();
if(temp_state->appearInSolution()){
delete temp_state;
}
openList2.pop();
openList1.push(temp_state);
}
}
reverseBackStack();
}
template<class T>
void DepthFirstSearch<T>::reverseBackStack() {
State<T>* x;
if(!openList1.empty()){
x= openList1.top();
openList1.pop();
openList2.push(x);
}else if(!openList2.empty()){
x = openList2.top();
openList2.pop();
openList1.push(x);
}
}
#endif //FLIGTSIMPROJ2_DEPTHFIRSTSEARCH_H