【发布时间】:2015-02-20 00:35:48
【问题描述】:
考虑以下算法,该算法使用二叉搜索树对包含 n 个元素的列表进行排序:
initialise t to be an empty binary search tree
for each element x in the list,
add x to t
while t is not empty,
remove and print the smallest element of t
这个算法的最坏情况时间复杂度是多少,如果树是 使用实现:
a) 普通的二叉搜索树?
b) AVL 树?
我的解决方案:我认为解决方案是,对于 AVL 树:O(Log N),对于 BST,它是 O(N)
【问题讨论】:
-
这个问题似乎是题外话,因为它是关于一般的编程概念,而不是特定于代码的问题。它更适合 Programmers SE。
-
这个问题和我正在学习的数据结构有关。
-
@keyser 我做了研究,我的研究得到了答案,但我不确定。
-
@keyser 我已经编辑了我的问题。
-
在我看来,遍历 BST(最坏情况)需要 (N) 次才能找到最小值,但在 AVL 树中它已经平衡,因此只需要 log (n) 即可找到最小,因为旋转
标签: algorithm binary-search-tree avl-tree