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-
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
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 doubtfulright,
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