【问题标题】:Binary Tree - Pointers as Parameters二叉树 - 指针作为参数
【发布时间】:2015-03-12 16:06:29
【问题描述】:

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

快速指针:

hojaNodo = leafNode(西班牙语)。

31:27:错误:成员引用类型“hojaNodo” (aka 'struct node') 不是指针;也许您打算使用“。”? destroy_tree((*leaf)->right);

31:20:警告:不兼容的指针类型传递 'struct node *' 到'hojaNodo **' 类型的参数(又名'struct node **') [-Wincompatible-pointer-types] destroy_tree((*leaf)->right);

10:30:注意:在此处将参数传递给参数“叶子”无效 destroy_tree(hojaNodo **leaf);

32:12:错误:将“hojaNodo”(又名“结构节点”)传递给 不兼容类型的参数'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()

帮助!请... :(

【问题讨论】:

    标签: c pointers tree structure treenode


    【解决方案1】:

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

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

    我没有运行代码,但它看起来不错。我仍然想知道 tempRoot。为什么不简单地使用 root?

    【讨论】:

    • 现在我为自己感到难过,我看到了,但我想我已经尝试过改变它......非常感谢您的时间和帮助。这是我养成的坏习惯,我知道我应该停止这样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 2013-07-15
    相关资源
    最近更新 更多