C 在二叉树中添加节点时使用指向结构指针的指针

C 在二叉树中添加节点时使用指向结构指针的指针,c,pointers,tree,traversal,C,Pointers,Tree,Traversal,我想知道为什么在二叉树中插入节点时使用指针对指针。 但是,在遍历二叉树时,我们仅通过指向根节点的简单指针引用该树。但为什么在插入节点时 任何人都可以帮助我提供原因或参考链接,以了解为什么它是指针指向指针 /*This program clears out all the three methods of traversal */ #include<stdio.h> #include<stdlib.h> /* Let us basically describe how

我想知道为什么在二叉树中插入节点时使用指针对指针。 但是,在遍历二叉树时,我们仅通过指向根节点的简单指针引用该树。但为什么在插入节点时

任何人都可以帮助我提供原因或参考链接,以了解为什么它是指针指向指针

/*This program clears out all the three methods of traversal */

#include<stdio.h>
#include<stdlib.h>

/* Let us basically describe how a particular node looks in the binary tree .... Every node in the tree has three major elements , left child, right child, and  and the data. */

struct  TreeNode {
int data;
struct TreeNode *leftChild;
struct TreeNode *rightChild;
};

void inorder(struct TreeNode *bt);
void preorder(struct TreeNode *bt);
void postorder(struct TreeNode *bt);
int insert(struct TreeNode **bt,int num);

main()
{
int num,elements;
struct TreeNode *bt;
int i;

printf("Enter number of elements to be inserted in the tree");
scanf("%d",&num);

printf("Enter the elements to be inserted inside the tree");
for(i=0;i<num;i++)
{
scanf("%d",&elements);
insert(&bt,elements);
printf("\n");
}

printf("In Order Traversal \n");
inorder(bt);

printf("Pre Order Traversal \n");
preorder(bt);

printf("Post Order Traversal \n");
postorder(bt);

return 0;
}

int insert(struct TreeNode **bt,int num)
{
if(*bt==NULL)
{
*bt= malloc(sizeof(struct TreeNode));

(*bt)->leftChild=NULL;
(*bt)->data=num;
(*bt)->rightChild=NULL;

return;
}
else{
/* */
if(num < (*bt)->data)
{
insert(&((*bt)->leftChild),num);
}
else
{
insert(&((*bt)->rightChild),num);
}
}
return;
}

void inorder(struct TreeNode *bt){
if(bt!=NULL){


//Process the left node
inorder(bt->leftChild);

/*print the data of the parent node */
//printf(" %d ", bt->data);

/*process the right node */
inorder(bt->rightChild);
}

}

void preorder(struct TreeNode *bt){
if(bt)
{
//Process the parent node first
printf("%d",bt->data);

//Process the left node.
preorder(bt->leftChild);

//Process the right node.
preorder(bt->rightChild);


}

}


