简体   繁体   English

二进制搜索树-节点删除

[英]Binary Search Tree - node deletion

I am trying to understand why when deleting a node in a BST tree and having to keep the children and adhering to the BST structure, you have to either take the node's right child (higher value, then node being deleted) and if that right child has a left child take that child. 我试图理解为什么在删除BST树中的节点并保留子节点并坚持BST结构时为什么必须采用该节点的正确子节点(较高的值,然后删除该节点),以及如果该正确的子节点有一个左孩子带走那个孩子。 Else just the the node being deleted right child. 否则,只是该节点正被删除的子节点。

Why don't you just take the node being deleted left child, if there's one. 如果有一个节点,为什么不只是将要删除的节点作为子节点呢? It still works out correctly? 它仍然可以正常工作吗?

Or have I missed something. 或者我错过了什么。

I'm reading this article. 我正在读这篇文章。

You're oversimplifying. 你太简单了。

The node selected to replace the one that was deleted must be larger than all the nodes to the left of the deleted one, and smaller than all the nodes to the right. 选择替换替换已删除节点的节点必须大于已删除节点左侧的所有节点,并且必须小于右侧所有节点。 So it must be either the left subtree's rightmost descendant or the right subtree's leftmost descendant; 因此,它必须是左子树的最右后代或右子树的最左后代。 except if one or the other subtree is entirely absent, we can remove a level of tree entirely simply by replacing the deleted node with the child that was present. 除非一个或另一个子树完全不存在,否则我们可以简单地通过将删除的节点替换为存在的子节点来完全删除树的级别。

The rules listed in the article will always give you the right subtree's leftmost descendant when both trees are present. 当两棵树都存在时,本文中列出的规则将始终为您提供右子树的最左后代。 If you wished, you could indeed derive an alternative ruleset that used the leftmost subtree's rightmost descendant instead. 如果愿意,您确实可以派生出一个替代规则集,该规则集使用最左子树的最右后代。

It does not "work out correctly" to just always use the left child. 仅始终使用左孩子不能“正确锻炼”。 Indeed, if there is a child on the right and the left child itself has two children, it cannot even be done without essentially rebuilding the tree. 的确,如果右边有一个孩子,而左边的孩子本身有两个孩子,那么即使没有实质性的重建树也无法做到。

You would be correct for the special case that you described. 您所描述的特殊情况是正确的。 But for something more general where you can have many more levels deeper than the node being deleted you need to replace that node with a node that will be less than everything to the right, and greater than everything to the left. 但是对于更一般的情况,您可以比要删除的节点更深的级别,则需要用一个小于右边的所有节点,大于左边的所有节点替换该节点。 So as an example: 因此,例如:

   2
 /  \
1    6
    / \
   4   7
    \
     5  

Let's say you wanted to move the node 6, now following your instructions we will replace it with the left child, node 4. Now what do we do with node 5? 假设您要移动节点6,现在按照说明将其替换为左侧的子节点4。现在如何处理节点5? We could make it the left child of node 7 (or the left most descendant of node 7 if it existed), but why would you do all this reshuffling when you know that removing a leaf is trivial and you just want to replace the node with another node that would keep every node on the left less and every node on the right greater. 我们可以将其设为节点7的左子节点(如果存在,则为节点7的最左子节点),但是当您知道移除叶子很简单并且只想用替换该节点时,为什么要进行所有这些改组另一个节点,它将使左侧的每个节点更少,右侧的每个节点更大。

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

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