【发布时间】:2012-08-10 12:13:11
【问题描述】:
我很难找出平均和最坏情况的时间复杂度。所以我用以下逻辑删除了这个 BST 节点
当你在二叉搜索树中删除一个节点时,有3种情况
1> The node to delete has no children. That's easy: just release its resources and you're done. Time complexity O(1)
2> The node has a single child node. Release the node and replace it with its child, so the child holds the removed node's place in the tree. Time complexity O(1)
3> The node has two children. Find the right-most child of node's left subtree. Assign its value to root, and delete this child. **Here time compexity can be maximum O(N)**
To find the node to be deleted can be **maximum O(N)**
那么如何计算总体平均时间复杂度和最差时间复杂度?
【问题讨论】:
-
这是一棵平衡树吗?不是,找到节点是 O(n)。
-
@KarolyHorvath ......我错过了一点......这不是一棵平衡的树......我只会进行必要的编辑......
-
@AndreasBrinck ... N 是 BST 的大小 .... 对于倾斜树,我们可能需要多达“N”个操作来找到一个节点...所以最坏情况下的时间复杂度为找到一个节点是 O(N)
-
是的,我只是觉得问题的标题有点误导。在 BST 中查找节点的复杂性会更合适。
-
复杂度是耗时最长的操作的复杂度,所以在这种情况下是O(n)。
标签: c++ algorithm binary-search-tree