有人可以解释一下我在return语句中执行两个递归函数,比如这个
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data)
{
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
int size(struct node* node)
{
if (node==NULL)
return 0;
else
return(size(node->left) + 1 + size(node->right));
}
int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("Size of the tree is %d", size(root));
getchar();
return 0;
}
在这种情况下,size()函数如何在return语句中执行,无论是从左到右还是从右到左!我想知道两个函数的执行流程。
答案 0 :(得分:1)
递归由两个概念驱动,即传播条件和终止条件。
条件(node == NULL)是一个终止条件,来自递归和大小(node-> left)+ 1 + size(node-> right)是传播条件,传播左侧树和右侧来自根节点和后续节点的树,并为节点本身的大小增加1。
为了完整地解释你,我们需要一个样本树
3 4 5 1 2 9 8
递归对于样本树
是这样的size(3)
= size(4) + size(5) + 1
现在让我们看size(4) = size(1) + size (2) + 1
size(1)= size(NULL)+ 1 = 0 + 1 = 1(因为1没有左边,如果node为NULL,则函数返回0 - 终止条件)
size(2)= size(NULL)+ 1 = 0 + 1 = 1
因此,尺寸(4)是3
以类似的方式,尺寸(5)也将是3 size(1)= 3 + 3 + 1 =树中的7个节点
因此执行是
size (3)
size (4) + size (5) + 1
size (1) + size (2) + 1 + size(9) +size (8) + 1
size (NULL) + 1 +size (NULL) + 1 + 1 +size (NULL) + 1 +size (NULL) + 1 +1
最终回归
return 0 + 1+ 0+1+1+0+1+0+1+1
返回7
答案 1 :(得分:0)
这是序列
size(root)
=size(root->left) + 1 + size(root->right)
=(size(root->left->left) + 1+ size(root->left->right)) + 1 + size(root->right)
=((size(root->left->left->left) + 1 + size(root->left->left->right)) + 1+ size(root->left->right)) + 1 + size(root->right)
=((0 + 1 + size(root->left->left->right)) + 1+ size(root->left->right)) + 1 + size(root->right)
=((0 + 1 + 0) + 1+ size(root->left->right)) + 1 + size(root->right)
=(1 + 1+ size(root->left->right)) + 1 + size(root->right)
=(1 + 1+ (size(root->left->right->left) +1+ size(root->left->right->right)) + 1 + size(root->right)
=(1 + 1+ (0 +1+ size(root->left->right->right)) + 1 + size(root->right)
=(1 + 1+ (0 +1+ 0)) + 1 + size(root->right)
=(1 + 1+ 1) + 1 + size(root->right)
=(3) + 1 + (size(root->right->left)+ 1 +size(root->right->right))
=(3) + 1 + (size(root->right->left->left)+ 1+ size(root->right->left->right))+ 1 +size(root->right->right))
=(3) + 1 + (0+ 1+ size(root->right->left->right))+ 1 +size(root->right->right))
=(3) + 1 + (0+ 1+ 0)+ 1 +size(root->right->right))
=(3) + 1 + (1)+ 1 +(size(root->right->right->left)+1+ size(root->right->right->right))
=(3) + 1 + (1)+ 1 +(0+1+ size(root->right->right->right))
=(3) + 1 + (1)+ 1 +(0+1+ 0)
=(3) + 1 + (1)+ 1 +(1)
=(3) + 1 + (3)
=7
以下是代码序列:
//step 1
size(root)
{
//node===root
if (root==NULL)//no
return 0;
else
return(size(root->left) + 1 + size(root->right));
//it will call size(root->left)
//after size(root->left) it will plus 1 then will call size(root->right)
}
//step 2
size(root->left)
{
if (root->left==NULL)//no
return 0;
else
return(size(root->left->left) + 1 + size(root->left->right));
}
//step 3
size(root->left->left)
{
if (root->left->left==NULL)//no
return 0;
else
return(size(root->left->left->left) + 1 + size(root->left->left->right));
}
//step 4
size(root->left->left-->left)
{
if (root->left->left==NULL)//yes
return 0;
//it returns to step 3
}
//step 5 from step 3
size(root->left->left)
{
if (root->left->left==NULL)//no
return 0;
else
return(0 + 1 + size(root->left->left->right));
//size(root->left->left->left==0
//now it calls size(root->left->left->right)
}
//step 6 from step 5
size(root->left->left->right)
{
if (root->left->left->right)//yes
return 0;
//it retuns to step 5
}
//step 7 from step 3
size(root->left->left)
{
if (root->left->left==NULL)//no
return 0;
else
return(0 + 1 + 0);//==1
//size(root->left->left->right)==0
//now it returns to 2
}
希望你了解其余部分。