[英]Binary Search Tree using std::unique ptr
I'm trying to implement a Binary Search Tree using smart pointers and I've read that the recommended way to implement it is by using unique_ptr since a parent owns the child and there are no multiple owners in a binary search tree.我正在尝试使用智能指针实现二叉搜索树,并且我读到推荐的实现方法是使用 unique_ptr ,因为父母拥有孩子并且二叉搜索树中没有多个所有者。
Take this tree for example,以这棵树为例,
10
4 20
2 5 11 30
3
here 4 is left child of 10 and 2 is left child of 4 and 3 is right child of 2 and so on.这里 4 是 10 的左孩子,2 是 4 的左孩子,3 是 2 的右孩子,依此类推。
Now the struct looks like,现在结构看起来像,
template<typename T>
struct TreeNode {
T data;
std::unique_ptr<TreeNode<T>> left, right;
};
there's a unique_ptr<TreeNode<T>> root
pointing to the root.有一个
unique_ptr<TreeNode<T>> root
指向根。
Now if I'm understanding it correctly unique_ptr's
can't be copied or assigned and they have unique ownership of the object.现在,如果我理解正确,则
unique_ptr's
无法复制或分配,并且它们具有对象的唯一所有权。
So if I want to traverse the tree I can't do something like initialize a std::unique_ptr<TreeNode<T>> temp
to the root and traverse every node from there since it'll throw an error once I try to set the root
which is a unique_ptr
to the temp
.因此,如果我想遍历树,我不能执行诸如将
std::unique_ptr<TreeNode<T>> temp
初始化到根并从那里遍历每个节点之类的操作,因为一旦我尝试设置它就会抛出错误root
是temp
的unique_ptr
。
So do I need to use a raw pointer of type TreeNode*
to traverse the tree and perform operations?那么我是否需要使用
TreeNode*
类型的原始指针来遍历树并执行操作? is it good or safe to do so?这样做好还是安全? For all my operations on this tree then will I have to use raw pointers?
对于我在这棵树上的所有操作,我是否必须使用原始指针?
Another problem is with deleting a node.另一个问题是删除节点。 If I say want to delete node with the value
3
.如果我说要删除值为
3
节点。 If I initialize a raw pointer of type TreeNode* temp
and arrive at Treenode 3
.如果我初始化一个
TreeNode* temp
类型的原始指针并到达Treenode 3
。 Then if I call delete(temp) what will happen?那么如果我调用 delete(temp) 会发生什么? A
unique_ptr
from TreeNode 2
is pointing at TreeNode 3
.来自
TreeNode 2
的unique_ptr
指向TreeNode 3
。 What will happen to this pointer?这个指针会发生什么?
Then if I call delete(temp) what will happen?
那么如果我调用 delete(temp) 会发生什么?
The TreeNode
will be destroyed. TreeNode
将被销毁。 Note that delete
doesn't need parentheses注意
delete
不需要括号
A
unique_ptr
fromTreeNode
2 is pointing atTreeNode
3. What will happen to this pointer?来自
TreeNode
2 的unique_ptr
指向TreeNode
3。这个指针会发生什么?
The pointer becomes invalid, and it is undefined behaviour for the unique_ptr
object to be destroyed, as it will attempt to delete
an invalid pointer.指针变为无效,并且
unique_ptr
对象被销毁是未定义的行为,因为它将尝试delete
无效指针。
So do I need to use a raw pointer of type TreeNode* to traverse the tree and perform operations?
那么我是否需要使用 TreeNode* 类型的原始指针来遍历树并执行操作? is it good or safe to do so?
这样做好还是安全? For all my operations on this tree then will I have to use raw pointers?
对于我在这棵树上的所有操作,我是否必须使用原始指针?
You can have a reference (or pointer) to a std::unique_ptr
, you don't need to copy it.你可以有一个指向
std::unique_ptr
的引用(或指针),你不需要复制它。
Rather than delete
a raw pointer, you can call the reset
member function of the unique_ptr
to free the pointed-to TreeNode
您可以调用
unique_ptr
的reset
成员函数来释放指向的TreeNode
,而不是delete
原始指针
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.