【问题标题】:Redirecting a pointer by reference in a Binary-Search-Tree在二叉搜索树中通过引用重定向指针
【发布时间】:2017-12-04 23:43:12
【问题描述】:

给定以下结构:

struct TElem {
  int val;
};

typedef int TKey;

struct Node {
  TKey key;
  struct TElem *elem;
  struct Node *left;
  struct Node *right;
};


struct bst {
  struct Node *root;
};

还有bst_Search和bst_Insert这两个函数。

struct TElem* bst_Search(struct bst *T, TKey c, struct Node **posins)
{
  struct Node *q = T->root;
  posins = &(T->root);                      // (1)

  while (q)
    if (q->key == c)
      return q->elem;
    else if ( c < q->key) {
      q = q->left;
      posins = &(q->left);
    } else {
      q = q->right;
      posins = &(q->right);
    }

  return NULL;
}

void bst_Insert(struct bst *T, TKey c, struct TElem *x)
{
  struct Node **posins;
  struct TElem *v;

  posins = malloc(sizeof *posins);
  v = bst_Search(T, c, posins);             // (2)

  if (!v) {
    struct Node *q = malloc(sizeof *q);
    q->key   = c;
    q->elem  = x;
    q->left  = NULL;
    q->right = NULL;
    *posins  = q;                           // (3)
  } else {
    printf("Key exists %d\n", c);
  }

  free(posins);
}

而main() 是“主要”

  struct bst *T = malloc(sizeof *T);
  T->root = NULL;

  struct TElem *x = elem_New(10);
  bst_Insert(T, c, x);

我正在尝试插入一个新元素(elem_New 工作正常)到 BST,使用辅助函数 bst_Search 返回指向正确位置的指针 插入。 (1) 在首先将posins 指向T-&gt;root 内存地址时被调用。 (据我调试,这一切正常)。

然后posins 地址返回到调用函数bst_Insert,因为bst_Search 在(2) 找到了返回NULL 的位置。它在if 语句中正确设置q。然后在 (3),我希望 posins 指向的位置(在这种情况下 T->root 也指向的地址)现在应该重定向到新的树节点 @987654338 @。等效于T-&gt;root = q,但通过引用使用此调用。

【问题讨论】:

  • 当您搜索树以找到存储节点的位置时,您无法真正返回 NULL 并在该位置进行分配。请记住,left 或 right 指针具有 NULL,因此您不能返回它并为其分配一些东西,否则您将遇到分段错误。一个简单的解决方案是返回父级并在其右指针上分配,或者复制搜索逻辑并添加到正确的位置。
  • @Isac bst_Search 返回NULL,但不在我正在使用的返回值(存储在v)中,我确实使用posins。
  • posins 将拥有来自posins = &amp;(q-&gt;right); 的NULL。请记住,当 q == NULL 时退出,这意味着 q-&gt;left 和 q-&gt;right 在上一次迭代中是 NULL。
  • posins 实际上毫无意义。
  • BLUEPIXY @Isac ,是的。有道理..正在努力。

标签: c pointers binary-search-tree pass-by-reference


【解决方案1】:

因为当你的“bst_Search”出来时你的“posins”没有改变

void test(int *p){
// g_c is a gobal int 
p = &g_c;
 printf("%X ",p);}

在调用 test(int *p) 时做你的想法 p的值会改变指向g_c吗?不,因为 p 只是一个参数,并且在退出过程时不会更改它的值

my english is poor  so i just hope the e.g. code will help your understand~ :)

【讨论】:

    【解决方案2】:

    您更新 q 的方式是错误的。让我们说这棵树是这样的:

    现在假设您搜索 10,bst_search(10)(为了简单和理解,我删除了您的参数)

    那么,这就是你所做的:

    posins = 5 // I am writing numeric values instead of addresses for understanding
    
    // Now
    10 > 5
    p = p->right // => p = 12
    posins = p->left // => posins = 9, cause p is pointing to 12
    
    Again, 10 < 12
    So, p = p->left; // => p = 9
    posins = p->left; // posins = NULL
    

    这是出错的地方,你应该指向 9,但你指向 9 的左边,这是你应该编码的:

    struct TElem* bst_Search(struct bst *T, TKey c, struct Node **posins)
    {
      struct Node *q = T->root;
      posins = &(T->root);                      // (1)
    
      while (q)
        if (q->key == c)
          return q->elem;
        else if ( c < q->key) {
          q = q->left;
          posins = &q;
        } else {
          q = q->right;
          posins = q;
        }
    
      return NULL;
    }
    

    如有任何疑问,请发表评论。

    【讨论】:

      【解决方案3】:

      主要问题是调用参数不能被posins = &amp;(T-&gt;root);之类的操作更新。

      93) 一个函数可以改变它的参数值,但是这些 更改不会影响参数的值。另一方面, 可以将指针传递给对象,并且函数可以 改变指向的对象的值。

      因此,如果要将调用变量更改为P 的值,则必须为*P。
      即struct Node **posins 应该是struct Node ***posins。

      整体代码如下所示。

      #include <stdio.h>
      #include <stdlib.h>
      
      struct TElem {
          int val;
      };
      
      typedef int TKey;
      
      struct Node {
          TKey key;
          struct TElem *elem;
          struct Node *left;
          struct Node *right;
      };
      
      struct bst {
          struct Node *root;
      };
      
      int TKeyCmp(TKey a, TKey b){
      /* return value :
      ** a = b : 0
      ** a > b : positive value
      ** a < b : negative value
      */
          return a < b ? -1 : a > b;
      }
      struct TElem *elem_New(int value){
          struct TElem *ep = malloc(sizeof(*ep));//check omitted
          ep->val = value;
          return ep;
      }
      void elem_free(struct TElem *ep){
          free(ep);
      }
      void print(struct Node *np){
          if(np){
              print(np->left);
              printf("(%d, %d)", np->key, np->elem->val);
              print(np->right);
          }
      }
      
      struct TElem *bst_Search(struct bst *T, TKey c, struct Node ***posins){
          *posins = &T->root;
      
          while (**posins){
              int cmp = TKeyCmp(c, (**posins)->key);
              if(cmp == 0)
                  return (**posins)->elem;
              else if ( cmp < 0)
                  *posins = &(**posins)->left;
              else
                  *posins = &(**posins)->right;
          }
      
          return NULL;
      }
      
      void bst_Insert(struct bst *T, TKey c, struct TElem *x){
          struct Node **posins;
      
          if (!bst_Search(T, c, &posins)) {
              struct Node *q = malloc(sizeof *q);
              q->key   = c;
              q->elem  = x;
              q->right = q->left  = NULL;
              *posins  = q;
          } else {
              elem_free(x);//avoid memory leak
              printf("Key exists %d\n", c);
          }
      }
      
      int main(void){
          struct bst *T = malloc(sizeof(*T));
      
          T->root = NULL;
      
          bst_Insert(T, 5, elem_New(10));
          bst_Insert(T, 1, elem_New(21));
          bst_Insert(T, 9, elem_New(42));
          bst_Insert(T, 1, elem_New(99));//Key exists 1
          print(T->root);//(1, 21)(5, 10)(9, 42)
          //release bst
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-20
        • 2020-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多