简体   繁体   English

二叉树-指针作为参数

[英]Binary Tree - Pointers as Parameters

Hello everyone thank you for your time. 大家好,感谢您的宝贵时间。 So today I started reading about binary trees. 因此,今天我开始阅读有关二叉树的文章。 I decided to give it a try on my own using recursive functions but Im not sure if Im using the correct pointers.(Im obviously not because of errors but I don't see what to change...a little guidance, please? :( 我决定使用递归函数自行尝试一下,但是我不确定我是否使用正确的指针。(我显然不是因为错误,但我看不到要更改什么...请稍作指导?: (

Quick pointers: 快速提示:

hojaNodo = leafNode(its in spanish). hojaNodo = leafNode(西班牙语)。

31:27: error: member reference type 'hojaNodo' (aka 'struct node') is not a pointer; 31:27:错误:成员引用类型'hojaNodo'(又名“结构节点”)不是指针; maybe you meant to use '.'? 也许您打算使用'。'? destroy_tree((*leaf)->right); destroy_tree((*叶) - >右);

31:20: warning: incompatible pointer types passing 'struct node *' to parameter of type 'hojaNodo **' (aka 'struct node **') [-Wincompatible-pointer-types] destroy_tree((*leaf)->right); 31:20:警告:不兼容的指针类型将“结构节点*”传递给类型为“ hojaNodo **”的参数(又名“结构节点**”)[-Wincompatible-pointer-types] destroy_tree((* leaf)-> right );

10:30: note: passing argument to parameter 'leaf' here void destroy_tree(hojaNodo **leaf); 10:30:注意:在此处将参数传递给参数'leaf'无效destroy_tree(hojaNodo ** leaf);

32:12: error: passing 'hojaNodo' (aka 'struct node') to parameter of incompatible type 'void *' free(*leaf); 32:12:错误:将'hojaNodo'(又名'结构节点')传递给不兼容类型'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()

Help! 救命! Please... :( 请... :(

The compiler gave some errors in destroy_tree. 编译器在destroy_tree中给出了一些错误。 After fixing them the function becomes: 修复它们后,功能将变为:

void destroy_tree(hojaNodo **leaf){

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

I didn't run the code but it looks OK. 我没有运行代码,但看起来还可以。 I still wonder about tempRoot. 我仍然想知道tempRoot。 Why not simply use root? 为什么不简单地使用root?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM