【发布时间】:2011-01-19 03:28:44
【问题描述】:
我做了以下涉及二进制堆结构的算法:
Algorithm: heapMinimum(node)
Input : Position n
Output : Sequence minList; containing the postions that hold the minimum value
1. minList <-- sequence
2. if parent(node) == NULL // current node is the root of the tree
3. minList.insertLast(node)
4. if (leftchild(node).element() == node.element())
5. concat(heapMinimum(leftchild(node), minList))
6. if(right(node).element() == node.element())
7. concat(heapMinimum(rightChild(node), minList))
8. return minList
算法所做的基本上是遍历给定其根的二叉堆,以查找并存储保存最小值(即与根的值匹配的值)的节点。
现在,我无法以大 O 表示法计算算法的运行时间。我感到困惑的原因是因为用于遍历每个节点的左右子节点的递归。
除concat 外,所有操作都以恒定时间运行,O(1)。但是我该如何计算这种递归解决方案的运行时间呢?
【问题讨论】:
-
看看堆上操作的复杂性:en.wikipedia.org/wiki/…
标签: algorithm heap big-o tree-traversal