Maze solver complexity - c++

I have sevral questions about maze - solver algorithms:C
What is the time complexity of recursive (backtracking) maze-solver?(As an amount of paths in the matrix? - I can`t figure this number..)
What is the time complexity of BFS based maze-solver?(O(n^2)?) n is a dimension of the squared maze matrix?
What is the best algoritm to count the number of all possible paths from the source to the destination in the maze?
Can you propose the idea if and how an it be implemented using parallel computing (opecl/cuda)
Here is the class for my maze solver ,which has brute (recursive) and bfs based version.I implemented it and the questions are based on this maze-solver implementation
//MazeSolver.h
//#define N 5
typedef enum {BLACK,WHITE,GRAY,VISITED} color;
class MazeSolver
{
public:
MazeSolver(){}
struct Cell
{
unsigned int _x;
unsigned int _y;
Cell* _p;
Cell(unsigned int x = 0,unsigned int y = 0, Cell* p = NULL) : _x(x),_y(y),_p(p) {}
bool operator == (const Cell& c)
{
return _x == c._x && _y == c._y;
}
};
bool solveMazeBrute(color maze[][N],unsigned int n,int xS,int yS,int xD,int yD,std::list<Cell>& path);
bool solveMazeBFS(color maze[][N],unsigned int n,int xS,int yS,int xD,int yD,std::list<Cell>& path);
private:
std::queue<Cell* > _bfs;
std::vector<Cell* > _cells;
Cell* addCellBFS(color maze[][N],unsigned int n,int x,int y,Cell* p = NULL);
};
//MazeSolver.cpp
MazeSolver::Cell* MazeSolver::addCellBFS(color maze[][N],unsigned int n,int x,int y,Cell* p)
{
if (x >= 0 && x < n && y >= 0 && y < n && maze[x][y] == WHITE)
{
Cell* c = new Cell(x,y,p);
maze [x][y] = VISITED;
_bfs.push(c);
_cells.push_back(c);
return c;
}
return NULL;
}
bool MazeSolver::solveMazeBrute(color maze[][N],unsigned int n,int xS,int yS,int xD,int yD,std::list<MazeSolver::Cell>& path)
{
bool solved = false;
if (xS < 0 || xS >= n || yS < 0 || yS >= n || maze[xS][yS] == VISITED || maze[xS][yS] == BLACK)
{
return false;
}
Cell s(xS,yS);
Cell d(xD,yD);
if (s == d)
{
path.push_front(s);
return true;
}
maze[xS][yS] = VISITED;
if (solveMazeBrute(maze,n,xS + 1,yS,xD,yD,path) ||
solveMazeBrute(maze,n,xS - 1,yS,xD,yD,path) ||
solveMazeBrute(maze,n,xS,yS + 1,xD,yD,path) ||
solveMazeBrute(maze,n,xS,yS - 1,xD,yD,path))
{
path.push_front(s);
solved = true;
}
maze[xS][yS] = WHITE;
return solved;
}
bool MazeSolver::solveMazeBFS(color maze[][N],unsigned int n,int xS,int yS,int xD,int yD,std::list<Cell>& path)
{
Cell d(xD,yD);
addCellBFS(maze,n,xS,yS);
while(!_bfs.empty())
{
Cell* cur = _bfs.front();
if (*cur == d)
{
while (cur != NULL)
{
path.push_front(*cur);
cur = cur->_p;
}
return true;
}
_bfs.pop();
addCellBFS(maze,n,cur->_x - 1,cur->_y,cur);
addCellBFS(maze,n,cur->_x + 1,cur->_y,cur);
addCellBFS(maze,n,cur->_x,cur->_y - 1,cur);
addCellBFS(maze,n,cur->_x,cur->_y + 1,cur);
}
for(std::vector<Cell*>::iterator itC= _cells.begin();itC != _cells.end();++itC)
{
maze[(*itC)->_x][(*itC)->_y] = WHITE;
delete *itC;
}
return false;
}

Maybe we can find the target in O(n).
Let's imagine 5X5 matrix.
In each iteration we'll step single step ahead, we'll check the cell is valid and is not the end of maze, and mark it as "visited".
So, we'll start at the first cell (0,0). in next iteration we'll check the next layer, mean (0,1),(1,0), in next iteration we'll continue check the next layer (0,2),(1,1),(2,0). and so on.
So, we'll check each cell once only! and we'll find the end (the target) in n complexity.
Am i wrong?

Related

Im trying to make an astar algorithm for a school thing

Here is my code i've been working on. I've scraped off all the errors from the code. But i cant get the code to run correctly and the application either explodes or doesn't atleast visualise the astar.
#include <SFML/Graphics.hpp>
#include <queue>
#include <unordered_map>
#include <cmath>
#include <vector>
const int WIDTH = 10;
const int HEIGHT = 10;
struct Node
{
int x;
int y;
mutable float gScore;
mutable float fScore;
mutable const Node* parent;
Node(int _x, int _y) : x(_x), y(_y), gScore(0), fScore(0), parent(nullptr) {}
bool operator==(const Node& other) const
{
return x == other.x && y == other.y;
}
};
// Hash function for nodes, needed for storing them in an unordered_map
struct NodeHash
{
std::size_t operator()(const Node& node) const
{
return std::hash<int>()(node.x) ^ std::hash<int>()(node.y);
}
};
// Overload the ">" operator for nodes, needed for using them in a priority queue
struct NodeGreater
{
bool operator()(const Node& left, const Node& right) const
{
return left.fScore > right.fScore;
}
};
// Represents a 2D grid
class Grid
{
public:
Grid()
{
// Initialize the grid with all nodes as walkable
for (int x = 0; x < WIDTH; x++)
{
for (int y = 0; y < HEIGHT; y++)
{
nodes[x][y] = true;
}
}
}
// Set the walkability of a node
void setNode(int x, int y, bool walkable)
{
nodes[x][y] = walkable;
}
// Check if a node is walkable
bool isNodeWalkable(int x, int y)
{
return nodes[x][y];
}
private:
bool nodes[WIDTH][HEIGHT];
};
// Calculate the Euclidean distance between two nodes
float euclideanDistance(Node a, Node b)
{
int xDist = b.x - a.x;
int yDist = b.y - a.y;
return std::sqrt(xDist * xDist + yDist * yDist);
}
// Estimate the cost of reaching the end node from a given node
float heuristicCostEstimate(Node start, Node end)
{
return euclideanDistance(start, end);
}
// Get the neighbors of a given node
std::vector<Node> getNeighbors(Grid grid, Node node)
{
std::vector<Node> neighbors;
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
// Skip the current node
if (x == 0 && y == 0)
{
continue;
}
int neighborX = node.x + x;
int neighborY = node.y + y;
// Make sure the neighbor is within the bounds of the grid
if (neighborX >= 0 && neighborX < WIDTH && neighborY >= 0 && neighborY < HEIGHT)
{
// Make sure the neighbor is walkable
if (grid.isNodeWalkable(neighborX, neighborY))
{
Node neighbor = { neighborX, neighborY };
neighbors.push_back(neighbor);
}
}
}
}
return neighbors;
}
// Reconstruct the path from the end node to the start node
std::vector<Node> reconstructPath(Node end)
{
std::vector<Node> path;
Node current = end;
while (current.parent != nullptr)
{
path.push_back(current);
current = *current.parent;
}
path.push_back(current);
std::reverse(path.begin(), path.end());
return path;
}
// Run the A* algorithm to find the shortest path from the start to the end node
std::vector<Node> aStar(Grid grid, Node start, Node end)
{
// Initialize the open and closed sets
std::priority_queue<Node, std::vector<Node>, NodeGreater> openSet;
std::unordered_map<Node, bool, NodeHash> closedSet;
// Add the start node to the open set
openSet.push(start);
// Set the starting node's gScore to 0 and fScore to the estimated cost to the end node
start.gScore = 0;
start.fScore = heuristicCostEstimate(start, end);
// Keep searching until the open set is empty
while (!openSet.empty())
{
// Get the node in the open set with the lowest fScore
Node current = openSet.top();
openSet.pop();
// If the current node is the end node, we have found the shortest path
if (current == end)
{
return reconstructPath(current);
}
// Add the current node to the closed set
closedSet[current] = true;
// Get the neighbors of the current node
std::vector<Node> neighbors = getNeighbors(grid, current);
// Iterate through the neighbors
for (const Node& neighbor : neighbors)
{
// Skip the neighbor if it is already in the closed set
if (closedSet.count(neighbor) > 0)
{
continue;
}
// Calculate the tentative gScore for the neighbor
float tentativeGScore = current.gScore + euclideanDistance(current, neighbor);
bool neighborIsBetter = false;
bool neighborInOpenSet = false;
// Check if the neighbor is already in the open set
std::priority_queue<Node, std::vector<Node>, NodeGreater> tempOpenSet = openSet;
while (!tempOpenSet.empty())
{
Node tempNeighbor = tempOpenSet.top();
tempOpenSet.pop();
if (tempNeighbor == neighbor)
{
neighborInOpenSet = true;
break;
}
}
// Update the neighbor's scores and parent if the tentative gScore is lower than
if (tentativeGScore < neighbor.gScore)
{
neighborIsBetter = true;
neighbor.gScore = tentativeGScore;
neighbor.fScore = tentativeGScore + heuristicCostEstimate(neighbor, end);
}
if (!neighborInOpenSet || neighborIsBetter)
{
// Update the neighbor's parent to the current node
neighbor.parent = &current;
// Add the neighbor to the open set if it is not already there
if (!neighborInOpenSet)
{
openSet.push(neighbor);
}
}
}
}
// If we reach here, it means that we have not found a path to the end node
return std::vector<Node>();
}
int main()
{
// Create the window and grid
sf::RenderWindow window(sf::VideoMode(500, 500), "A*");
Grid grid;
// Set some nodes as unwalkable
grid.setNode(3, 3, false);
grid.setNode(3, 4, false);
grid.setNode(3, 5, false);
grid.setNode(4, 3, false);
// Set the start and end nodes
Node start = { 1, 1 };
Node end = { 8, 8 };
// Run the A* algorithm and get the path
std::vector<Node> path = aStar(grid, start, end);
// Main loop
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
// Draw the grid
for (int x = 0; x < WIDTH; x++)
{
for (int y = 0; y < HEIGHT; y++)
{
sf::RectangleShape rect(sf::Vector2f(50, 50));
rect.setPosition(x * 50, y * 50);
if (grid.isNodeWalkable(x, y))
{
rect.setFillColor(sf::Color::White);
}
else
{
rect.setFillColor(sf::Color::Black);
}
window.draw(rect);
}
}
// Draw the path
for (Node node : path)
{
sf::RectangleShape rect(sf::Vector2f(50, 50));
rect.setPosition(node.x * 50, node.y * 50);
rect.setFillColor(sf::Color::Green);
window.draw(rect);
}
// Draw the start and end nodes
sf::RectangleShape startRect(sf::Vector2f(50, 50));
startRect.setPosition(start.x * 50, start.y * 50);
startRect.setFillColor(sf::Color::Red);
window.draw(startRect);
sf::RectangleShape endRect(sf::Vector2f(50, 50));
endRect.setPosition(end.x * 50, end.y * 50);
endRect.setFillColor(sf::Color::Blue);
window.draw(endRect);
window.display();
}
return 0;
}
any idea why its doing this?
I've tried to debug the code and i cant seem to find the problem. Either the code doesn't run or it just shows the same thing

Unsure about the time complexity

So I was doing a question on LeetCode...
Question:
Write an efficient algorithm that searches for a target value in an m x n integer matrix. The matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example 1:
Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
Output: true
Example 2:
Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
Output: false
So first I attempted this question by regular brute force method:
Answer-1:
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int t) {
int i=0,j,I=matrix.size(),J,mj;
for(;i<I;i++)
{
j=0;J=matrix[i].size()-1;
mj=(j+J)/2;
while(j<=J)
{
if(matrix[i][mj]==t)
return true;
else if(matrix[i][mj]<t)
j=mj+1;
else
J=mj-1;
mj=(j+J)/2;
}
}
return false;
}
};
Then I decided to do it by idk Divide and Conquer ig...
Answer-2:
class Solution {
public:
bool VFinder(vector<vector<int>>& matrix, int hstart, int hend, int vstart, int vend, int ele)
{
if(vstart > vend || hstart > hend)
return false;
int mid = vstart;
int start = vstart;
int end = vend;
while(start<=end)
{
mid = (int)((start + end) / 2);
if (start == end)
{
if (matrix[mid][hstart] == ele)
{
return true;
}
else
{
return HFinder(matrix, hstart + 1, hend, vstart, mid, ele);
}
}
else
{if(matrix[mid][hstart] <ele && matrix[mid+1][hstart] > ele)
return HFinder(matrix, hstart+1, hend, vstart, mid,ele);
else if(matrix[mid][hstart] <ele)
start = mid+1;
else if(matrix[mid][hstart] >ele)
end = mid-1;
else if(matrix[mid][hstart] == ele)
return true;}
}
return false;
}
bool HFinder(vector<vector<int>>& matrix, int hstart, int hend, int vstart, int vend, int ele)
{
if(hstart > hend || vstart > vend)
return false;
int mid = hstart;
int start = hstart;
int end = hend;
while(start<=end)
{
mid = (int)((start + end) / 2);
if (start == end)
{
if (matrix[vstart][mid] == ele)
{
return true;
}
else
{
return VFinder(matrix, hstart, mid, vstart + 1, vend, ele);
}
}
else
{if(matrix[vstart][mid] <ele && matrix[vstart][mid+1] > ele)
return VFinder(matrix, hstart, mid, vstart+1, vend, ele);
else if(matrix[vstart][mid] <ele)
start = mid+1;
else if(matrix[vstart][mid] >ele)
end = mid-1;
else if(matrix[vstart][mid] == ele)
return true;}
}
return false;
}
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int horizontal = matrix[0].size();
int vertical = matrix.size();
return HFinder(matrix, 0, horizontal-1, 0, vertical-1, target);
}
};
So the first code after submission took about : 328 ms
And the second one took about : 488 ms
But in second one, I am after each step reducing the search space, so it should have taken less time as compared to the first code, but it is not happening so.
Can someone please tell me why is it so?
And also what is the time complexity of the second code?

C++: Value of pointer changes "randomly"

I'm creating a function that creates a map from an image with a maze in it.
It takes the image, goes to every pixel and decides if it has to create a node there. One rule is, that when it finds a white pixel at one of the borders it will either be the start or the end node. To tell my pathfinding function where to start, I push the node into a vector(like all other nodes, too) and save the address of that node in a (double)pointer. My problem is, at the end of the loop, the variables in this node change their values(a bool variable might get the value 133) I actually never modify most of the members in this loop. I really have no idea why this is the case...
For examle:
After assigning the node to the pointer:
Completed: false
DistanceTo: 4294967295
PreviousNode: 0x0
XPos: 3
YPos: 0
m_vConnections: <0 Elements>
After function finished:
Completed: 8
DistanceTo: 32674
PreviousNode: SomeAddress
XPos: 3
YPos: 0
m_vConnections: <0 Elements>
The node at SomeAddress actually has completely screwed values, too, but I suspect that the address just changed and it now interprets the data found there as a node.
Sometimes m_vConnections becomes "inaccesseble" which results in sigsegvs when I try to connect something to it.
My function:
bool CreateGraph(const sf::Image &mMaze, std::vector<dijkstra::CNode> *vGraph, dijkstra::CNode **mStart, dijkstra::CNode **mEnd, SMazeCol mColors)
{
*mStart = 0;
unsigned short nExits = 0;
//Get Maze Colours
sf::Color mWallColor = mColors.Wall;
sf::Color mPathColor = mColors.Path;
//Create nodes
for(unsigned int y = 0; y < mMaze.getSize().y; ++y)
{
for(unsigned int x = 0; x < mMaze.getSize().x; ++x)
{
if(mMaze.getPixel(x, y) == mPathColor) //Current pixel is a path
{
bool bTop = false;
bool bBottom = false;
unsigned short nNeighbours = 0;
//Check surroundings of pixel
if(y != 0 && mMaze.getPixel(x, y - 1) == mPathColor)
{
bTop = true;
++nNeighbours;
}
if(y != mMaze.getSize().y - 1 && mMaze.getPixel(x, y + 1) == mPathColor)
{
bBottom = true;
++nNeighbours;
}
if(x != 0 && mMaze.getPixel(x - 1, y) == mPathColor)
{
++nNeighbours;
}
if(x != mMaze.getSize().x - 1 && mMaze.getPixel(x + 1, y) == mPathColor)
{
++nNeighbours;
}
//Decide if a node has to be created at that pixel
if(x == 0 || y == 0 || x == mMaze.getSize().x - 1 || y == mMaze.getSize().y - 1)
{
dijkstra::CNode mNode;
mNode.XPos = x;
mNode.YPos = y;
vGraph->push_back(mNode);
if(*mStart == 0)
{
*mStart = &vGraph->back();
++nExits;
}
else
{
*mEnd = &vGraph->back();
++nExits;
}
}
else if(nNeighbours == 2 && bTop != bBottom)
{
dijkstra::CNode mNode;
mNode.XPos = x;
mNode.YPos = y;
vGraph->push_back(mNode);
}
else if(nNeighbours > 2)
{
dijkstra::CNode mNode;
mNode.XPos = x;
mNode.YPos = y;
vGraph->push_back(mNode);
}
}
}
}
if(nExits != 2)
return false;
The CNode class:
struct SConnection
{
SConnection(CNode *To, unsigned int Distance);
CNode *To;
unsigned int Distance;
};
class CNode
{
public:
CNode();
std::vector<SConnection> Connections()const;
bool AddConnection(CNode *mTo, unsigned int nDistance);
bool AddConnection(const SConnection &mConnection);
bool RemoveConnection(CNode *mTo);
bool operator > (const CNode &rhs)const;
bool operator < (const CNode &rhs)const;
bool operator == (const CNode &rhs)const;
CNode* Addr();
bool Completed;
unsigned int DistanceTo;
CNode *PreviousNode;
unsigned int XPos, YPos;
private:
std::vector<SConnection> m_vConnections;
};
Implementation:
SConnection::SConnection(CNode *To, unsigned int Distance)
{
this->Distance = Distance;
this->To = To;
}
CNode::CNode()
:Completed(false), DistanceTo(std::numeric_limits<unsigned int>::max()), PreviousNode(0)
{
}
std::vector<SConnection> CNode::Connections() const
{
return m_vConnections;
}
bool CNode::AddConnection(CNode *mTo, unsigned int nDistance)
{
if(mTo == 0)
return false;
if(mTo == this)
return false;
for(auto &it : m_vConnections)
{
if(it.To == mTo)
return false;
}
m_vConnections.push_back({mTo, nDistance});
mTo->m_vConnections.push_back({this, nDistance});
return true;
}
bool CNode::AddConnection(const SConnection &mConnection)
{
return (AddConnection(mConnection.To, mConnection.Distance));
}
bool CNode::RemoveConnection(CNode *mTo)
{
for(auto it = m_vConnections.begin(); it != m_vConnections.end(); ++it)
{
if(it->To == mTo)
{
m_vConnections.erase(it);
for(auto it2 = mTo->m_vConnections.begin(); it2 != m_vConnections.end(); ++it2)
{
if(it2->To == this)
mTo->m_vConnections.erase(it2);
}
}
}
return false;
}
bool CNode::operator >(const CNode &rhs)const
{
return DistanceTo > rhs.DistanceTo;
}
bool CNode::operator <(const CNode &rhs) const
{
return DistanceTo < rhs.DistanceTo;
}
bool CNode::operator ==(const CNode &rhs)const
{
return DistanceTo == rhs.DistanceTo;
}
CNode *CNode::Addr()
{
return this;
}
Your main problem is here:
dijkstra::CNode mNode;
//...
vGraph->push_back(mNode);
if(*mStart == 0)
{
*mStart = &vGraph->back();
++nExits;
}
else
{
*mEnd = &vGraph->back();
++nExits;
}
}
else if(nNeighbours == 2 && bTop != bBottom)
{
dijkstra::CNode mNode;
//...
vGraph->push_back(mNode);
}
else if(nNeighbours > 2)
{
dijkstra::CNode mNode;
//...
vGraph->push_back(mNode);
}
Local variables are not placed in global heap, and can be forgotten by programm soon after the code block where they were created is finished.
The better way to improve your code is to rewrite function declaration as
bool CreateGraph(const sf::Image &mMaze, std::vector<dijkstra::CNode*> *vGraph, dijkstra::CNode **mStart, dijkstra::CNode **mEnd, SMazeCol mColors)
And inside function change all:
dijkstra::CNode mNode
vGraph->push_back(mNode);
to
std::shared_ptr<dijkstra::CNode> mNode =
std::shared_ptr<dijkstra::CNode>(new dijkstra::CNode(X, Y));
vGraph->push_back(mNode.get());

Kruskal's Algorithm in O(n log n)

I'm interested in learning how to run Kruskal's algorithm on O(n log n) in c++. I have implemented the algorithm with a solution that runs on O(n^2), and the code for that is below.
I know that it should be possible to run Kruskal's on O(n log n), but I'm stymied as to how this can be done. I would appreciate any and all tips.
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
//sort by weight
bool sorting (vector<int> i, vector<int> j) {return i[2]<j[2];}
class Submap {
private:
set<int> finder;
public:
Submap(int x) {finder.insert(x);}
Submap(set<int> x) {finder = x;}
set<int> pointreturn() {return finder;}
//function to add new set to current tree
void add(set<int> np) {
finder.insert(np.begin(), np.end());
}
void add(int n) {
finder.insert(n);
}
int size() {return int(finder.size());}
//find function returns true if the value is not found
bool find(int x) {
if (finder.find(x) == finder.end())
return true;
else
return false;
}
};
class Map {
private:
vector<Submap> submaps;
public:
int find(int x) {
int finder = -1;
for(int i = 0; i < int(submaps.size()); i++) {
if(!submaps[i].find(x)) {
finder = i;
break;
}
}
return finder;
}
void newsubmap(int a, int b) {
set<int> nextset;
nextset.insert(a);
nextset.insert(b);
submaps.push_back(Submap(nextset));
}
void addendum(int a, int index) {
submaps[index].add(a);
}
Submap subfind(int i) {return submaps[i];}
void fuse(int index1, int index2) {
submaps[index1].add(submaps[index2].pointreturn());
vector<Submap> nextmaps;
for(int i = 0; i < int(submaps.size()); i++) {
if (i != index2)
nextmaps.push_back(submaps[i]);
}
submaps = nextmaps;
}
int size() {return submaps[0].size();}
};
Map kruskal (vector<vector<int>> &graph, int weight, int junct) {
//sort the graph
sort(graph.begin(), graph.end(), sorting);
Map currmap;
int usedweight = 0;
for(int i=0; i<graph.size(); i++) {
int a = currmap.find(graph[i][0]);
int b = currmap.find(graph[i][1]);
//the boolean expression here is false if both points are already in the same submap
if(a != b || a == -1) {
usedweight += graph[i][2];
//if neither point is in the map so far
if(a == -1 && b == -1) {
currmap.newsubmap(graph[i][0], graph[i][1]);
}
//if one point is in the map so far
else if (a != -1 && b == -1) {
currmap.addendum(graph[i][1], a);
}
else if (a == -1 && b != -1) {
currmap.addendum(graph[i][0], b);
}
//if both points are in the map, but different submaps
else {
currmap.fuse(a, b);
}
}
//if the first set in the map is spanning, the algorithm is done
if(currmap.size() == junct)
break;
}
return (currmap);
}

Dictionary function with BST returns vector

The function dictionary_select returns a vector which contains words that are starting with W.
In this code dictionary_select gives an error message. But I can not find it. Is there anybody can help me?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct tree {
string data;
tree *left;
tree *right;
};
typedef tree* dictionary;
bool dictionary_ins_word(dictionary & D, const string & W)
{
if(W == "" || W == " ")
return false;
tree* dic;
dic = new tree;
dic->data = W;
dic->left = NULL;
dic->right = NULL;
if(D == NULL) {
D = dic;
}
else {
if(W <= D->data)
dictionary_ins_word(D->left, W);
else
dictionary_ins_word(D->right, W);
}
return true;
}
bool dictionary_lookup(const dictionary & D, const string & W)
{
if(W == "" || W == " ")
return false;
if(D == NULL)
return false;
if(W == D->data)
return true;
else if (W < D->data)
return dictionary_lookup(D->left, W);
else
return dictionary_lookup(D->right, W);
}
bool dictionary_is_empty(const dictionary & D)
{
if(D == NULL)
return true;
else
return false;
}
bool dictionary_del_word(dictionary & D, const string & W)
{
if(!dictionary_lookup(D, W))
return false;
if(W < D->data)
dictionary_del_word(D->left, W);
else if(W > D->data)
dictionary_del_word(D->right, W);
else {
string item;
tree* temp;
temp = D;
if(D->left == NULL) {
D = D->right;
delete temp;
}
else if(D->right == NULL) {
D = D->left;
delete temp;
}
else {
while(D->left->right != NULL)
D->left = D->left->right;
item = D->left->data;
D->data = item;
dictionary_del_word(D->left, W);
}
}
return true;
}
bool dictionary_min(string & W, const dictionary & D)
{
dictionary min;
if(D == NULL)
return false;
min = D;
while(min->left != NULL)
min = min->left;
W = min->data;
return true;
}
vector <string> dictionary_select(const dictionary & D, const string & W)
{
vector < string > result;
vector < string > zeroVec;
string temp;
zeroVec.push_back("");
if(D == NULL)
return zeroVec;
temp = D->data;
size_t found = temp.find(W);
if(found == 0)
result.push_back(D->data);
if(W <= D->data)
return dictionary_select(D->left, W);
else
return dictionary_select(D->right, W);
}
int main()
{
bool b[5];
dictionary l;
string W, str;
vector <string> vec;
l = new tree;
l->data = "berdi";
l->left = NULL;
l->right = NULL;
b[0] = dictionary_ins_word(l, "atas");
b[1] = dictionary_ins_word(l, "cara");
b[2] = dictionary_ins_word(l, "ata");
b[3] = dictionary_ins_word(l, "atax");
vec = dictionary_select(l, "ata");
for(int i=0; i<vec.size(); i++) {
cout << vec[i] << " ";
}
getchar();
return 0;
}
The problem is your function dictionary_select declares the result vector and never returns it. Below is how you would change this:
vector <string> dictionary_select(const dictionary & D, const string & W)
{
vector < string > result;
vector < string > tempVector; // add this to store the result from the recursion
vector < string > zeroVec;
string temp;
zeroVec.push_back("");
if(D == NULL)
return zeroVec;
temp = D->data;
size_t found = temp.find(W);
if(found == 0)
result.push_back(D->data);
if(W <= D->data)
tempVector = dictionary_select(D->left, W); // get the recursion result
else
tempVector = dictionary_select(D->right, W); // get the recursion result
result.insert(result.end(), tempVector.begin(), tempVector.end()); // append all the results
return result; // return the result
}
UPDATE
In order for the function to return the correct data, you have to also ensure that you look at both left and right sides of the tree to get the appropriate results. The function below has the update:
vector <string> dictionary_select(const dictionary & D, const string & W)
{
vector < string > result;
vector < string > tempVectorLeft; // add this to store the result from the left recursion
vector < string > tempVectorRight; // add this to store the result from the right recursion
vector < string > zeroVec;
string temp;
zeroVec.push_back("");
if(D == NULL)
return zeroVec;
temp = D->data;
size_t found = temp.find(W);
if(found == 0)
result.push_back(D->data);
if(found == 0 || W <= D->data)
tempVectorLeft = dictionary_select(D->left, W); // store results
if(found == 0 || W > D->data)
tempVectorRight = dictionary_select(D->right, W); // store results
result.insert(result.end(), tempVectorLeft.begin(), tempVectorLeft.end()); // append all the left results
result.insert(result.end(), tempVectorRight.begin(), tempVectorRight.end()); // append all the right results
return result;
}
I propose you the following variant:
vector <string> dictionary_select(const dictionary & D, const string & W)
{
vector < string > result;
vector < string > zeroVec;
if (D == NULL)
return zeroVec;
string temp = D->data;
size_t found = temp.find(W);
if (found == 0)
result.push_back(D->data);
if (found || W <= D->data)
for(auto x: dictionary_select(D->left, W))
result.push_back(x);
if (found || W > D->data)
for (auto x : dictionary_select(D->right, W))
result.push_back(x);
return result;
}
The problem in the if / else approach used in you initial algortihm to choose between recureion on left or right node, is that you could have a note like this:
atar
/ \
ata atax
So if the data in the head of the node matches, you need to check both left and right to be sure that you're not missing something.