尾递归渗透

时间:2012-08-19 22:00:26

标签: java data-structures heap tail-recursion heapsort

public void insert(int number) {
    heapArr.add(number);
    percolateUp(this.heapArr.size()-1);
}


public void percolateUp(int key) {
    if (getParent(key) >=0 && heapArr.get(getParent(key)) < heapArr.get(key)) {
        int temp = heapArr.get(getParent(key));
        heapArr.set(getParent(key), heapArr.get(key));
        heapArr.set(key, temp);
        percolateUp(getParent(key));
    } else {
        return;
    }
}

我试图围绕尾递归,并据我所知,一些编译器可以判断你是否有尾递归函数,而不是创建任何额外的堆栈帧。在我的上面的例子中,我试图弄清楚递归的percolate是否是尾递归。它不一定等待从函数返回,所以编译器会将其识别为尾递归?

0 个答案:

没有答案