简体   繁体   English

公平删除二叉搜索树中的节点

[英]Fair deletion of nodes in Binary Search Tree

The idea of deleting a node in BST is: 在BST中删除节点的想法是:

  1. If the node has no child, delete it and update the parent's pointer to this node as null 如果该节点没有子节点,则将其删除并将该节点的父节点指针更新为null

  2. If the node has one child, replace the node with its children by updating the node's parent's pointer to its child 如果该节点有一个子节点,则通过更新该节点的父节点指向其子节点的指针,将该节点替换为其子节点

  3. If the node has two children, find the predecessor of the node and replace it with its predecessor, also update the predecessor's parent's pointer by pointing it to its only child (which only can be a left child) 如果该节点有两个子节点,请找到该节点的前任节点并将其替换为其前任节点,并通过将其指向其唯一的子节点(只能是左子节点)来更新前任节点的父节点的指针。

the last case can also be done with use of a successor instead of predecessor! 最后一种情况也可以使用后继而不是前继来完成!

It's said that if we use predecessor in some cases and successor in some other cases (giving them equal priority) we can have better empirical performance , 有人说,如果在某些情况下使用前任产品,而在其他情况下使用后继产品(给予它们同等的优先级),我们可以得到更好的经验性能,

Now the question is , how is it done ? 现在的问题是,如何完成? based on what strategy? 基于什么策略? and how does it affect the performance ? 以及它如何影响性能? (I guess by performance they mean time complexity) (我猜按性能,它们表示时间复杂度)

What I think is that we have to choose predecessor or successor to have a more balanced tree ! 我认为,我们必须选择前任或后继才能拥有更加平衡的树! but I don't know how to choose which one to use ! 但是我不知道该如何选择使用哪一个!

One solution is to randomly choose one of them (fair randomness) but isn't better to have the strategy based on the tree structure ? 一种解决方案是随机选择其中之一(相当的随机性),但是基于树形结构的策略不是更好吗? but the question is WHEN to choose WHICH ? 但是问题是什么时候选择哪个?

As you said, it's a question of balance, so in general the method that disturbs the balance the least is preferable. 如您所说,这是平衡的问题,因此通常最好选择最小化平衡的方法。 You can hold some metrics to measure the level of balance (eg, difference from maximal and minimal leaf height, average height etc.), but I'm not sure whether the overhead worth it. 您可以持有一些度量标准来衡量平衡水平(例如,与最大和最小叶高,平均高度的差等),但是我不确定开销是否值得。 Also, there are self-balancing data structures (red-black, AVL trees etc.) that mitigate this problem by rebalancing after each deletion. 此外,还有一些自平衡数据结构(红黑,AVL树等)可通过在每次删除后进行重新平衡来缓解此问题。 If you want to use the basic BST, I suppose the best strategy without apriori knowledge of tree structure and the deletion sequence would be to toggle between the 2 methods for each deletion. 如果您想使用基本的BST,我想最好的策略是在没有先验知识的树状结构知识的情况下进行,而删除顺序将是在每次删除的2种方法之间切换。

The thing is that is fundamental problem - to find correct removal algorithm for BST. 问题是根本的问题-为BST找到正确的删除算法。 For 50 years people were trying to solve it (just like in-place merge) and they didn't find anything better then just usual algorithm (with predecessor/successor removing). 50年来,人们一直在尝试解决它(就像就地合并一样),他们没有找到比通常的算法更好的东西(删除前任/后继)。 So, what is wrong with classic algorithm? 那么,经典算法有什么问题呢? Actually, this removing unbalances the tree. 实际上,此删除操作使树不平衡。 After several random operations add/remove you'll get unbalanced tree with height sqrt(n) . 经过多次随机操作add/remove ,你会得到不平衡树高度sqrt(n) And it is no matter what you choosed - remove successor or predecessor (or random chose beetwen these ways) - the result is the same. 不管您选择什么-删除后继者或前任者(或通过这些方式随机选择beetwen)-结果是相同的。

So, what to choose? 那么,该选择什么呢? I'm guessing random based (succ or pred) deletion will postpone unbalancing of your tree. 我猜基于随机(成功或掠夺)的删除将推迟您的树的不平衡。 But, if you want to have perfectly balanced tree - you have to use red-black ones or something like that. 但是,如果您想拥有完美平衡的树,则必须使用红黑树或类似的东西。

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

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