简体   繁体   English

为什么二叉搜索树趋向于变得不平衡?

[英]Why does binary search tree tend to become unbalanced to the right?

This is from Heap lecture slide 8. 这是从演讲幻灯片8。

在此处输入图片说明 Add, peek, and remove make sense to me as to why those operations are O(log n)- traversing through BST cuts tree in half after each move. 对于为什么这些操作都是O(log n)遍历BST的原因,添加,查看和删除对我来说是有意义的,因为每一步之后都将树砍了一半。 Can anyone explain the intuition behind the last sentence though, that "The tree tends to become unbalanced to the right"? 有人能解释一下最后一句话背后的直觉吗:“树倾向于在右边变得不平衡”? Why not to left? 为什么不离开? To me, it should be balanced because of the law of averages, like that the frequency of elements less than the root should even out with the frequency of elements greater than the root over time. 对我来说,应该根据平均值定律进行平衡,例如随着时间的推移,小于根的元素的频率应该与大于根的元素的频率均匀。 Law of Averages 平均法则

Don't overthink it. 不要想太多。 It's simply because of the remove operation which always goes to the far left element and removes it. 这仅仅是因为remove操作始终转到最左边的元素并将其删除。 After several of these operations, you would end up with the tree being "heavier" to the right of the tree, regardless of root node or anything else. 经过几次上述操作后,无论根节点或其他任何节点如何,您都将最终使树在树的右侧“重”。

Even if you have an extremely high valued root node that tends to push newly added elements to the left, you'll still eventually end up with a subtree on the left that is "right-heavy". 即使您有一个极高价值的根节点,它倾向于将新添加的元素推向左侧,您最终仍将最终在左侧得到一个“右沉”的子树。

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

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