简体   繁体   中英

Deletion in Binary Search Tree

Why is deletion in a BST a O(log(n)) operation. As i undersand it involves freeing a node and pointing the parent's reference to NULL . Shouldn't this take 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.

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.

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).

For example, for avl tree k = 1 and for red black tree K = 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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