[英]Adding to a complete binary tree using recursion in C
I am trying to learn recursion, so I tried one program to create a complete binary tree and then print the sum of all its elements, I wrote the insertion part myself and I am confused that my pointer variable "curr"
which points to a tree node, why I am able to do "curr = curr->left"
as it is just a pointer to a node. 我正在尝试学习递归,所以我尝试了一个程序来创建完整的二叉树,然后打印其所有元素的总和,我自己编写了插入部分,并且感到困惑的是我的指针变量
"curr"
指向一棵树节点,为什么我能够执行"curr = curr->left"
因为它只是指向节点的指针。 shouldn't only the actual tree node contain these left and right fields ? 不仅实际的树节点还应该包含这些左右字段? Just give me a heads up on this novice doubt.
请给我一个关于新手疑问的提示。 I am surprised that my program does the job though.
我很惊讶我的程序可以完成这项工作。
Thanks :) 谢谢 :)
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *left,*right;
};
struct node *head = NULL;
struct node *curr = NULL;
void insert_Data(int val,struct node* curr){
struct node *tempnode = (struct node*)malloc(sizeof(struct node));
tempnode->data = val;
tempnode->left = NULL;
tempnode->right = NULL;
if(head==NULL){
head = tempnode;
curr = head;
printf("head element is : %d\n",head->data);
}
else{
if(curr->left == NULL){
curr->left = tempnode;
}
else if(curr->right == NULL){
curr->right = tempnode;
}
else{
curr = curr->left;
insert_Data(val,curr);
}
}
}
//to test program
int sumNode(struct node* root ) {
// if there is no tree, its sum is zero
if( root == NULL ) {
return 0 ;
} else { // there is a tree
printf("element is = %d\n",root->data);
return root->data + sumNode( root->left ) + sumNode( root->right ) ;
}
}
int main() {
int arr[] = {1,2,3,4,5,6,7,8,9};
int i;
for(i=0;i<9;i++){
insert_Data(arr[i],head);
}
int result = sumNode(head);
printf("\n\n%d",result);
return 0;
}
For the meaning of the arrow operator ->
see this related question Arrow operator (->) usage in C . 有关箭头运算符
->
的含义,请参见C中有关此问题的箭头运算符(->)用法 。
Regarding the question title I suggest a recursive insert
method, that follows a root in, root out pattern: 关于问题标题,我建议一个递归
insert
方法,该方法遵循从内到外的模式:
// in: the value to be inserted as new node
// the head of the sub-tree that should contain the new node
// out: the new head of the subtree
struct node* insert_Data(int val, struct node* subtreeRoot);
As long as you don't rebalance the tree, this would basically result in a behavior where any valid subtreeRoot
input would return itself with the new value added somewhere down the tree hierarchy and a NULL
input would return the newly created node as output. 只要您不重新平衡树,这基本上就会导致以下行为:任何有效的
subtreeRoot
输入都将返回自身,并在树层次结构中的某个位置添加新值,而NULL
输入将返回新创建的节点作为输出。
struct node* insert_Data(int val, struct node* subtreeRoot)
{
if (subtreeRoot == NULL)
{
struct node *tempnode = (struct node*)malloc(sizeof(struct node));
tempnode->data = val;
tempnode->left = NULL;
tempnode->right = NULL;
return tempnode;
}
else if (val < subtreeRoot->data)
{
subtreeRoot->left = insert_Data(val, subtreeRoot->left);
}
else // val is bigger than the subtree root data
{
subtreeRoot->right = insert_Data(val, subtreeRoot->right);
}
return subtreeRoot;
}
Use like: 使用方式如下:
head = insert_Data(arr[i], head);
For now, the return value only helps in keeping the NULL
comparisons in a single place. 目前,返回值仅有助于将
NULL
比较保持在一个位置。 But as said, this signature and usage also allows a lot more sophisticated insert methods, where the subtree structure is completely changed on inserts. 但是如上所述,这种签名和用法还允许使用更复杂的插入方法,其中子树结构在插入时会完全改变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.