Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 编码二叉搜索树_C_Loops_Binary Search Tree - Fatal编程技术网

C 编码二叉搜索树

C 编码二叉搜索树,c,loops,binary-search-tree,C,Loops,Binary Search Tree,根据我的编码类,左边的子项是2*i,右边的子项是2*i+1 考虑到这一点,我编写了以下代码: void max_heap( int key ){ int leftkey = ( 2 * key ) + 1; int rigtkey = ( 2 * key ) + 2; printf( "key is: %d left child index is: %d right child index is: %d \n", key, leftkey, rigtkey

根据我的编码类,左边的子项是
2*i
,右边的子项是
2*i+1

考虑到这一点,我编写了以下代码:

void max_heap( int key ){
    int leftkey = ( 2 * key ) + 1;
    int rigtkey = ( 2 * key ) + 2;
    printf( "key is: %d left child index is: %d right child index is: %d \n",
        key, leftkey, rigtkey );
    printf( "value at key is: %d left child is: %d right child is: %d \n",
        heap[key], heap[leftkey], heap[rigtkey] );

    if ( key >= 0 ){ // My base case?
        if ( leftkey < size && leftkey != size ){
            //Makes sure I don't go out of bounds

            if ( heap[leftkey] > heap[key] ){
                swap( &heap[leftkey], &heap[key] );

            }
        }
        else if ( rigtkey < size && rigtkey != size ){
            // Makes sure I don't go out of bounds

            if ( heap[rigtkey] > heap[key] ){
                swap( &heap[rigtkey], &heap[key] );

            }
        }
        max_heap( key-- );

    }
}
结果是:

key is: 3 left child index is: 7 right child index is: 8 
value at key is: 4 left child is: 0 right child is: 0 

key is: 3 left child index is: 7 right child index is: 8 
value at key is: 4 left child is: 0 right child is: 0 

key is: 3 left child index is: 7 right child index is: 8 
value at key is: 4 left child is: 0 right child is: 0 

key is: 3 left child index is: 7 right child index is: 8 
value at key is: 4 left child is: 0 right child is: 0 

...

它永远都是这样循环的,永远不会进行交换。

问题在于如何递归。您正在使用
max_heap(键--)递归
这实际上意味着“首先调用
max_heap(key);
,然后设置
key=key-1;
”。这会给您一个无休止的循环,因为对函数的每次调用都使用最初提供的相同键。如果预先递减(
max_heap(--key);
),将获得更好的结果


也就是说,这不是一个用递归解决的好问题。while循环会更好。

问题在于如何递归。您正在使用
max_heap(键--)递归
这实际上意味着“首先调用
max_heap(key);
,然后设置
key=key-1;
”。这会给您一个无休止的循环,因为对函数的每次调用都使用最初提供的相同键。如果预先递减(
max_heap(--key);
),将获得更好的结果


也就是说,这不是一个用递归解决的好问题。while循环会更好。

+1同样值得一提的是,该算法本身似乎已被破坏(或者自我编写max heap以来,它已经太久了)。前任:。考虑两种情况:当代码> HeAP[LepTime]>Heop[KE] < /COD>和<代码> HeAP[RealKEK]>Heop[LeftKim] 是真的。+ 1也值得提及算法本身看起来是坏的(或者自从我编码了一个MAX堆以来它已经很久了)。前任:。考虑当 Heop[LeftKy] > Heop[KE] < /C>和<代码> Heop[RealKim] > Heop[LeftKim] < /Cord>时会发生什么。
key is: 3 left child index is: 7 right child index is: 8 
value at key is: 4 left child is: 0 right child is: 0 

key is: 3 left child index is: 7 right child index is: 8 
value at key is: 4 left child is: 0 right child is: 0 

key is: 3 left child index is: 7 right child index is: 8 
value at key is: 4 left child is: 0 right child is: 0 

key is: 3 left child index is: 7 right child index is: 8 
value at key is: 4 left child is: 0 right child is: 0 

...