void postorder(struct TreeNode *bt){

if(bt)
{
//process the left child
postorder(bt->leftChild);

//process the right child
postorder(bt->rightChild);


//process the parent node
printf("%d",bt->data);


}
}
/*此程序清除所有三种遍历方法*/
#包括
#包括
/*让我们基本上描述一个特定的节点在二叉树中的外观。。。。树中的每个节点都有三个主要元素:左子元素、右子元素和数据*/
树状结构{
int数据;
struct TreeNode*leftChild;
结构树节点*右子节点;
};
顺序无效(结构树节点*bt);
无效预订单(结构树节点*bt);
无效邮购(struct TreeNode*bt);
int insert(结构树节点**bt,int num);
main()
{
int num,元素;
结构树节点*bt;
int i;
printf(“输入要插入到树中的元素数”);
scanf(“%d”和&num);
printf(“输入要插入到树中的元素”);
对于(i=0;ileftChild=NULL;
(*bt)->data=num;
(*bt)->rightChild=NULL;
返回;
}
否则{
/* */
如果(num<(*bt)->数据)
{
插入(&((*bt)->leftChild),num;
}
其他的
{
插入(&((*bt)->rightChild),num;
}
}
返回;
}
空索引(结构树节点*bt){
如果(bt!=NULL){
//处理左节点
顺序(bt->leftChild);
/*打印父节点的数据*/
//printf(“%d”,bt->data);
/*处理正确的节点*/
顺序(bt->rightChild);
}
}
无效预订单(结构树节点*bt){
国际单项体育联合会(英国电信)
{
//首先处理父节点
printf(“%d”,bt->data);
//处理左节点。
预订单(bt->leftChild);
//处理正确的节点。
预订单(bt->rightChild);
}
}
作废后订单(结构树节点*bt){
国际单项体育联合会(英国电信)
{
//处理左边的子对象
postorder(bt->leftChild);
//处理正确的孩子
邮购(bt->rightChild);
//处理父节点
printf(“%d”,bt->data);
}
}

这是因为insert的第一部分包含了一个新的结构treenode。如果您只传入了一个结构treenode*,则如下所示:

int insert(struct TreeNode *bt,int num)
{
   if(bt==NULL)
   {
       bt= malloc(sizeof(struct TreeNode));

       (bt)->leftChild=NULL;
       (bt)->data=num;
       (bt)->rightChild=NULL;

   return;
   }
  ...
}
这样做的问题是,bt是本地插入的,因此main中的bt将保持不变。因此,您传入一个指向main的bt的指针,这样insert就可以对其进行更改。

“我想知道为什么在二叉树中插入节点时使用指向指针的指针。但是,在遍历二叉树时,我们只是通过指向根节点的简单指针引用树。但是为什么在插入节点时使用指针?”

实际上,我们甚至不需要代码来回答这个问题。如果您想在C中修改(写入)外部函数中的数据,您需要有数据的地址。就像:

main() {
   int x = 2;
   change_me(x);
   printf("%d\n", x); // prints 2
}

void change_me(int x){
   x++;
}
没有意义。您(在本例中)正在获取VARABLE的本地副本,对该值所做的任何更改都仅在本地范围内。如果希望这些更改传播回调用函数,则需要地址:

main() {
   int x = 2;
   change_me(&x);
   printf("%d\n", x); // prints 3
}

void change_me(int* x){
   (*x)++;
}
指针也是如此。在链表示例中,如果要打印值,我需要遍历树并读取数据。我不需要更改任何内容,只需指针即可。但是,如果我要修改树,请执行以下操作:

struct node{
   int val;
   sturct node* next;
};

main() {
  struct node* head = malloc(sizeof(struct node));
  head->val = 3;
  insert_a_node_in_front(head);
}

insert_a_node_in_front(node * ptr) {
    struct node* temp = ptr;
    ptr = malloc(sizeof(struct node));
    ptr->val = 5;
    ptr->next = temp;
}
嗯,你猜怎么着?我们实际上并没有插入那个节点,因为
head
的值从未更改。它仍然指向原始节点,且
val==3
。原因与以前相同,我们试图更改参数的本地副本的值。如果我们希望这些更改保持不变,它需要原始节点的地址副本:

  insert_a_node_in_front(&head);
}

insert_a_node_in_front(node ** ptr) {
    struct node* temp = (*ptr);
    (*ptr) = malloc(sizeof(struct node));
    (*ptr)->val = 5;
    (*ptr)->next = temp;
}

下面给出了使用指针的一个非常好的提示: 尝试以下基本方法:-


你没有错过这里的一个
*/
/*处理正确的节点
?是的。我的vi编辑器是一种颜色的。无法解决。但是指向指针的问题仍然没有解决。现在它正在运行代码!!我仍然无法确定它是插入节点时指向指针的指针。有人可以ovide引用。或者它的原因。所以,当我们传递指针时,我们只能更改它指向的数据。当我们使用指针指向指针时,我们可以更改它指向的地址。非常感谢您的帮助,先生!