我做了以下涉及二进制堆结构的算法:
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 表示法计算算法的运行时间。我感到困惑的原因是因为用于遍历每个节点的左右子节点的递归。
所有操作都在恒定时间内运行O(1)
,除了concat
。但是我该如何计算这种递归解决方案的运行时间呢?