我在找出平均和最差时间复杂度时遇到了一些困难。所以我用以下逻辑
删除了这个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)**
那么如何计算总体平均值和最差时间复杂度?
答案 0 :(得分:0)
在这种情况下,我认为最坏情况的复杂性足以描述这种情况。
为了找出最坏情况的复杂性,只需找出你提到的可能的三种情况中的最坏情况(O(n)情况)。因此,最坏情况的复杂性是O(n)。
在一些罕见的情况下(例如Quicksort),人们选择描述平均情况复杂性以及最坏情况的复杂性。在Quicksort的情况下,这是因为Quicksort几乎在所有情况下都是O(n * log(n)),并且在一些非常罕见的情况下仅减少到O(n ^ 2)。因此,人们描述了平均情况以及最坏情况的复杂性。
但是,在从二叉搜索树中删除节点的情况下,删除叶节点(没有子节点和最佳情况)不会比删除节点更频繁或少得多。 /两个孩子(除非你正在为特殊情况开发)。
因此,从二叉搜索树中删除节点的复杂性是O(n)。
答案 1 :(得分:0)
平均案例复杂度为O(删除时为log(n)为了查找节点,它将采用O(log(n)并再次删除O(log(n)) 因此平均值为O(log(n))+ O(log(n))= O(log(n)) 最坏的情况显然是O(n) 有关详细信息,请访问http://en.wikipedia.org/wiki/Binary_search_tree#Deletion