gpt4 book ai didi

c - 二叉树 - 指针作为参数

转载 作者:行者123 更新时间:2023-11-30 15:22:51 24 4
gpt4 key购买 nike

大家好,感谢您抽出时间。所以今天我开始阅读有关二叉树的内容。我决定使用递归函数自己尝试一下,但我不确定我是否使用了正确的指针。(我显然不是因为错误,但我不知道要改变什么......请给我一点指导?: (

快速提示:

hojaNodo = leafNode(西类牙语)。

31:27: error: member reference type 'hojaNodo' (aka 'struct node') is not a pointer; maybe you meant to use '.'? destroy_tree((*leaf)->right);

31:20: warning: incompatible pointer types passing 'struct node *' to parameter of type 'hojaNodo **' (aka 'struct node **') [-Wincompatible-pointer-types] destroy_tree((*leaf)->right);

10:30: note: passing argument to parameter 'leaf' here void destroy_tree(hojaNodo **leaf);

32:12: error: passing 'hojaNodo' (aka 'struct node') to parameter of incompatible type 'void *' free(*leaf);

#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()

救命啊!请...:(

最佳答案

编译器在 destroy_tree 中给出了一些错误。修复它们后,函数变为:

void destroy_tree(hojaNodo **leaf){

if(*leaf != 0)
{
destroy_tree(&(*leaf)->left);
destroy_tree(&(*leaf)->right);
free(*leaf);
}
}

我没有运行代码,但看起来没问题。我仍然对 tempRoot 感到好奇。为什么不简单地使用root?

关于c - 二叉树 - 指针作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29014719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com