Javascript中的递归maxHeap实现

Javascript中的递归maxHeap实现,javascript,algorithm,Javascript,Algorithm,我一直在阅读有关堆的文章,并看到了各种javascript实现。 是插入和移除的一个很好的例子。 但是,我想以递归的方式编写max堆的构建。 例如,在C++中,下面的代码应该做的是: // C++ program for building Heap from Array #include <iostream> using namespace std; // To heapify a subtree rooted with node i which is /

我一直在阅读有关堆的文章,并看到了各种javascript实现。 是插入和移除的一个很好的例子。 但是,我想以递归的方式编写max堆的构建。 例如,在C++中,下面的代码应该做的是:


// C++ program for building Heap from Array 
  
#include <iostream> 
  
using namespace std; 
  
// To heapify a subtree rooted with node i which is 
// an index in arr[]. N is size of heap 
void heapify(int arr[], int n, int i) 
{ 
    int largest = i; // Initialize largest as root 
    int l = 2 * i + 1; // left = 2*i + 1 
    int r = 2 * i + 2; // right = 2*i + 2 
  
    // If left child is larger than root 
    if (l < n && arr[l] > arr[largest]) 
        largest = l; 
  
    // If right child is larger than largest so far 
    if (r < n && arr[r] > arr[largest]) 
        largest = r; 
  
    // If largest is not root 
    if (largest != i) { 
        swap(arr[i], arr[largest]); 
  
        // Recursively heapify the affected sub-tree 
        heapify(arr, n, largest); 
    } 
} 
  
// Function to build a Max-Heap from the given array 
void buildHeap(int arr[], int n) 
{ 
    // Index of last non-leaf node 
    int startIdx = (n / 2) - 1; 
  
    // Perform reverse level order traversal 
    // from last non-leaf node and heapify 
    // each node 
    for (int i = startIdx; i >= 0; i--) { 
        heapify(arr, n, i); 
    } 
} 

我得到这个错误:

VM56:2 Uncaught RangeError: Maximum call stack size exceeded
    at heapify (<anonymous>:2:20)
    at heapify (<anonymous>:10:9)
    at heapify (<anonymous>:10:9)
    at heapify (<anonymous>:10:9)
    at heapify (<anonymous>:10:9)
    at heapify (<anonymous>:10:9)
    at heapify (<anonymous>:10:9)
    at heapify (<anonymous>:10:9)
    at heapify (<anonymous>:10:9)
    at heapify (<anonymous>:10:9)
    ```

Can anyone help me figure out what's wrong with my implementation? 
VM56:2未捕获范围错误:超过最大调用堆栈大小
在希皮菲(:2:20)
在希皮菲(:10:9)
在希皮菲(:10:9)
在希皮菲(:10:9)
在希皮菲(:10:9)
在希皮菲(:10:9)
在希皮菲(:10:9)
在希皮菲(:10:9)
在希皮菲(:10:9)
在希皮菲(:10:9)
```
有人能帮我找出我的实现出了什么问题吗?
几个问题:

(1) 此部分中有一个错误:

let left = 2 * i;
let right = 2 * i + 1;
这是不对的。假设根的索引是
i=0
,那么
left
的值是多少?再次0。。。那不可能是对的。正确的公式在您前面引用的代码中:

let left = 2 * i + 1;
let right = 2 * i + 2;
(2) 第二个错误发生在调用
swap
的行中。你没有传递正确的论点。它需要3个:
A
,然后是两个索引。您正在传递两个值。应该是(另见下一点):

(3) 然后,
swap
函数有一个错误的实现(JavaScript只有按值调用)。在该函数中更改
x
y
对数组没有影响。应该是这样的:

function swap (A, x, y){
    let c = A[x];
    A[x] = A[y];
    A[y] = c;
}
(4) 最后,只有在进行交换时,才应进行递归的
heapify
函数调用:

if(largest !== i) {
    swap(A, i, largest);
    heapify(A, n, largest)
}
已更正代码段中的代码:

函数构建最大堆(A,n){
让开始指数=数学楼层(n/2);
对于(让i=start\u index;i>=0;i--){
heapify(A,n,i);
}
}
函数heapify(A,n,i){
设左=2*i+1;
设右=2*i+2;
设最大=i;
如果(A[左]>A[最大])最大=左;
如果(A[右]>A[最大])最大=右;
如果(最大!==i){
掉期(A、i、最大);
heapify(A、n、最大)
}
}
功能交换(A、x、y){
设c=A[x];
A[x]=A[y];
A[y]=c;
}
//示例调用
设A=[4,3,2,1,5,9,8,7,6];
构建最大堆(A,A.长度);
console.log(A.join())
function swap (A, x, y){
    let c = A[x];
    A[x] = A[y];
    A[y] = c;
}
if(largest !== i) {
    swap(A, i, largest);
    heapify(A, n, largest)
}