简体   繁体   English

二进制搜索树中的删除

[英]Deletion in Binary Search Tree

Why is deletion in a BST a O(log(n)) operation. 为什么在BST中删除O(log(n))操作。 As i undersand it involves freeing a node and pointing the parent's reference to NULL . 正如我所说,它涉及释放节点并将父引用指向NULL Shouldn't this take O(1) 不应该这个拿O(1)

The issue is how to delete a node which has two children -- the tree must be restructured so that the children find suitable new parents. 问题是如何删除有两个子节点的节点 - 必须重新构建树,以便子节点找到合适的新父节点。 Detailed explanation here . 详细说明在这里 Google is your friend. 谷歌是你的朋友。

如果你从树的根开始,那就是O(lg n ):那么你必须搜索要删除的元素,然后搜索它的有序后继元素。

如果要删除节点及其所有子节点,则它很简单,但如果要保持排序顺序,则必须重建子节点。

Deletion in a binary search tree is O(h) where h is the height of the tree. 二叉搜索树中的删除是O(h),其中h是树的高度。

Now that u haven't mentioned whether the tree is balanced or not the worst case complexity for an unbalanced tree would be O(n), ie when it is a degenerate tree. 既然你没有提到树是否平衡,不平衡树的最坏情况复杂性将是O(n),即当它是退化树时。

In case the bst is one of the balanced ones(Avl, red black etc.), then worst case complexity would be O(lg n) as the height of virtually all balanced bst is K*(lg n). 如果bst是平衡的(avl,红黑等)之一,则最坏情况复杂度将是O(lg n),因为几乎所有平衡bst的高度是K *(lg n)。

For example, for avl tree k = 1 and for red black tree K = 2. 例如,对于avl树k = 1并且对于红黑树K = 2。

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

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