473,473 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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(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-
>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

Sep 19 '07 #1
2 1755
xianwei <ba*********@gmail.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(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-
>>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.
Sep 19 '07 #2
On 9 20 , 12 10 , Ben Bacarisse <ben.use...@bsb.me.ukwrote:
xianwei <baikaish...@gmail.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(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-
>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.Someone 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.~~:-)

Sep 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
77
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
4
by: mitsura | last post by:
Hi, I am writing a program in Python and I am using wx.Python for the GUI. I have no prior GUI and Python experience so that's why I turn to the specialists for aid. Basically, my app is a...
2
by: Sky | last post by:
Basically, I'm stumped on how to translate something I wrote in PHP to ASP.NET, and I'm having a hard time figuring out what is right way to do it now... The scenario in PHP was as follows: I...
1
by: pauldepstein | last post by:
Hi All, First, perhaps some would be kind enough to help me, once I state my approximate background I have written a moderately complex c++ program consisting of 11 files, all Bloodshed Dev. ...
1
by: 418928 | last post by:
Hi everybody, I have some doubts about RDF. I hope you can help me with them: -In http://www.w3.org/TR/rdf-primer, they use the namespace "http://www.example.org/terms/">", but the URL...
2
by: arnuld | last post by:
/* C++ Primer - 4/e * exercise - 4.16 */ #include <iostream> int main() {
4
by: sharan | last post by:
Hello Friends I have a problem in Data Structure (In C) i want to implement a General tree having three child nodes of each parent node ...please send me any simple Example( code) by which i can...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.