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
C 递归在二叉搜索树中是如何工作的?_C_Algorithm_Recursion - Fatal编程技术网

C 递归在二叉搜索树中是如何工作的?

C 递归在二叉搜索树中是如何工作的?,c,algorithm,recursion,C,Algorithm,Recursion,二叉搜索树算法通常使用递归,我很难使用它 这是一个将树转换为镜像的代码 void mirror_image(struct tree* node1) { if (node1==NULL) return; else { struct tree *temp; mirror_image(node1->left); mirror_image(node1->right); temp=node1->left;

二叉搜索树算法通常使用递归,我很难使用它

这是一个将树转换为镜像的代码

void mirror_image(struct tree* node1)
{
  if (node1==NULL)
    return;

  else
   {
      struct tree *temp;
      mirror_image(node1->left);
      mirror_image(node1->right);

       temp=node1->left;
       node1->left=node1->right;
       node1->right=temp;

    }

}

这是如何工作的?

因此,它使用

镜像(node1->左)

和正确的儿童树使用

镜像(node1->右侧)

当到达终点时

if (node1==NULL)
    return;
它使用交换过程交换它们:

temp=node1->left;
node1->left=node1->right;
node1->right=temp;

我建议尝试使用一个小的二叉树,在纸上自己看。

因此,它使用

镜像(node1->左)

和正确的儿童树使用

镜像(node1->右侧)

当到达终点时

if (node1==NULL)
    return;
它使用交换过程交换它们:

temp=node1->left;
node1->left=node1->right;
node1->right=temp;

我建议尝试使用一个小的二叉树,在纸上自己看。

基本上,您是通过更改其左右节点来创建新树的。指针,因为您正在更改地址。首先,将左节点的值赋给临时指针变量。然后将右节点的值转换为左节点。最后,temp中的值将移动到右节点。它类似于交换。

基本上,您通过更改其左右节点来创建新树。指针,因为您正在更改地址。首先,将左节点的值赋给临时指针变量。然后将右节点的值转换为左节点。最后,temp中的值将移动到右节点。这就像交换