在這裏,我寫了一個代碼來實現二進制搜索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