-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
87 lines (79 loc) · 2.38 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*************************************************************************
> File Name: test.c
> Author: snowflake
> Mail: ︿( ̄︶ ̄)︿
> Created Time: 2018年09月23日 星期日 15时50分29秒
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct BSTNode {
int key;
struct BSTNode *lchild, *rchild;
} BSTNode;
BSTNode *init(int key) {
BSTNode *p = (BSTNode *)malloc(sizeof(BSTNode));
p->key = key;
p->lchild = p->rchild = NULL;
return p;
}
BSTNode *insert(BSTNode *root, int key) {
if (root == NULL) return init(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;
}
BSTNode *predecesor(BSTNode *node) {
BSTNode *temp = node->lchild;
while (temp->rchild) temp = temp->rchild;
return temp;
}
BSTNode *delete_node(BSTNode *root, int key) {
if (root == NULL) return root;
if (root->key == key) {
if (root->lchild == NULL && root->rchild == NULL) {
free(root);
return NULL;
} else if (root->lchild == NULL || root->rchild == NULL) {
BSTNode *temp = root->lchild ? root->lchild : root->rchild;
free(root);
return temp;
} else {
BSTNode *temp = predecesor(root);
temp->key ^= root->key;
root->key ^= temp->key;
temp->key ^= root->key;
root->lchild = delete_node(root->lchild, key);
}
} else {
if (root->key < key) root->rchild = delete_node(root->rchild, key);
else root->lchild = delete_node(root->lchild, key);
}
return root;
}
void output(BSTNode *root) {
if (root == NULL) return ;
output(root->lchild);
printf("%d ", root->key);
output(root->rchild);
return ;
}
int main() {
srand(time(0));
BSTNode *root = NULL;
#define OP_NUM 20
for (int i = 0; i < OP_NUM; i++) {
int key = rand() % 30;
root = insert(root, key);
printf("insert %d to tree\n", key);
output(root), printf("\n");
}
for (int i = 0; i < OP_NUM; i++) {
int key = rand() % 30;
root = delete_node(root, key);
printf("delete %d from tree\n", key);
output(root), printf("\n");
}
return 0;
}