赞
踩
#include <stdio.h>
#include <stdlib.h>
#define KEY(n) (n ? n->key : 0)
typedef struct Node {
int key;
struct Node *lchild, *rchild;
} Node;
Node *getNewNode(int key) {
Node *p = (Node *)malloc(sizeof(Node));
p->key = key;
p->lchild = p->rchild = NULL;
return p;
}
int search(Node *root, int val) {
if (root == NULL) return 0;
if (root->key == val) return 1;
if (val < root->key) return search(root->lchild, val);
return search(root->rchild, val);
}
Node *insert(Node *root, int key) {
if (root == NULL) return getNewNode(key);
if (root->key == key) return root;
if (key < root->key) root->lchild = insert(root->lchild, key);
else root->rchild = insert(root->rchild, key);
return root;
}
Node *predecessor(Node *root) {
Node *temp = root->lchild;
while (temp->rchild) temp = temp->rchild;
return temp;
}
Node *erase(Node *root, int key) {
if (root == NULL) return NULL;
if (key < root->key) {
root->lchild = erase(root->lchild, key);
} else if (key > root->key) {
root->rchild = erase(root->rchild, key);
} else {
if (root->lchild == NULL && root->rchild == NULL) {
free(root);
return NULL;
} else if (root->lchild == NULL || root->rchild == NULL) {
Node *temp = root->lchild ? root->lchild : root->rchild;
free(root);
return temp;
} else {
Node *temp = predecessor(root);
root->key = temp->key;
root->lchild = erase(root->lchild, temp->key);
}
}
return root;
}
void clear(Node *root) {
if (root == NULL) return ;
clear(root->lchild);
clear(root->rchild);
free(root);
return ;
}
void output(Node *root) {
if (root == NULL) return ;
output(root->lchild);
printf("(%d, %d, %d)\n", KEY(root), KEY(root->lchild), KEY(root->rchild));
output(root->rchild);
return ;
}
int main() {
int op, val;
Node *root = NULL;
while (~scanf("%d%d", &op, &val)) {
switch (op) {
case 0: printf("search %d, result : %d\n", val, search(root, val)); break;
case 1: root = insert(root, val); break;
case 2: root = erase(root, val); break;
}
if (op) {
output(root);
printf("------------\n");
}
}
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。