About tree -- some code in C Primer - I don't understand

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xianwei

    About tree -- some code in C Primer - I don't understand

    First,

    typedef struct pair {
    Node *parent;
    Node *child;
    } Pair;

    static Pair SeekItem(cosnt Item *pI, const Tree *pTree)
    {
    Pair look;
    look.parent = NULL;
    look.child = pTree->root;

    if (pTree->root == NULL)
    {
    return look;
    }

    while (look.child != NULL)
    {
    if (ToLeft(pI, &(look.child->item)))
    {
    look.parent = look.child;
    look.child = look.child->left;
    }
    else if (ToRight(pI, &(look.child->item)))
    {
    look.parent = look.child;
    look.child = look.child->right;
    }
    else
    break;
    }
    return look;
    }


    bool DeleteItem(cons t Item *pi, Tree *pTree)
    {
    Pair look;

    look = SeekItem(pi, pTree);
    if (look.child == NULL)
    {
    return false;
    }
    else if (look.parent == NULL)
    {
    DeleteNode(&pTr ee->root);
    }
    else if (look.parent->left == look.child)
    {
    DeleteNode(&loo k.parent->left);
    }
    else
    {
    DeleteNode(&loo k.parent->right);
    }

    return true;
    }

    I don't know why the upper code use

    else if (look.parent->left == look.child)
    {
    DeleteNode(&loo k.parent->left);
    }
    else
    {
    DeleteNode(&loo k.parent->right);
    }
    I think use
    else
    {
    DeleteNode(&loo k.child);
    }
    have the some effect.
    The C primer is a all-known book, so I know is my wrong, but I don't
    wrong
    where it is.~~please point

    then, second
    static void DeleteNode(Node **ptr)
    {
    Node *temp;
    puts((*ptr)->item.petName );
    if ((*ptr)->left == NULL)
    {
    temp = *ptr;
    *ptr = (*ptr)->right;
    free(temp);
    }
    else if ((*ptr)->right == NULL)
    {
    temp = *ptr;
    *ptr = (*ptr)->left;
    free(temp);
    }
    else
    {
    ......
    ......
    }
    }

    I don't think the if else if clause will delete the node,and keep
    the tree "alive", ~
    if child_node->left == NULL
    you must let the parent node, left field point the child_node-
    >right,
    but the above don't do this, so I doubtful
    My English is so poor, I can't express clearly why I think the code
    is wrong~~,
    but I try my best :(

    The C primer is a all-known book, so I know somewhere is my wrong,
    but I don't know where it is.~~please point

  • Ben Bacarisse

    #2
    Re: About tree -- some code in C Primer - I don't understand

    xianwei <baikaishiuc@gm ail.comwrites:
    First,
    >
    typedef struct pair {
    Node *parent;
    Node *child;
    } Pair;
    >
    static Pair SeekItem(cosnt Item *pI, const Tree *pTree)
    {
    Pair look;
    <snip function body>
    return look;
    }
    >
    >
    bool DeleteItem(cons t Item *pi, Tree *pTree)
    {
    Pair look;
    look = SeekItem(pi, pTree);
    if (look.child == NULL)
    {
    return false;
    }
    else if (look.parent == NULL)
    {
    DeleteNode(&pTr ee->root);
    }
    else if (look.parent->left == look.child)
    {
    DeleteNode(&loo k.parent->left);
    }
    else
    {
    DeleteNode(&loo k.parent->right);
    }
    >
    return true;
    }
    >
    I don't know why the upper code use
    >
    else if (look.parent->left == look.child)
    {
    DeleteNode(&loo k.parent->left);
    }
    else
    {
    DeleteNode(&loo k.parent->right);
    }
    I think use
    else
    {
    DeleteNode(&loo k.child);
    }
    have the some effect.
    The C primer is a all-known book, so I know is my wrong, but I
    don't wrong
    DeleteNode is given a pointer to the Node, presumably so it may modify
    the data held there. look is a local variable, so &look.child refers
    to an object that will vanish soon (when DeleteItem returns) so any
    modifications to it will not be modification to the tree itself.

    From this sample, I am not overly impressed with the code in this
    book, but your change would change the program's meaning. I think the
    program could probably be written more clearly, though.
    where it is.~~please point
    >
    then, second
    static void DeleteNode(Node **ptr)
    {
    Node *temp;
    puts((*ptr)->item.petName );
    if ((*ptr)->left == NULL)
    {
    temp = *ptr;
    *ptr = (*ptr)->right;
    free(temp);
    }
    else if ((*ptr)->right == NULL)
    {
    temp = *ptr;
    *ptr = (*ptr)->left;
    free(temp);
    }
    else
    {
    ......
    ......
    }
    }
    >
    I don't think the if else if clause will delete the node,and keep
    the tree "alive", ~
    if child_node->left == NULL
    you must let the parent node, left field point the child_node-
    >>right,
    but the above don't do this, so I doubtful
    I not sure what problem you are seeing. I can't see any obvious
    problem with the code, but I am missing all the types and the "big
    picture" about what the code is for.
    My English is so poor, I can't express clearly why I think the code
    is wrong~~,
    but I try my best :(
    >
    The C primer is a all-known book, so I know somewhere is my wrong,
    You mean "well-known". Not all well-known books are good. Some are
    well-known for being bad. I don't know this one.

    --
    Ben.

    Comment

    • xianwei

      #3
      Re: About tree -- some code in C Primer - I don't understand

      On 9 20 , 12 10 , Ben Bacarisse <ben.use...@bsb .me.ukwrote:
      xianwei <baikaish...@gm ail.comwrites:
      First,
      >
      typedef struct pair {
      Node *parent;
      Node *child;
      } Pair;
      >
      static Pair SeekItem(cosnt Item *pI, const Tree *pTree)
      {
      Pair look;
      >
      <snip function body>
      >
      >
      >
      >
      >
      return look;
      }
      >
      bool DeleteItem(cons t Item *pi, Tree *pTree)
      {
      Pair look;
      look = SeekItem(pi, pTree);
      if (look.child == NULL)
      {
      return false;
      }
      else if (look.parent == NULL)
      {
      DeleteNode(&pTr ee->root);
      }
      else if (look.parent->left == look.child)
      {
      DeleteNode(&loo k.parent->left);
      }
      else
      {
      DeleteNode(&loo k.parent->right);
      }
      >
      return true;
      }
      >
      I don't know why the upper code use
      >
      else if (look.parent->left == look.child)
      {
      DeleteNode(&loo k.parent->left);
      }
      else
      {
      DeleteNode(&loo k.parent->right);
      }
      I think use
      else
      {
      DeleteNode(&loo k.child);
      }
      have the some effect.
      The C primer is a all-known book, so I know is my wrong, but I
      don't wrong
      >
      DeleteNode is given a pointer to the Node, presumably so it may modify
      the data held there. look is a local variable, so &look.child refers
      to an object that will vanish soon (when DeleteItem returns) so any
      modifications to it will not be modification to the tree itself.
      >
      From this sample, I am not overly impressed with the code in this
      book, but your change would change the program's meaning. I think the
      program could probably be written more clearly, though.
      >
      >
      >
      >
      >
      where it is.~~please point
      >
      then, second
      static void DeleteNode(Node **ptr)
      {
      Node *temp;
      puts((*ptr)->item.petName );
      if ((*ptr)->left == NULL)
      {
      temp = *ptr;
      *ptr = (*ptr)->right;
      free(temp);
      }
      else if ((*ptr)->right == NULL)
      {
      temp = *ptr;
      *ptr = (*ptr)->left;
      free(temp);
      }
      else
      {
      ......
      ......
      }
      }
      >
      I don't think the if else if clause will delete the node,and keep
      the tree "alive", ~
      if child_node->left == NULL
      you must let the parent node, left field point the child_node-
      >right,
      but the above don't do this, so I doubtful
      >
      I not sure what problem you are seeing. I can't see any obvious
      problem with the code, but I am missing all the types and the "big
      picture" about what the code is for.
      thanks, yesterday I spent some times, The C primer book is right,
      I have some miss understand about Node **(pointer to pointer)~~~.
      thank you enthusiasm.Some one is right, I should need read the source
      code again and again.
      >
      My English is so poor, I can't express clearly why I think the code
      is wrong~~,
      but I try my best :(
      >
      The C primer is a all-known book, so I know somewhere is my wrong,
      >
      You mean "well-known". Not all well-known books are good. Some are
      well-known for being bad. I don't know this one.
      Thank you recommendation. And I think C Primer is a good book~~,but it
      is true that not all "well-known" book are good.~~:-)

      Comment

      Working...