简体   繁体   English

如何返回 C 中树中特定值的指针?

[英]how to return the pointer of a specific value in a tree in C?

using this struct:使用这个结构:

typedef struct node {
  int content;
  struct node* right;
  struct node* left;
}Node;

I'm trying to write a code that return the pointer of the the node if node ->content== x I have tried this so far but I don't think its right:我正在尝试编写一个代码来返回节点的指针 if node ->content== x 我到目前为止已经尝试过了,但我认为它不正确:

 Node* find (Node* p,int x)
{
  Node* L,*R;
    if(p==NULL)
    retutn NULL;
  L=find(p->left,x);
    if(L->content == x)
    return L;
  R=find(p->right,x);
    if(R->content == x)
  return R;
}

can u help me correct my code?你能帮我纠正我的代码吗?

First, the code doesn't even compile because of retutn .首先,由于retutn ,代码甚至无法编译。


L=(p->left,x);    // Equivalent to: L = x;

should be应该

L=find(p->left,x);

Same idea for the right side.右侧的想法相同。


L could be NULL. L可以是 NULL。

if(L->content == x)

should be应该

if(L != NULL)

or just要不就

if(L)

Same idea for the right side.右侧的想法相同。


You never check if the current node is a match.您永远不会检查当前节点是否匹配。 You need to add the following:您需要添加以下内容:

if ( p->content == x )
   return p;

The following is correct:以下是正确的:

Node* L, *R;

However, it's very easy to accidentally do但是,很容易不小心做

Node* L, R;

Maybe you should avoid grouping such declarations.也许您应该避免对此类声明进行分组。 On the plus side, this should be caught at compile-time (unless you avoid turning on even the most basic warnings).从好的方面来说,这应该在编译时被捕获(除非你避免打开最基本的警告)。


All together,全部一起,

Node* find(Node* p, int x)
{
   if ( !p )
      return NULL;

   if ( p->content == x )
      return p;

   Node* L = find( p->left, x );
   if ( L )
      return L;

   Node* R = find( p->right, x );
   if ( R )
      return R;

   return NULL;
}

The part of the function starting from these statements从这些语句开始的 function 部分

L=find(p->left,x);
  if(L->content == x)
  return L;

is incorrect.是不正确的。 For starters it is unclear why the node pointed to by the pointer p is not checked.对于初学者来说,不清楚为什么不检查指针 p 指向的节点。 And the call of the function can return a null pointer.而function的调用可以返回一个null指针。 So this if statement can invoke undefined behavior.所以这个 if 语句可以调用未定义的行为。

The function definition depends on whether it is a binary search tree or not. function 定义取决于它是否是二叉搜索树。 That is whether nodes of the tree are ordered.那就是树的节点是否有序。

If it is not a binary search tree then the function can be defined the following way如果它不是二叉搜索树,则可以通过以下方式定义 function

Node * find( const Node *root, int x )
{
    if ( root == NULL )
    {
        return NULL;
    }
    else if ( root->content == x )
    {
        return ( Node * )root;
    }
    else
    {
        Node *p = find( root->left, x );
        p == NULL ? return find( root->right, x ) : p;
    }
}

If the tree is a binary search tree then the function can look the following way如果树是二叉搜索树,那么 function 可以如下所示

Node * find( const Node *root, int x )
{
    if ( root == NULL )
    {
        return NULL;
    }
    else if ( x < root->content )
    {
        return find( root->left, x );
    }
    else if ( root->content < x )
    {
        return find( root->right, x );
    }
    else
    {
        return ( Node * )root;
    }
}

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

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