728x90
반응형
TNode* search(TNode* n, int key) {
if (n == NULL) return NULL;
else if (key == n->data) return n;
else if (key < n->data) return search(n->left, key);
else return search(n->right, key);
}
int insert(TNode* r, TNode* n) {
if (n->data == r->data) return 0;
else if (n->data < r->data) {
if (r->left == NULL) r->left = n;
else insert(r->left, n);
}
else {
if (r->right == NULL) r->right = n;
else insert(r->right, n);
}
return 1;
}
void delete(TNode* parent, TNode* node) {
TNode* child, * succ, * succp;
// case 1
if ((node->left == NULL && node->right == NULL)) {
if (parent == NULL) root = NULL;
else {
if (parent->left == node) {
parent->left = NULL;
}
else {
parent->right = NULL;
}
}
}
// case 2
else if (node->left == NULL || node->right == NULL) {
child = (node->left != NULL) ? node->left : node->right;
if (node == root) root = child;
else {
if (parent->left == node) {
parent->left = child;
}
else {
parent->right = child;
}
}
}
// case 3
else {
succp = node;
succ = node->right;
while (succ->left != NULL) {
succp = succ;
succ = succ->left;
}
if (succp->left == succ) {
succp->left = succ->right;
}
else {
succp->right = succ->right;
}
node->data = succ->data;
node = succ;
}
free(node);
}
728x90
반응형