简体   繁体   English

不了解此二进制搜索树(BST)示例算法

[英]Don't understand this Binary Search Tree (BST) example algorithm

In the deletion code from here . 这里删除代码。

I don't understand the first snippet of deletion code (where the node does not have two children). 我不了解删除代码的第一个摘要(该节点没有两个孩子)。

If the node being deleted has a parent and a child itself (ie the node has one child) how does this work? 如果要删除的节点本身有一个父级和一个子级(即该节点有一个子级),这如何工作?

The code is just deleting the node and not setting the parent's pointers to the now orphaned child. 代码只是删除节点,而不是将父级的指针设置为现在孤立的子级。

Am I missing something? 我想念什么吗?

I might be wrong, but the code on the referenced site seems OK. 我可能错了,但是引用站点上的代码似乎还可以。 I haven't tested it, though. 我还没有测试过。

This is true, because the delete function takes one argument which is of type BSTNode** node. 这是正确的,因为delete函数采用一个BSTNode **节点类型的参数。 This is NOT a pointer to the node. 这不是指向节点的指针。 This is a pointer to the parent's node pointer to the node itself . 这是指向父节点的指针,指向节点本身 This might be a little sloppy, but I must admit after realizing what the code does, it is an elegant in its way solution. 这可能有点草率,但是我必须承认在意识到代码的作用之后,这是一种优雅的解决方案。 So when you rewrite (*node), you are not rewriting the node itself , instead you are rewriting the node's parent's pointer to the node . 因此,当您重写(* node)时,您不是在重写节点本身 ,而是在重写节点的父节点到node指针 Effectively the code is doing what you suggested in a slightly perverted manner :D. 有效地,代码以一种稍微变态的方式执行了您建议的操作:D。 Hope you understood what I meant and I hope I got it right. 希望您理解我的意思,希望我理解正确。

I would also recommend that you read further about red-black trees, since this article gives insight only to creating the tree, but the described structure has no asymptotic bounds for its height. 我还建议您进一步阅读红黑树,因为本文仅提供有关创建树的见识,但是所描述的结构对其高度没有渐近界限。 If, eg you push sorted values in this structure, it will be a connected list instead of a balanced tree. 例如,如果按此结构推送排序的值,它将是一个连接列表,而不是平衡树。


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM