Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.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/4/algorithm/11.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_Algorithm_Recursion_Iteration - Fatal编程技术网

C 递归函数到迭代函数的变换

C 递归函数到迭代函数的变换,c,algorithm,recursion,iteration,C,Algorithm,Recursion,Iteration,我试图将这个递归函数转换成一个迭代函数 void printPath(int parent[], int j) { // Base Case if (parent[j] == - 1) return; printPath(parent, parent[j]); printf("%d ", j); } 这是输出 0 1 0 1 2 0 1 2 3 0 7 6 5 4 0 7 6 5 0 7 6 0 7 0 1 2 8 这是我尝试

我试图将这个递归函数转换成一个迭代函数

void printPath(int parent[], int j) 
{ 

    // Base Case
    if (parent[j] == - 1) 
        return; 

    printPath(parent, parent[j]); 

    printf("%d ", j); 
}
这是输出

0 1
0 1 2
0 1 2 3
0 7 6 5 4
0 7 6 5
0 7 6
0 7
0 1 2 8
这是我尝试过的,但输出不正确

void printPath(int parent[], int j) 
{   
    int temp = parent[j];

    while (temp != - 1)
    {
        temp = parent[temp];
        printf("%d ", temp);

    }
} 
这是输出[不正确]

0 -1
0 0 -1
0 1 0 -1
0 6 7 0 -1
0 7 0 -1
0 0 -1
0 -1
0 1 0 -1
警告,(它是…)未测试的代码:-)

如注释中所述,递归函数以某种方式遍历数组,直到到达一个停止点,同时在堆栈中记住所有的段落:然后才开始打印。因此,应该使用数组来保存中间结果

void printPath(int parent[], int j) {
  int  revert[V];    // to reverse; "V" is a costant (==9, size of vector)
  int  count=0;      // perhaps not needed, is assumed to always reach V
  int  temp;

  // unroll and store
  temp = j;         // edited; my temp=parent[j] was wrong
  while (temp) {
    revert[count++] = temp;
    temp = parent[temp];
  }

  // print reversed
  while (count) {
    count--;
    printf("%d ", revert[count]);
  }
}
我不确定这个程序是否有效,现在无法测试。在你最初的诱惑中有一个错误,因为

    temp = parent[temp];
    printf("%d ", temp);
它甚至输出-1,因为它先打印,然后检查


希望这有帮助,我尝试更正错误并实现反转。

根据您的问题,我得到的父数组为:

int parent[] = {-1,0,1,2,5,6,7,0,2};
我还运行您的代码:

void printPath(int parent[], int j) 
{   
    int temp = parent[j];

    while (temp != - 1)
    {
        temp = parent[temp];
        printf("%d ", temp);

    }
} 
现在要注意三件事:

1) 初始化
int temp=j
而不是使用
parent[j]
。这确保您从
j
开始,而不是从
parent[j]
开始

2) 而不是这样做:

temp = parent[temp];
printf("%d ", temp);
这样做:

printf("%d ", temp);
temp = parent[temp];
这确保您首先打印j,然后打印父[j]

进行上述更改后,您的输出将如下所示:

1 0 
2 1 0 
3 2 1 0 
4 5 6 7 0 
5 6 7 0 
6 7 0 
7 0 
8 2 1 0 
现在是最后一个音符

3) 要获得所需的输出,您需要按相反的顺序打印。一种方法是首先对路径中的元素进行计数。然后创建一个该大小的数组。从
j
开始再次遍历,并将元素存储在数组中。之后,可以按相反顺序打印阵列

进行这些更改后,我们得到以下代码:

void printPath(int parent[], int j) 
{   
    // Initialized temp from j
    int temp = j;

    // int variable that will store the number of elements in path
    int cnt=0;

    // traversing the path for first time to get count of elements in path
    while (temp != - 1)
    {
        cnt++;
        temp = parent[temp];
    }

    // creating array of cnt size
    int arr[cnt];

    // traversing the path for second time.
    // this time we will store elements in the array
    temp = j;
    int i=0;

    while(temp!=-1){
        arr[i++]=temp;
        temp = parent[temp];
    }

    // printing the elements in reverse order
    for(int i=cnt-1;i>=0;i--)
        printf("%d ",arr[i]);
} 
上述代码将为您提供正确的输出(如您所述):


在处理迭代解决方案之前,您的递归解决方案需要修复,因为它无法显示子0的路径:

void printPath_recursive(int parent_of[], int child)
{
    int parent = parent_of[child];

    if (parent != -1)
    {
        printPath_recursive(parent_of, parent);
    }

    printf("%d ", child); 
}
迭代解决方案遵循人们建议的思路:

int parent_map[] = {-1, 0, 1, 2, 5, 6, 7, 0, 2};

#define PARENT_MAP_LENGTH (sizeof(parent_map) / sizeof(parent_map[0]))

void printPath_iterative(int parent_of[], int child) 
{
    int s = 0, ancestry[PARENT_MAP_LENGTH];

    while (child != -1)
    {
        ancestry[s++] = child;

        child = parent_of[child];
    }

    while (s > 0)
    {
        printf("%d ", ancestry[--s]);
    }
}
以下任一项的输出:

0
0 1
0 1 2
0 1 2 3
0 7 6 5 4
0 7 6 5
0 7 6
0 7
0 1 2 8

当您尝试此操作时发生了什么?@cigien输出错误(与第一个不同)请将该信息(两段代码的输出)添加到问题中。您必须反向打印输出。因此,您需要将数字存储在某个位置,然后将其向后打印。正如@0x499602D2所建议的,您的迭代代码正在将子项打印到父项。使用数组存储值并按相反顺序打印。测试代码。替换
temp=parent[j]带有
temp=j
。代码遗漏了最后一个数字。建议将其作为一个测试,请将建议包含在您的解决方案中,这样我就可以将其标记为答案hak you@AbhayAravinda and Java,奇怪的是这样一个简短的例程很难完全理解,但团队工作是有效的。
0
0 1
0 1 2
0 1 2 3
0 7 6 5 4
0 7 6 5
0 7 6
0 7
0 1 2 8