-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_sort.c
145 lines (135 loc) · 2.41 KB
/
tree_sort.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode{
int data;
struct TreeNode *lc, *rc;
}tn, *tp;
void insert_tree(tp *node, int num){
tp new_node = (tp)malloc(sizeof(tn));
new_node->data = num;
new_node->lc = NULL;
new_node->rc = NULL;
if ((*node) == NULL){
*node = new_node;
}
else{
tp temp = *node;//保留树头
while (temp != NULL){
if (num > (temp->data)){
if (temp->rc == NULL){
temp->rc = new_node;
return;
}
else{
temp = temp->rc;
}
}
else{
if (temp->lc == NULL){
temp->lc = new_node;
return;
}
else{
temp = temp->lc;
}
}
}
}
}
void del_tree(tp *node, int num){
tp t1 = *node;
tp t2 = NULL;
while (t1 != NULL){
if (num == t1->data){
if (t1->lc == NULL && t1->rc ==NULL){
if (t2->lc == t1){
t2->lc = NULL;
}
else{
t2->rc = NULL;
}
free(t1);
printf("%d has been moved!\n", num);
return;
}
else if (t1->lc == NULL){
tp temp = t1->rc;
if (t2->lc == t1){
t2->lc = temp;
}
else{
t2->rc = temp;
}
free(t1);
printf("%d has been moved!\n", num);
return;
}
else if (t1->rc == NULL){
tp temp = t1->lc;
if (t2->lc == t1){
t2->lc = temp;
}
else{
t2->rc = temp;
}
free(t1);
printf("%d has been moved!\n", num);
return;
}
else{//直接前驱,转左后右尽头 中序遍历
tp target = t1->lc;
tp before = NULL;
while (target->rc != NULL){
before = target;
target = target->rc;
}
if (before != NULL){//避免根头
before->rc = target->lc;
}
if (t2 != NULL && t2->lc == t1){
t2->lc = target;
}
else if (t2 != NULL && t2->rc == t1){
t2->rc = target;
}
else{
*node = target;
}
if (t1->lc != target){//避免转左后无右
target->lc = t1->lc;
}
target->rc = t1->rc;
free(t1);
printf("%d has been moved!\n", num);
return;
}
}
else if (num > t1->data){
t2 = t1;
t1 = t1->rc;
}
else{
t2 = t1;
t1 = t1->lc;
}
}
printf("number: %d doesn't exist!!!\n", num);
}
void preorder(tp node){
if (node == NULL){
return;
}
printf("%d\n", node->data);
preorder(node->lc);
preorder(node->rc);
}
int main(void){
tp top = NULL;
int a[] = {6,1,2,5,8,9,7,3,4,0};
for (int i = 0; i<10; i++){
insert_tree(&top, a[i]);
}
del_tree(&top, 8);
preorder(top);
return 0;
}