-I wrote a program to find the shortest path from a source node to a target node. Everything is fine, the program found the shortest path. But i have a problem, that is not able to print or get each node in the path. I tried many ways but no result. Hope anyone can help me, thanks everyone.
///////////////////////////////
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
typedef struct Node
{
int vertex;
int g;
int h;
int f;
Node* parent;
Node(int vertex)
{
this->vertex = vertex;
this->g = 0;
this->h = 0;
this->f = 0;
this->parent=NULL;
}
Node(int vertex,int g, int h, int f,Node*parent)
{
this->vertex = vertex;
this->g = g;
this->h = h;
this->f = f;
this->parent = parent;
}
}Node;
struct Edge
{
int source;
int dest;
int g;
int h;
};
struct comp
{
bool operator()(const Node* lhs, const Node* rhs) const {
return lhs->f < rhs->f;
}
};
std::vector<Node*>openList;
std::vector<Node*>closeList;
Node* startPos;
Node* endPos;
static const int WeightW = 10;
class Graph
{
public:
std::vector<std::vector<Edge>>adjlist;
Graph(const std::vector<Edge>& edges, int N)
{
adjlist.resize(N);
for (auto &edge:edges)
{
adjlist[edge.source].push_back(edge);
}
}
};
int isContains(std::vector<Node*>* Nodelist, int vertex);
void printPath(Node*node);
void findShortestPath(const Graph& grap,Node* start,Node* end, int N)
{
Node* node;
openList.push_back(start);
while (openList.size()>0)
{
node = openList[0];
closeList.push_back(node);
openList.erase(openList.begin());
std::cout << "start" << std::endl;
int u = node->vertex;
std::cout << "V: " << u << " g :" << node->g << std::endl;
std::cout << "continous" << std::endl;
for (auto v : grap.adjlist[u])
{
if (v.dest == end->vertex)
{
std::cout << "FindNode " << v.dest << std::endl;
printPath(node);
return;
}
if (isContains(&closeList, v.dest) == -1)
{
if (isContains(&openList, v.dest) == -1)
{
int vertex = v.dest;
std::cout <<"V: "<< vertex << std::endl;
int h = v.h;
int currentg = node->g + v.g;
int f = currentg + h;
std::cout <<"vertext: "<<vertex<< " h: " << h << " g: " << currentg << " f: " << f << std::endl;
Node* newNode = new Node(vertex, currentg, h, f,node->parent);
openList.push_back(newNode);
}
}
}
std::cout<<"Close: ";
for (size_t i = 0; i < closeList.size(); i++)
{
std::cout << closeList[i]->vertex << " ";
}
std::cout << std::endl;
sort(openList.begin(), openList.end(),comp());
std::cout << "Open: ";
for (size_t i = 0; i < openList.size(); i++)
{
std::cout << openList[i]->vertex << " ";
}
std::cout << std::endl;
std::cout << "end" << std::endl;
std::cout << std::endl;
}
}
void printPath(Node* node)
{
std::cout << std::endl;
if (node->parent != NULL)
printPath(node->parent);
std::cout << node->vertex << " ";
}
int isContains(std::vector<Node*>* Nodelist,int vertex)
{
for (int i = 0; i < Nodelist->size(); i++)
{
if (Nodelist->at(i)->vertex== vertex)
{
return i;
}
}
return -1;
}
int main()
{
//{Node,Node,G,H}
//Firt Node
//second Node
//G is the movement cost to move from the starting point to a given square on the grid
// following the path generated to get there
//H is the estimated movement cost to move from that given square on the grid to the final destination
std::vector<Edge>edges =
{
{0,1,5,17},
{0,2,5,13},
{1,0,5,16},
{1,3,3,16},
{1,2,4,13},
{2,0,5,16},
{2,1,4,17},
{2,3,7,16},
{2,4,7,16},
{2,7,8,11},
{3,2,7,13},
{3,7,11,11},
{3,10,16,4},
{3,11,13,7},
{3,12,14,10},
{4,2,7,13},
{4,5,4,20},
{4,7,5,11},
{5,4,4,16},
{5,6,9,17},
{6,5,9,20},
{6,13,12,7},
{7,3,11,16},
{7,4,5,16},
{7,8,3,10},
{8,7,3,11},
{8,9,4,8},
{9,8,4,10},
{9,13,3,7},
{9,15,8,0},
{10,3,16,16},
{10,11,5,7},
{10,13,7,7},
{10,15,4,0},
{11,3,13,16},
{11,10,5,4},
{11,12,9,10},
{11,14,4,5},
{12,3,14,16},
{12,11,9,7},
{12,14,5,5},
{13,9,3,8},
{13,10,7,4},
{13,15,7,0},
{14,11,4,7},
{14,12,5,10},
{15,9,8,8},
{15,10,4,4},
{15,13,7,7},
};
int n = edges.size();
Graph grap(edges, n);
//std::cout << h << std::endl;
Node* start = new Node(0);
Node* end = new Node(15);
findShortestPath(grap, start, end, n);
//Astar astar;
//Node* startPos = new Node(5, 1);
//Node* endPos = new Node(1, 8);
//astar.printMap();
//astar.search(startPos, endPos);
//cout << endl;
//astar.printMap();
system("pause");
return 0;
}
Your program doesn't find the shortest path. It gives the wrong output. (you're on the right track though)
I will assume you are trying to find the shortest path by using BFS. Let's take a look at line 113:
sort(openList.begin(), openList.end(),comp());
Here you're sorting your BFS queue (vector in your case) and thus destroying the right order.
Delete that line.
Congrats, now your program finds the shortest path!
Next, as I understand, for each node you branch into, you remember which node you came from in order to backtrack the path once you reach the destination or final node.
In line 102:
Node* newNode = new Node(vertex, currentg, h, f,node->parent);
you are assigning the new node's grandparent instead of parent. Change that line to
Node* newNode = new Node(vertex, currentg, h, f,node);
Now your printPath function works properly and prints the right path. (just add the target node)
Anyways, your code has a lot of space for improvements. Check out other implementations online and try to see if you can code it as short and clean for practice. Good luck!
This program reads a CSV file and enters it into a binary search tree. So far I have managed to insert a new node, put it in order, however, internally, perform a search asking for the Varibale Key but I have not managed to get it to work, could someone help me?
The CSV file is reading a file that includes:
1, name1,123456
2, name2,165151
3, name3,1566516
#include <iostream>
#include <iomanip>
#include <fstream>
#include <memory>
#include <string>
#include <sstream>
#include <vector>
struct Person {
int key;
std::string name;
int num;
};
struct Node : Person {
Node(const Person &person) : Person(person) {}
std::unique_ptr<Node> left, right;
void insert(const Person &person);
};
void Node::insert(const Person &person) {
/* recur down the tree */
if (key > person.key) {
if (left)
left->insert(person);
else
left = std::make_unique<Node>(person);
} else if (key < person.key) {
if (right)
right->insert(person);
else
right = std::make_unique<Node>(person);
}
}
std::vector<Person> persons;
void inorder(Node *root) {
if (root) {
// cout<<"\t";
inorder(root->left.get());
std::cout << '\t' << root->key << ' ' << root->name << ' ' << root->num << '\n';
inorder(root->right.get());
}
}
Node *minValueNode(Node *node) {
Node *current = node;
/* loop down to find the leftmost leaf */
while (current && current->left) current = current->left.get();
return current;
}
int main() {
std::unique_ptr<Node> root;
std::ifstream fin("data.txt");
if (!fin) {
std::cout << "File not open\n";
return 1;
}
std::string line;
const char delim = ',';
while (std::getline(fin, line)) {
std::istringstream ss(line);
Person person;
ss >> person.key;
ss.ignore(10, delim);
std::getline(ss, person.name, delim);
ss >> person.num;
if (ss) persons.push_back(person);
}
for (unsigned int i = 0; i < persons.size(); i++) {
std::cout << std::setw(5) << persons[i].key << std::setw(20)
<< persons[i].name << std::setw(15) << persons[i].num << '\n';
if (!root) root = std::make_unique<Node>(persons[i]);
else root->insert(persons[i]);
}
std::cout << "\n\nInorder:\n";
// cout<<node.name;
inorder(root.get());
return 0;
}
/*bool busqueda(Node *root, int dato)
{
if(root){
return false;
}
else if(root->key==dato){
return true;
}
else if(dato<root->key){
busqueda(root->left.get(),dato);
}
else{
return busqueda(root->right.get(),dato);
}
}*/
Presuming that this is the function you need help with:
bool busqueda(Node *root, int dato) {
if(root){
return false;
}
else if(root->key==dato){
return true;
}
else if(dato<root->key){
busqueda(root->left.get(),dato);
}
else{
return busqueda(root->right.get(),dato);
}
}
There are a few issues here:
if(root) {
This is testing if root is not nullptr, so you're going to immediately bail if root points at something. Meaning, you will immediately return false if any tree is provided, which is the opposite of what you want. Even worse, if root is null you will proceed and try to dereference a null pointer, which will crash the program. Change this line to if (root != nullptr) {.
else if(dato<root->key){
busqueda(root->left.get(),dato);
}
This recursive call looks right except you don't return the result meaning that you will reach the end of a function returning bool without actually returning anything meaningful. Just add return before this function call.
You can mostly express this function using boolean logic as well:
bool busqueda(Node *root, int dato) {
return root != nullptr && (
root->key == dato ||
busqueda((dato < root->key ? root->left : root->right).get(), dato)
);
}
This makes it impossible to forget to return something.
As a side note, enable all compiler warnings -- it would have warned you about reaching the end of a non-void function without returning a value.
Here's my stack:
template <class Type>
struct Node {
Type value;
Node* next;
Node(Type n_value)
: value(n_value), next(nullptr)
{ }
static Node<Type>* Add(Node<Type>* head, const Type value) {
if (!head) { head = new Node<Type>(value); }
else { head->next = Add (head->next, value); }
return head;
}
static void Remove(Node<Type>* head) {
if (head->next) { Remove(head->next); }
else { delete head; }
}
static void Empty(Node<Type>* head) {
if (head->next) { Empty(head->next); }
delete head;
}
static void Display(const Node<Type>* head) {
std::cout << "\nBEGINNING\n";
std::function<void(const Node<Type>*)> DisplayElements = [&] (const Node<Type>* head) {
if (head) {
std::cout << head->value << std::endl;
if (head->next) { DisplayElements(head->next); }
}
};
DisplayElements(head);
std::cout << "\nEND\n";
}
};
With following code:
auto beg = Node<int>::Add(nullptr, 0);
for (int i = 0; i < 10; i++) { Node<int>::Add(beg, i); }
Node<int>::Display(beg);
Node<int>::Empty(beg);
Everything is okay:
BEGINNING
0
0
1
2
3
4
5
6
7
8
9
END
Program ended with exit code: 0
But when I try to remove something:
auto beg = Node<int>::Add(nullptr, 0);
for (int i = 0; i < 10; i++) { Node<int>::Add(beg, i); }
Node<int>::Remove(beg);
Node<int>::Remove(beg);
Node<int>::Display(beg);
Node<int>::Empty(beg);
return 0;
I get Pointer being freed was not allocated on delete head; in Remove. Why? It's absolutely the same as Empty (which erases whole list), except it deletes only last node. Empty works fine.
Empty is needed to delete whole list (so that we don't get memory leaks), and Remove is to remove last element.
I am unable to reproduce your results, so instead of an answer, I am providing a nudge on how you might proceed.
I changed the parameter of DisplayElements to 'lhead' (for local head), because the 'head' declaration shadow's the one in the outer scope. You also have some other shadows ... so I recommend you add -Wshadow to your compiler settings.
I also added to your Display() function some diagnostic hex output.
static void Display(const Node<Type>* head)
{
std::cout << "\nBEGINNING\n";
std::function<void(const Node<Type>*)> DisplayElements =
[&] (const Node<Type>* lhead)
{
if (lhead)
{ // vvvvvvvvvvvv--diagnostic only
std::cout << hex << lhead << " " << dec
<< lhead->m_value << std::endl;
if (lhead->m_next)
{
DisplayElements(lhead->m_next);
}
}
};
DisplayElements(head);
std::cout << "\nEND\n";
}
And in addition ... I changed the following. By avoiding the Empty() and the other couple of lines, the first display will more closely match the second:
auto beg = Node<int>::Add(nullptr, 0);
{
for (int i = 0; i < 10; i++) { Node<int>::Add(beg, i); }
Node<int>::Display(beg);
// Node<int>::Empty(beg);
}
{
// auto beg = Node<int>::Add(nullptr, 0);
// for (int i = 0; i < 10; i++) { Node<int>::Add(beg, i); }
Node<int>::Remove(beg);
Node<int>::Remove(beg);
Node<int>::Display(beg);
Node<int>::Empty(beg);
}
Now consider connecting the debugger and setting a break point to inspect what "Remove()" is doing.
Don't be shy about adding more diagnostic cout's.
What I notice is:
a) that the last lines of output are an infinite loop.
b) outputs for values 1..8 are identical to the first,
c) the output for value 9 is missing ... and I suppose should be ... but something about that last Node (perhaps the one with 8?) must be confused.
I am trying to insert nodes in a binary tree recursively, but the code is only doing the root node and it's left and right children. I am trying to figure out how to get past that point.
I have already tried different implementations, using queues, doing level order insert. I believe the problem is due to the fact that in my main function I only call with root, and if that is the problem, how would I go about calling with the left and right childs.
Main Function:
int main() {
treenode* root = new treenode();
for(int a = 1; a < 15; a++) {
insert(root, a);
}
cout << "Height: " << height(root) << endl;
cout << "Printed Tree: " << endl;
for(int a = 0; a <= height(root); a++) {
printGivenLevel(root, a); //Print every level
cout << endl;
}
return 0;
}
Here is my insert function:
void insert(treenode*& node, int val) {
if(node == nullptr) {
node = new treenode(val);
return;
}else{
if(node->left == nullptr) {
insert(node->left, val);
}else{
insert(node->right, val);
}
}
}
A treenode has a value, a left child and right child:
struct treenode {
//The value of the node
int value;
//Pointers to the left and right children
treenode *left, *right;
//Constructor with values;
treenode(int val=0, treenode *l = nullptr, treenode *r = nullptr) : value(val), left(l), right(r) {};
};
I would expect the result to be something like so:
0
1 2
3 4 5 6
7 8 9 10 11 12 13 14
But my actual output is only:
0
1 2
Thanks in advance
... but the code is only doing the root node and it's left and right
children. I am trying to figure out how to get past that point.
I find no error in your treenode::insert, your problem might be in some other code you do not show. For example, you did not provide "height(root)" or "Printed Tree" info. I could not diagnose them.
I have provided some alternatives for various ideas. see "dump()" and "height()" and "size()".
Note: "dump()" is none of pre, in-, or post- order, because the sorted input creates unbalance tree displays (they are worst-case-unbalanced). I found this display the easiest to review.
Perhaps the following will help you diagnose your mistakes. Good luck.
Note the use of "if(false) cout << ...". These are diagnostic output which might provide some insights by enabling them, and / or adding such items to your code.
Did you try your debugger yet?
#include <chrono> // short form names----vvvvvvv
typedef std::chrono::high_resolution_clock HRClk_t; // std-chrono-hi-res-clk
typedef HRClk_t::time_point Time_t; // std-chrono-hi-res-clk-time-point
typedef std::chrono::nanoseconds NS_t; // std-chrono-nanoseconds
using namespace std::chrono_literals; // suffixes like 100ms, 2s, 30us
using std::chrono::duration_cast;
#include <iostream>
using std::cout, std::endl, std::flush;
#include <iomanip>
using std::setw;
namespace DTB
{
class treenode
{
friend class T920_t;
int value; //The value of the node
treenode *left, *right; //Pointers to the left and right children
//Constructor with values;
treenode(int val=0, treenode *l = nullptr, treenode *r = nullptr)
: value(val), left(l), right(r) { ctor('A'); };
void ctor(char kar) {
if (left) left->ctor(kar);
{
if(false) // diagnostic
cout << "\n ctor: " << " " << kar
<< " val: " << setw(3) << value << flush;
}
if(right) right->ctor(kar);
}
void insert ( treenode*& node, int val)
{
if(node == nullptr)
{
node = new treenode(val);
if (false) node->dump(); // diagnostic
return;
}
else
{
if(node->left == nullptr)
{
insert(node->left, val);
if (false) node->dump(); // diagnostic
}
else
{
insert(node->right, val);
if (false) node->dump(); // diaagnostic
}
}
}
int height(int lvl = 1)
{
static int maxHeight = 0;
if (left) left->height (lvl+1);
if(right) right->height (lvl+1);
if (lvl > maxHeight) maxHeight = lvl;
return maxHeight;
}
int size()
{
int count = 1; // this element
if (left) { count += left->size(); };
if(right) { count += right->size(); }
return count;
}
void dump(int lvl=0)
{
if (left) left->dump (lvl+1);
if(right) right->dump (lvl+1);
{
cout << "\n " // << lvl
<< setw(3*lvl) << ' '
<< value << flush;
}
}
}; // class treenode
typedef treenode Node_t; // shorter name for user-defined-type
class T920_t
{
public:
int operator()(int argc, char* argv[]) { return exec(argc, argv); }
private:
int exec(int , char** )
{
int retVal = 0;
Time_t start_ns = HRClk_t::now();
Node_t* root = new Node_t(); // 1st node
for(int v = 1; v < 21; ++v) { // 20 more
root->insert(root, v);
}
cout << "\n\n size : " << root->size() // element count
<< " maxHeight : " << root->height()
<< endl;
dumpAll(root);
for(int v = 1; v < 11; ++v) { // 10 more
root->insert(root, v);
}
cout << "\n\n size : " << root->size() // element count
<< " maxHeight : " << root->height()
<< endl;
dumpAll(root);
auto duration_ns = duration_cast<NS_t>(HRClk_t::now() - start_ns).count();
cout << "\n\n\n T920_t::exec() duration "
<< duration_ns << " ns "
<< " cpluplus vers : " << __cplusplus << std::endl;
return retVal;
}
void dumpAll (Node_t*& node)
{
cout << "\n dumpAll(): ";
node->dump();
cout << endl;
}
}; // class T920_t
} // namespace DTB
int main(int argc, char* argv[]) { return DTB::T920_t()(argc, argv); }
A partial output is:
size : 21 maxHeight : 11
dumpAll():
1
3
5
7
9
11
13
15
17
19
20
18
16
14
12
10
8
6
4
2
0
size : 31 maxHeight : 16
dumpAll():
1
3
...
10
8
6
4
2
0
T920_t::exec() duration 271095 ns cpluplus vers : 201703
I tried to strictly implement in c++ what I'm studying in algorithmic at the moment, recursive functions with simple linked lists. Here is what I've come by :
#include <iostream>
using namespace std;
class Liste {
private :
int val;
Liste *suivante;
public :
Liste(int val = 0, Liste *suivante = NULL) {
this->val = val;
this->suivante = suivante;
}
void afficherElement() const {
cout << "Adresse : " << this << endl;
cout << "Valeur : " << val << endl;
cout << "Adresse suivante : " << suivante << endl;
cout << endl;
}
int tete() const {
return val;
}
Liste reste() const {
return *suivante;
}
bool estVide() const {
return (suivante == NULL);
}
Liste prefixer(int val) {
Liste *nouvelle = new Liste(val, this);
return *nouvelle;
}
Liste suffixer(int val) {
suivante = new Liste(val);
afficherElement(); // test (last element won't be displayed)
return *suivante;
}
};
int main() {
Liste uneListe(3); // 1st element
uneListe.suffixer(5).suffixer(8).suffixer(10); // adding 3 more
cout << "-----------\n\n";
uneListe.afficherElement(); // displaying 1st element : ok
uneListe.reste().afficherElement(); // displaying 2nd element : pointer is NULL !!???
// uneListe.reste().reste().afficherElement(); --> segmentation fault, predictably enough
return 0;
}
As you can guess, it doesn't work. When I add elements, calling the display method within the add method, elements seem to be well formed although the pointer value and the next element's adress differ (I don't get why).
But, after adding process is done, I try to display the list again, 1st element is well linked with 2nd, but then there is a NULL pointer value. Wonder why ??
I've seen a code with two classes (Node and List), that works fine, but I'd like to know what is wrong with mine. Is it that I'm creating new objects of a class within this same class ?
Thanks,
for right this problem you most change this line
Liste suffixer(int val)
to
Liste* suffixer(int val)
and then change this line
return *suivante;
to
return suivante;
and in main use this line
uneListe.suffixer(5)->suffixer(8)->suffixer(10);
instead of
uneListe.suffixer(5).suffixer(8).suffixer(10);
Your class methods Liste::prefixer(int val) and Liste suffixer(int val) will return a copy of the object created, they should return a pointer to the object (or a reference).
e.g.
Liste *Liste::suffixer(int val){
if(suivante == nullptr)
suivante = new Liste(val);
else
throw std::runtime_error("Generic error message");
return suivante;
}
or
Liste &Liste::suffixer(int val){
... previous inner method ...
return *suivante;
}
Class Liste contains a value and a reference, which is not what a list is: a singly linked list is a pointer to an element containing a value and a pointer to the next node.
You might use a value+pointer element as a list object, ignoring the val member. This would require different coding for some methods, e.g., for tete() and reste().
But, since using
typedef Liste * real_list_type;
is what you have in mind (? - see below), let's look at the methods.
bool estVide() const { return (suivante == NULL); }
This is in contradiction to the real_list_type being a mere List *; if you compare this to method reste(), it actually tests whether the tail is empty, not the list itself! (It would be in sync with using a value+pointer object as the list object.)
Liste suffixer(int val) { suivante = new Liste(val); ... }
This is bad: it replaces suivante with a new object, no matter what's stored in there (a memory leak). You'll have to do
Liste suffixer(int val) {
if( suivante == NULL ){
suivante = new Liste(val);
} else {
suivante->suffixer( val );
}
return *this;
}
LATER
I think that would be the best way to keep it as close to the abstract concept as possible. Note that there is no "isEmpty" - this is done by a test whether the List * variable representing the list equals NULL, but you can't have a method for that.
template<typename T>
class List {
public:
List( T v, List* t = nullptr ) : value(v), next(t){}
~List(){ delete next; }
List* prepend( T v ){
return new List( v, this );
}
List* append( T v ){
if( next == nullptr ){
next = new List( v );
} else {
next->append( v );
}
return this;
}
T head(){ return value; }
List* tail(){ return next; }
void dump(){
List* curr = this;
std::string del = "";
while( curr != nullptr ){
std::cout << del << curr->value;
del = ", ";
curr = curr->next;
}
std::cout << std::endl;
}
private:
T value;
List* next;
};
int main(){
typedef List<int> * intList;
intList list = new List<int>( 1 );
list->append( 2 )->append( 3 );
list->dump();
}
Here is the "fixed" version of my first attempt :
#include <iostream>
using namespace std;
class Liste {
private :
int val;
bool vide;
Liste *suivante;
public :
Liste(int val = 0, bool vide = true, Liste *suivante = NULL) {
this->val = val;
this->vide = vide;
this->suivante = suivante;
}
void afficherElement() const {
cout << "Adresse : " << this << endl;
cout << "Valeur : " << val << endl;
cout << "Vide : " << vide << endl;
cout << "Adresse suivante : " << suivante << endl;
cout << endl;
}
int head() const {
return val;
}
Liste *reste() const {
return suivante;
}
bool estVide() const {
return vide;
}
Liste *prefixer(int val) {
Liste *nouvelle = new Liste(val, this);
return nouvelle;
}
Liste *suffixer(int val) {
if(suivante == NULL) {
suivante = new Liste(val);
vide = false;
}
return suivante;
}
};
void afficherListe(Liste *uneListe) {
(*uneListe).afficherElement();
if(!(*uneListe).estVide()) {
afficherListe((*uneListe).reste());
}
}
int main() {
Liste *test = new Liste(3);
(*test).suffixer(5);
afficherListe(test);
return 0;
}
As expected it's awfully unpractical.
Laune's solution looks good...
However, the whole thing is bizarre, I suppose I'd be better off sticking with the regular List/Nodes way. Definitely gonna talk about that with my teacher.