[英]Depth first search with smart pointers for a binary search tree
This is my first time implementing DFS with smart pointers. 这是我第一次使用智能指针实现DFS。 I get this unknown error thrown:
我得到了这个未知的错误:
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools
\msvc\14.15.26726\include\xmemory0(881): error C2664: 'Node::Node(Node &&)':
cannot convert argument 1 from 'std::unique_ptr<Node,std::default_delete<_Ty>>' to 'const int &'
I am not sure how to go about fixing this issue. 我不知道如何解决这个问题。 Here is my code:
这是我的代码:
#include <algorithm>
#include <iostream>
#include <memory>
#include <utility>
#include <stack>
#include <queue>
struct Node {
int data;
std::unique_ptr<Node> left = nullptr;
std::unique_ptr<Node> right = nullptr;
Node(const int& x, std::unique_ptr<Node>&& p = nullptr, std::unique_ptr<Node>&& q = nullptr) :
data(x),
left(std::move(p)),
right(std::move(q)) {}
};
std::unique_ptr<Node> root = nullptr;
void insert(std::unique_ptr<Node>& root, const int& theData) {
std::unique_ptr<Node> newNode = std::make_unique<Node>(theData);
if (root == nullptr) {
root = std::move(newNode);
return;
}
else if (theData < root->data) {
insert(root->left, theData);
}
else {
insert(root->right, theData);
}
}
void inorderTraversal(std::unique_ptr<Node>& root) {
if (root != nullptr) {
inorderTraversal(root->left);
std::cout << root->data << " ";
inorderTraversal(root->right);
}
}
void preorderTraversal(std::unique_ptr<Node>& root) {
if (root != nullptr) {
std::cout << root->data << " ";
inorderTraversal(root->left);
inorderTraversal(root->right);
}
}
void postorderTraversal(std::unique_ptr<Node>& root) {
if (root != nullptr) {
inorderTraversal(root->left);
inorderTraversal(root->right);
std::cout << root->data << " ";
}
}
int getDepth(std::unique_ptr<Node>& root) {
if (!root) return 0;
else {
int l = getDepth(root->left);
int r = getDepth(root->right);
return std::max(l, r) + 1;
}
}
bool validate(std::unique_ptr<Node>& root, Node* previous) {
if (root == nullptr) return true;
if (!validate(root->left, previous)) return false;
if (previous != nullptr && previous->data >= root->data) return false;
previous = root.get();
return validate(root->right, previous);
}
void DFS(std::unique_ptr<Node>& root) {
std::stack<std::unique_ptr<Node>> s;
s.push(root);
while (!s.empty()) {
std::unique_ptr<Node> x = std::make_unique<Node>(s.top());
s.pop();
if (x->right != nullptr) s.push(x->right);
if (x->left != nullptr) s.push(x->left);
std::cout << x->data << " ";
}
}
int main() {
insert(root, 8);
insert(root, 10);
insert(root, 4);
insert(root, 2);
insert(root, 6);
inorderTraversal(root);
std::cout << "\n";
preorderTraversal(root);
std::cout << "\n";
postorderTraversal(root);
std::cout << "\n";
DFS(root);
std::cout << "\n";
std::cout << getDepth(root) << "\n";
if (validate(root, nullptr)) {
std::cout << "This is a BST!" << "\n";
}
else {
std::cout << "This is not a BST!" << "\n";
}
std::cin.get();
}
I tried to follow what others have done in Java since I could not find a good example in C++. 我试图跟随其他人在Java中所做的事情,因为我在C ++中找不到一个好的例子。 I just want to know what I should do for this implementation or if there is a reference I could see, thanks!
我只是想知道我应该为这个实现做些什么,或者如果有参考我可以看到,谢谢!
Your error happens because you're trying to make copies of the un-copyable: namely instances of std::unique_ptr<Node>
. 您的错误发生是因为您正在尝试制作不可复制的副本:即
std::unique_ptr<Node>
实例。
But your stack doesn't have to host std::unique_ptr<Node>
. 但是您的堆栈不必托管
std::unique_ptr<Node>
。 It can host native pointers to std::unique_ptr<Node>
. 它可以托管
std::unique_ptr<Node>
本机指针。 Or even just const Node*
甚至只是
const Node*
Both are shown below: 两者都显示如下:
void DFS(std::unique_ptr<Node>& root)
{
if (!root)
return;
std::stack<const std::unique_ptr<Node> *> s;
s.push(&root);
while (!s.empty())
{
auto pp = s.top();
s.pop();
if ((*pp)->right)
s.push(&(*pp)->right);
if ((*pp)->left)
s.push(&(*pp)->left);
std::cout << (*pp)->data << ' ';
}
}
Or 要么
void DFS(std::unique_ptr<Node>& root)
{
if (!root)
return;
std::stack<Node const*> s;
s.push(root.get());
while (!s.empty())
{
auto p = s.top();
s.pop();
if (p->right)
s.push(p->right.get());
if (p->left)
s.push(p->left.get());
std::cout << p->data << ' ';
}
}
With either change your code should pass compilation and actually run. 无论是哪种更改,您的代码都应该通过编译并实际运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.