[r]
(1)#include <iostream.h> #include <iomanip.h>
typedef struct nodet { elem data;
struct nodet *left, *right; } node;
typedef node *tree;
void inserttree(tree &t, elem x, int (*comp)(elem, elem)) {
if (t==NULL) {
t = new node;
memcpy(&t->data, &x, sizeof(elem)); t->left = t->right = NULL;
} else
if (comp(x,t->data)<0)
inserttree(t->left, x, comp); else
if (comp(x,t->data)>0)
(2)void del(tree &r, tree &q) {
if (r->right!=NULL) del(r->right, q); else
{
q->data = r->data; q = r;
r = r->left; }
}
void deletetree(tree &t, elem x, int (*comp)(elem, elem)) {
if (t!=NULL)
if ((comp(x,t->data)<0))
deletetree(t->left, x, comp); else
if ((comp(x,t->data)>0))
deletetree(t->right, x, comp); else
{
tree q = t;
(3)else
if (t->left==NULL) t = t->right; else
del(t->left, q); delete q;
} }
tree searchtree(tree t, elem x, int (*comp)(elem, elem)) {
if (t==NULL)
return NULL; if ((comp(x,t->data)<0))
return searchtree(t->left, x, comp); if ((comp(x,t->data)>0))
return searchtree (t->right, x, comp); return t;