Skip to content

Commit

Permalink
BFS + DFS(SMALL FIX)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomery11 committed Jan 15, 2019
1 parent b4f2e91 commit 228bfa4
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 3 deletions.
110 changes: 110 additions & 0 deletions BreadthFirstSearch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// Created by tomer on 1/15/19.
//

#ifndef FLIGTSIMPROJ2_BREADTHFIRSTSEARCH_H
#define FLIGTSIMPROJ2_BREADTHFIRSTSEARCH_H

#include "Searcher.h"
#include <queue>;

template <class T>
class BreadthFirstSearch {
private:
queue<State<T>*> openList1;

queue<State<T>*> openList2;
public:
void addToOpenList(State<T>* curr_state);
bool isOpenListEmpty();
void clearAllStates();
State<T*> popOpenList();
};

template<class T>
bool BreadthFirstSearch<T>::isOpenListEmpty() {
return (openList1.empty()||openList2.empty());
}

template<class T>
void BreadthFirstSearch<T>::addToOpenList(State<T> *curr_state) {
// before we put the state in our list we will check that it doesn't
//exist already
State<T>* temp_state;
bool is_inStack1=false;
if(!openList1.empty()){
while(!openList1.empty()){
is_inStack1=true;
temp_state=openList1.front();
if(temp_state==curr_state){
delete curr_state;
return;
}
openList1.pop();
openList2.push(temp_state);
}
}
if(!is_inStack1){
while(!openList2.empty()){
temp_state=openList2.front();
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>
void BreadthFirstSearch<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.front();
if(temp_state->appearInSolution()){
delete temp_state;
}
openList1.pop();
openList2.push(temp_state);
}
}
if(!is_inStack1){
while(!openList2.empty()){
temp_state=openList2.front();
if(temp_state->appearInSolution()){
delete temp_state;
}
openList2.pop();
openList1.push(temp_state);
}
}
}

template<class T>
State<T *> BreadthFirstSearch<T>::popOpenList() {
Searcher<T> :: evaluatedNodes++;
State<T>* toReturn;
if(!openList1.empty()){
toReturn=openList1.front();
openList1.pop();
}else{
toReturn=openList2.front();
openList2.pop();
}
return toReturn;
}


#endif //FLIGTSIMPROJ2_BREADTHFIRSTSEARCH_H
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 DepthFirstSearch.h)
State.cpp StatePriority.h StringReverser.h StringReverser.cpp Matrix.h Matrix.cpp DepthFirstSearch.h BreadthFirstSearch.h)
21 changes: 19 additions & 2 deletions DepthFirstSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ template <class T>
class DepthFirstSearch {
private:
stack<State<T>*> openList1;

stack<State<T>*> openList2;
void reverseBackStack();
public:
void addToOpenList(State<T>* curr_state);
bool isOpenListEmpty();
Expand Down Expand Up @@ -51,8 +51,11 @@ void DepthFirstSearch<T>::addToOpenList(State<T> *curr_state) {
}
if(is_inStack1)
openList2.push(curr_state);
else
else{
openList1.push(curr_state);
}

reverseBackStack();

}

Expand Down Expand Up @@ -102,7 +105,21 @@ void DepthFirstSearch<T>::clearAllStates() {
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);
}
}


Expand Down

0 comments on commit 228bfa4

Please sign in to comment.