[英]DFS implementation in C++
I have written below code.我写了下面的代码。 However I am not sure if I have inserted my tree correctly.但是我不确定我是否正确插入了我的树。 The code compiles sucessfully but I don't get the DFS traversed array in the output. Can someone tell me where I am going wrong?代码编译成功,但我没有得到 output 中的 DFS 遍历数组。有人能告诉我哪里出错了吗?
The input tree is below but please let me know if I have made the correct calls in the main function?输入树在下面,但如果我在主 function 中进行了正确的调用,请告诉我? As my output is coming - [ab c defghijk].因为我的 output 即将到来 - [ab c defghijk]。 whereas the correct output should be [abefij c dgkh].而正确的 output 应该是 [abefij c dgkh]。
A
/ \ \
B C D
/ \ / \
E F G H
/ \ \
I J K
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
string name;
vector <Node *> children;
Node(string name)
{
this->name = name;
}
//O(v+e) time | O(v) space
vector <string> depthFirstSearch(vector<string> *array)
{
array->push_back(this->name);
for(size_t i = 0; i < this->children.size(); i++)
children[i]->depthFirstSearch(array);
return *array;
}
Node *addChild(string name)
{
Node *child = new Node(name);
children.push_back(child);
return this;
}
};
int main()
{
Node n1("a");
n1.addChild("b");
n1.addChild("c");
n1.addChild("d");
n1.children[0]->addChild("e");
n1.children[0]->addChild("f");
n1.children[0]->children[1]->addChild("i");
n1.children[0]->children[1]->addChild("j");
n1.children[2]->addChild("g");
n1.children[2]->addChild("h");
n1.children[2]->children[0]->addChild("k");
vector <string> array;
n1.depthFirstSearch(&array);
for (size_t i = 0; i< array.size(); i++)
cout<<array[i]<<' ';
}
Note - Thanks for the explanation, but I made some edits in the way I constructed the tree in the main function and continued using return this statement and it worked.注意 - 感谢您的解释,但我对在主 function 中构建树的方式进行了一些编辑,并继续使用 return this 语句并且它起作用了。
The error is actually simple but not where it might be expected.该错误实际上很简单,但不是预期的错误。
The Node::addChild()
returns this
: Node::addChild()
返回this
:
Node *addChild(string name)
{
Node *child = new Node(name);
children.push_back(child);
return this; // <-- OUCH!
}
So, when used in main()
:因此,在main()
中使用时:
Node n1("a");
Node *n2 = n1.addChild("b"); // => n2 = &n1;
n1.addChild("c");
Node *n4 = n1.addChild("d"); // => n4 = &n1;
n2->addChild("e");
Node *n3 = n2->addChild("f"); // => n3 = n2 = &n1;
n3->addChild("i");
n3->addChild("j");
Node *n5 = n4->addChild("g"); // => n5 = n4 = &n1;
n4->addChild("h");
n5->addChild("k");
So, the tree actually does not get the expected shape but rather something like:所以,这棵树实际上并没有得到预期的形状,而是类似于:
A________________
/ \ \ \ \ \ \ \ \ \
B C D E F G H I J K
for what the current output of OP is fully correct.对于当前 output 的 OP 是完全正确的。
The fix is simple:修复很简单:
Node *addChild(string name)
{
Node *child = new Node(name);
children.push_back(child);
return child;
}
Output: Output:
a b e f i j c d g k h
Live Demo on colirucoliru 现场演示
OP insisted in that Node::addChild()
has to return this;
OP 坚持认为Node::addChild()
必须return this;
and asked for another way to circumvent the issue.并要求另一种方法来规避这个问题。 I tried to convince OP that return child;
我试图说服 OP return child;
would make more sense than return this;
比return this;
. .
Actually, I intuitively expected that Node::addChild()
would return the the created child which costed me some extra debugging to find the actual bug.实际上,我直觉地期望Node::addChild()
会返回创建的子节点,这让我花费了一些额外的调试时间来查找实际的错误。 :-) :-)
Concerning关于
the way I have constructed my tree in the main function - is it the right way or there can be more efficient way of doing that我在主 function 中构建我的树的方式 - 这是正确的方式还是可以有更有效的方式来做到这一点
I personally find the code in main()
not that bad as it is.我个人认为main()
中的代码并没有那么糟糕。
However, there are often more than one way to Rome.然而,通往罗马的道路往往不止一种。
So, here is just another idea:所以,这只是另一个想法:
A second constructor to construct child nodes explicitly:显式构造子节点的第二个构造函数:
Node(Node &parent, string name): Node(name)
{
parent.children.push_back(this);
}
which allows to write the tree init.这允许编写树初始化。 in main()
like this:在main()
中是这样的:
Node nA("a");
Node nB(nA, "b");
Node nC(nA, "c");
Node nD(nA, "d");
Node nE(nB, "e");
Node nF(nB, "f");
Node nI(nF, "i");
Node nJ(nF, "j");
Node nG(nD, "g");
Node nH(nD, "h");
Node nK(nG, "k");
( Node::addChild()
is even not used/needed in this case.) (在这种情况下甚至不使用/不需要Node::addChild()
。)
Output: Output:
a b e f i j c d g k h
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.