大家好,谢谢你的时间。 所以今天我开始阅读有关二叉树的内容。我决定尝试使用递归函数自己尝试但是我不确定我是否使用了正确的指针。(我显然不是因为错误而是我看不出要改变什么......请给我一点指导?: (
快速指针:
hojaNodo = leafNode(西班牙语)。
31:27:错误:成员引用类型'hojaNodo' (又名'struct node')不是指针;也许你打算用'。'? destroy_tree((*叶) - >右);
31:20:警告:不兼容的指针类型传递 'struct node *'到'hojaNodo **'类型的参数(又名'struct node **') [-Wincompatible指针类型] destroy_tree((*叶) - >右);
10:30:注意:将参数传递给参数'leaf'这里为void destroy_tree(hojaNodo ** leaf);
32:12:错误:将'hojaNodo'(又名'struct node')传递给 不兼容类型'void *'的参数 自由(*叶);
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int valor;
struct node *left;
struct node *right;
}hojaNodo;
void destroy_tree(hojaNodo **leaf);
void insert(int valor, hojaNodo **leaf);
hojaNodo *search(int valor, hojaNodo *leaf);
int menu(int eventHandler, hojaNodo **root);
int opcion_menu();
int main(){
int eventHandler;
hojaNodo *treeRoot = NULL;
intro();
eventHandler = opcion_menu();
menu(eventHandler, &treeRoot);
}
void destroy_tree(hojaNodo *leaf){
if(*leaf != 0)
{
destroy_tree((*leaf)->left);
destroy_tree((*leaf)->right);
free(*leaf);
}
}
void insert (int valor, hojaNodo **leaf)
{
if(*leaf == 0)
{
*leaf=(hojaNodo*)malloc(sizeof(struct node));
(*leaf)->valor = valor;
/* init los hijos a null */
(*leaf)->left = 0;
(*leaf)->right = 0;
}//eof check doesnt exist
else if(valor < (*leaf)->valor)
{
insert(valor, &(*leaf)->left);
}//eof elseif
else if(valor > (*leaf)->valor)
{
insert(valor, &(*leaf)->right);
}//eof elseif
}//eof insert
hojaNodo *search(int valor,hojaNodo *leaf)
{
if(leaf != 0){
if(valor == leaf->valor){
return leaf;
}//eof es el mismo
else if(valor< leaf->valor)
{
return search(valor, leaf->left);
}//eof si el valor es menor, vete a la izquierda.
else
{
return search(valor, leaf->right);
}//eof si el valor es mayor, vete a la derecha.
}//eof check leaf is NOT NULL
else return 0;
}//eof search
int opcion_menu(){
int userOption;
printf("1.Add Number.\n");
printf("2.Display Numbers\n");
printf("3.Delete Leaf\n");
printf("4.Exit");
scanf("%d",&userOption);
printf("User Option: %d\n", userOption);
return userOption;
}//eof opcion_menu
int menu(int userOption,hojaNodo **root){
hojaNodo *tempRoot = NULL;
tempRoot = *root;
int valor;
switch(userOption){
case 1:
printf("Gimme a number!\n");
printf(".");
scanf("%d",&valor);
insert(valor, &tempRoot);
break;
case 2:
printf("List Numbers\n");
// printList(tempRoot);
break;
case 3:
// printf("Eliminar Hoja");
destroy_tree(root);
break;
case 4:
printf("Exit");
userOption = -1;
break;
default:
printf("userOption Error, Bye!");
break;
}//eof switch
if(userOption != -1)
menu(opcion_menu(),&tempRoot);
return userOption;
}//eof menu()
int intro(){
char input[2];
printf("Welcome, this is an example of a binary tree.\n");
printf("Press Enter.");
fgets(input,sizeof(input),stdin);
if(input[0]=='\n') return 1;
else return 0;
}//eof intro()
帮助!请...... :(
答案 0 :(得分:1)
编译器在destroy_tree中给出了一些错误。修复后,功能变为:
void destroy_tree(hojaNodo **leaf){
if(*leaf != 0)
{
destroy_tree(&(*leaf)->left);
destroy_tree(&(*leaf)->right);
free(*leaf);
}
}
我没有运行代码,但看起来还不错。我仍然想知道tempRoot。为什么不简单地使用root?