简体   繁体   English

二进制搜索树中的删除操作

[英]Delete operation in a Binary Search Tree

I am having to hard code a binary search tree for a class. 我必须对一个类的二进制搜索树进行硬编码。 I somewhat understand the delete method for removing a node but I am getting mixed information as to what I replace it with... 我有点理解删除节点的删除方法,但是我得到的替换信息却是...

I have been told to use the left-most node in the right subtree OR the rightmost node in the left subtree... Do I replace with the smallest node in right subtree or largest Node in left subtree? 我被告知要使用右侧子树中的最左侧节点或左侧子树中的最右侧节点...我要替换为右侧子树中的最小节点还是左侧子树中的最大节点?

Does it make a difference which one that I use? 我使用哪一个会有所不同? Should I implement both and have the program alternatively switch off from each one? 我应该同时实现这两种功能,还是让程序分别关闭?

It doesn't matter which one you implement. 实施哪一个都没关系。 Replacing the deleted node with the rightmost node of the left subtree or the leftmost node of the right subtree will both result in a valid binary search tree. 将删除的节点替换为左子树的最右节点或右子树的最左节点都将产生有效的二进制搜索树。

If you want, you could alternate between them to try and maintain some balance. 如果需要,您可以在他们之间交替尝试以保持一定的平衡。 You could also choose the subtree with the higher height to remove from as well to achieve a similar thing. 您还可以选择具有更高高度的子树以从中删除,以实现类似的目的。

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

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