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

X

xianwei

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(const Item *pi, Tree *pTree)
{
Pair look;

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

return true;
}

I don't know why the upper code use

else if (look.parent->left == look.child)
{
DeleteNode(&look.parent->left);
}
else
{
DeleteNode(&look.parent->right);
}
I think use
else
{
DeleteNode(&look.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-
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
 
B

Ben Bacarisse

xianwei said:
First,

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

static Pair SeekItem(cosnt Item *pI, const Tree *pTree)
{
Pair look;
return look;
}


bool DeleteItem(const Item *pi, Tree *pTree)
{
Pair look;
look = SeekItem(pi, pTree);
if (look.child == NULL)
{
return false;
}
else if (look.parent == NULL)
{
DeleteNode(&pTree->root);
}
else if (look.parent->left == look.child)
{
DeleteNode(&look.parent->left);
}
else
{
DeleteNode(&look.parent->right);
}

return true;
}

I don't know why the upper code use

else if (look.parent->left == look.child)
{
DeleteNode(&look.parent->left);
}
else
{
DeleteNode(&look.parent->right);
}
I think use
else
{
DeleteNode(&look.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-
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.
 
X

xianwei

<snip function body>










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.








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.Someone is right, I should need read the source
code again and again.
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.~~:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top