在这里,我写了一个代码来实现二进制搜索tree.it不给任何错误,同时插入根node.But每当我试图插入子节点,我得到以下警告程序崩溃,而在二叉搜索树插入第二个节点
传递推兼容的指针类型的参数1
预期结构节点**但参数是结构节点*
传递推兼容的指针类型的参数1的
,然后程序crashes.What可能出错,这代码?
#include<stdio.h>
struct node {
int data;
struct node *left;
struct node *right;
};
void insert(int value);
void push(struct node **root_node,struct node *newNode);
void search(struct node *root_node,int value);
struct node *root;
int main(){
root= NULL;
int option,value;
for(;;){
printf("Please select an option from below : \n");
printf("1 for insert\n");
printf("2 for search\n");
printf("please enter your option : ");
scanf("%d",&option);
printf("\n");
switch(option){
case 1:
printf("you choose to insert\n");
printf("input your value :");
scanf("%d",&value);
insert(value);
printf("\n");
break;
default:
break;
}
}
}
void insert(int value){
struct node newNode ;
newNode.data = value;
newNode.left = NULL;
newNode.right = NULL;
push(&root,&newNode);
}
void push(struct node **root_node,struct node *newNode){
if(*root_node==NULL){
*root_node = newNode;
printf("inserted\n\n\n");
}else{
if((*root_node)->data > newNode->data){
push((*root_node)->left,newNode);
printf("left\n");
}else{
push((*root_node)->right,newNode);
printf("right\n");
}
}
}
1) 'struct node newNode;':'newNode'的生命周期在本地范围内。 2)'push((* root_node) - > left,newNode);' - >'push(&(* root_node) - > left,newNode);' – BLUEPIXY