【问题标题】:C binary search tree implementation - insertC二叉搜索树实现——插入
【发布时间】:2013-02-18 07:49:41
【问题描述】:

我正在尝试创建一个程序,该程序将单词列表作为输入,并将它们排序成二叉树,以便找到它们,例如像字典一样。这是我到目前为止所做的,但是我收到newEl -> el = input; 的分段错误我知道这是因为它在首次创建树时试图指向一个 NULL el,但我不确定什么是最好的改进我的代码的方法是。有人有想法么?谢谢。

struct node *tra(struct node * start, Type input) {
  struct node * thisNode = start;

  if (thisNode == NULL)

    Type current = thisNode -> el;

    if (strcmp(input, current) > 0)
        return tra(thisNode -> right, input);

    else if (strcmp(input, current) < 0)
        return tra(thisNode -> left, input);

    else
        return thisNode;
  }
}

Ta insert(Type input, Ta ta) {
  if ((find(input, ta)) == FALSE) {
    newEl -> el = input;

  }

  return ta;
}

Boolean find(Type input, Ta ta) {
    if (tra(ta -> head, input) == NULL)
        return FALSE;
    }

【问题讨论】:

  • 如果 find() 返回 false,tra 也将返回 NULL,并且对 newEl->XXX 的赋值将取消引用 NULL 指针。无论如何,最优雅的解决方案涉及到指针。
  • struct node *newEl = tra(ta -&gt; head, input); 将返回您现有的项目或 null。它永远不会给你一个指向新对象的指针。
  • 我知道,但我不知道如何解决它
  • 对不起,我开始看代码了。文字后来来了。 (我不喜欢文字 ;-)
  • 似乎您想创建一个新节点,但我看不到您为新节点分配空间的任何地方(例如“newEl = (struct node*)malloc(sizeof(struct node) );").

标签: c binary-tree binary-search-tree


【解决方案1】:

这是一个指向等效指针的指针:

typedef char *Type;
struct node {
  struct node *left , *right;
  Type payload;
  };    

struct node **find_pp(struct node **pp, Type input) {
  struct node *thisNode ;

  while ( thisNode = *pp ) {
    int diff;
    diff = strcmp(input, thisNode->payload);
    if (!diff) break;
    pp = (diff <0) ? &thisNode->left : &thisNode->right;
  }
return pp;
}

Boolean find(struct node *root, Type thing)
{
  struct node **pp;
  pp = find_pp( &root, thing);
  return (*pp) ? True : False;
}

void insert (struct node **pp, Type thing)
{
  struct node *newNode;
  pp = find_pp (pp, thing);

  if (*pp) return; /* already exists */
  *pp = newNode = malloc (sizeof *newnode);
  newNode->payload = strdup(thing);
  newNode->left = NULL;
  newNode->right = NULL;

return;
}

一些注意事项:

  • 将节点插入树意味着:分配给以前为 NULL 的指针
  • 空树也是一棵树:只是一个恰好为空的指针(指向树的根)
  • 在树中找到一个节点意味着:找到应该在的位置 (:=pointer)(如果存在的话)
  • 如果它不存在,这个指针正是应该插入它以使其存在的地方
  • 绘制图表(用纸和铅笔)会有所帮助。

【讨论】:

    【解决方案2】:

    既然您已经知道问题所在,那么您应该解决它。分配节点并插入。

    Ta insert(Type input, Ta ta) {
      if ((find(input, ta)) == FALSE) {
        // call to tra will fail. this is the place to create a new node
        struct node *newEl = (struct node*)  malloc(sizeof(struct node));
        newEl -> el = input;
        newEl -> left = 0;
        newEl -> right = 0;
        // do the insertion ....
      }
    }
    

    【讨论】:

      【解决方案3】:

      似乎您想创建一个新节点,但我没有看到您为新节点分配空间的任何地方,例如:

      newEl = (struct node*)malloc(sizeof(struct node));
      

      祝你好运!

      【讨论】:

      • 这仍然会导致调用者无法访问 newNode。顺便说一句:您不应该强制转换 malloc() 的返回值。它不是必需的,并且有潜在危险。
      • 为什么会有危险?
      • @wildplasser 你在重复我强烈反对的模因;请参阅常见问题解答。
      • FAQ , which FAQ, the c.l.c FAQ ?
      • stackoverflow.com/questions/605845/… 我接近底部了:-)。
      猜你喜欢
      • 1970-01-01
      • 2017-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多