Skip to content

Commit

Permalink
DFS
Browse files Browse the repository at this point in the history
  • Loading branch information
tomery11 committed Jan 15, 2019
1 parent bf967f8 commit 36050f0
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ add_executable(fligtSimProj2 main.cpp ISearcher.h ISearchable.h CacheManager.cpp
MySerialServer.cpp MyTestClientHandler.cpp Utils.cpp Utils.h
MyTestClientHandler.h MySerialServer.cpp MyParallelServer.cpp MyParallelServer.h
PQueueSearcher.h Searcher.h Server.h Solver.h State.h
State.cpp StatePriority.h StringReverser.h StringReverser.cpp Matrix.h Matrix.cpp)
State.cpp StatePriority.h StringReverser.h StringReverser.cpp Matrix.h Matrix.cpp DepthFirstSearch.h)
109 changes: 109 additions & 0 deletions DepthFirstSearch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// 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 {
private:
stack<State<T>*> openList1;

stack<State<T>*> openList2;
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);

}

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);
}
}

}


#endif //FLIGTSIMPROJ2_DEPTHFIRSTSEARCH_H

0 comments on commit 36050f0

Please sign in to comment.