#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node
{
int value;
struct Node *left;
struct Node *right;
};
struct Node *newNode(int value)
{
struct Node
*p
= malloc(sizeof(*p
)); p->value = value;
p->left = NULL;
p->right = NULL;
return p;
}
struct Node *addNode(struct Node *p, int value)
{
if (p == NULL)
{
return newNode(value);
}
else
{
if (value < p->value)
{
p->left = addNode(p->left, value);
}
else
{
p->right = addNode(p->right, value);
}
return p;
}
}
struct Node *deleteMinNode(struct Node *p)
{
struct Node* r;
if (p == NULL)
{
return NULL;
}
else
{
if (p->left == NULL)
{
r = p->right;
return r;
}
else
{
p->left = deleteMinNode(p->left);
return p;
}
}
}
struct Node *deleteNode(struct Node *p, int value, int *deleted)
{
struct Node *r;
struct Node *n;
if (p == NULL)
{
*deleted = 0;
return NULL;
}
else
{
if (value < p->value)
{
p->left = deleteNode(p->left, value, deleted);
return p;
}
else if (p->value < value)
{
p->right = deleteNode(p->right, value, deleted);
return p;
}
else
{
if (p->left == NULL)
{
r = p->right;
}
else if (p->right == NULL)
{
r = p->left;
}
else
{
n = p->right;
while (n->left != NULL)
{
n = n->left;
}
p->value = n->value;
p->right = deleteMinNode(p->right);
r = p;
}
*deleted = 1;
return r;
}
}
}
void writeNode(struct Node *p)
{
if (p == NULL)
{
}
else
{
writeNode(p->left);
writeNode(p->right);
}
}
void destroyNode(struct Node *p)
{
if (p == NULL)
{
return;
}
else
{
destroyNode(p->left);
destroyNode(p->right);
}
}
int main(int argc, char *argv[])
{
int i;
int deleted;
struct Node *p = NULL;
for (i = 1; i < argc; i++)
{
if (strcmp(argv
[i
], "--") == 0) {
break;
}
else
{
p
= addNode
(p
, atoi(argv
[i
])); }
}
writeNode(p);
for (i = i + 1; i < argc; i++)
{
p
= deleteNode
(p
, atoi(argv
[i
]), &deleted
);
if (deleted)
{
writeNode(p);
}
else
{
printf("deleteNode: 指定キーのノードがありません\n"); }
}
destroyNode(p);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbm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H0KfQoKc3RydWN0IE5vZGUgKmRlbGV0ZU5vZGUoc3RydWN0IE5vZGUgKnAsIGludCB2YWx1ZSwgaW50ICpkZWxldGVkKQp7CiAgc3RydWN0IE5vZGUgKnI7CiAgc3RydWN0IE5vZGUgKm47CgogIGlmIChwID09IE5VTEwpCiAgewogICAgKmRlbGV0ZWQgPSAwOwogICAgcmV0dXJuIE5VTEw7CiAgfQogIGVsc2UKICB7CiAgICBpZiAodmFsdWUgPCBwLT52YWx1ZSkKICAgIHsKICAgICAgcC0+bGVmdCA9IGRlbGV0ZU5vZGUocC0+bGVmdCwgdmFsdWUsIGRlbGV0ZWQpOwogICAgICByZXR1cm4gcDsKICAgIH0KICAgIGVsc2UgaWYgKHAtPnZhbHVlIDwgdmFsdWUpCiAgICB7CiAgICAgIHAtPnJpZ2h0ID0gZGVsZXRlTm9kZShwLT5yaWdodCwgdmFsdWUsIGRlbGV0ZWQpOwogICAgICByZXR1cm4gcDsKICAgIH0KICAgIGVsc2UKICAgIHsKICAgICAgaWYgKHAtPmxlZnQgPT0gTlVMTCkKICAgICAgewogICAgICAgIHIgPSBwLT5yaWdodDsKICAgICAgICBmcmVlKHApOwogICAgICB9CiAgICAgIGVsc2UgaWYgKHAtPnJpZ2h0ID09IE5VTEwpCiAgICAgIHsKICAgICAgICByID0gcC0+bGVmdDsKICAgICAgICBmcmVlKHApOwogICAgICB9CiAgICAgIGVsc2UKICAgICAgewogICAgICAgIG4gPSBwLT5yaWdodDsKCiAgICAgICAgd2hpbGUgKG4tPmxlZnQgIT0gTlVMTCkKICAgICAgICB7CiAgICAgICAgICBuID0gbi0+bGVmdDsKICAgICAgICB9CgogICAgICAgIHAtPnZhbHVlID0gbi0+dmFsdWU7CiAgICAgICAgcC0+cmlnaHQgPSBkZWxldGVNaW5Ob2RlKHAtPnJpZ2h0KTsKICAgICAgICByID0gcDsKICAgICAgfQoKICAgICAgKmRlbGV0ZWQgPSAxOwogICAgICByZXR1cm4gcjsKICAgIH0KICB9C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AgICAgIHdyaXRlTm9kZShwKTsKICAgICAgcHJpbnRmKCJcbiIpOwogICAgfQogICAgZWxzZQogICAgewogICAgICBwcmludGYoImRlbGV0ZU5vZGU6IOaMh+WumuOCreODvOOBruODjuODvOODieOBjOOBguOCiuOBvuOBm+OCk1xuIik7CiAgICB9CiAgfQoKICBkZXN0cm95Tm9kZShwKTsKCiAgcmV0dXJuIDA7Cn0=