简体   繁体   English

二叉搜索树 - 删除

[英]Binary Search Tree - Deletion

Regarding the deletion operation and specifically the case where a node has exactly 2 subtrees I know that choosing to find the minimum element of the right subtree to 'substitute' the corresponding element is the way to go as the sorted property is preserved.关于删除操作,特别是节点正好有 2 个子树的情况,我知道选择找到右子树的最小元素来“替换”相应的元素是保留排序属性的方法。

However why not choose the maximum element of the left subtree of the corresponding element?但是为什么不选择对应元素的左子树的最大元素呢?

I think that the sorted property does not break and can't think of a counter example.我认为 sorted 属性不会中断,也想不出反例。

From what I know, there's no "concrete" rule to always choose the minimum element of the right subtree.据我所知,没有“具体”规则总是选择右子树的最小元素。 Judging by symmetry, using the maximum element from the left subtree would also work.从对称性来看,使用左子树中的最大元素也可以。

Logically speaking, the inorder traversal of a BST produces a sorted output.从逻辑上讲,BST 的中序遍历会产生排序后的输出。 Consider the following output考虑以下输出

....A B C....

Here, A and C are the inorder predecessor (basically, the maximum element from the left subtree of B ) and inorder successor (the minimum element from the right subtree of B ) respectively.这里, AC分别是有序前驱(基本上是B的左子树的最大元素)和有序的后继( B的右子树的最小元素)。 Replacing B by either C or A will not change the sorted order, so there's no reason to say that C is the only option to replace B .CA替换B不会更改排序顺序,因此没有理由说C是替换B的唯一选项。

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

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