二叉树 - 指针作为参数

Binary Tree - Pointers as Parameters

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

快速指南:

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?