-
Notifications
You must be signed in to change notification settings - Fork 643
/
BST.cpp
145 lines (132 loc) · 3.21 KB
/
BST.cpp
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 <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 100;
typedef struct BST{
int key;
BST *lchild, *rchild, *parent;
}BST;
BST *head, *p, node[maxn];
int cnt;
void init(){
head = p = NULL;
cnt = 0;
memset(node, '\0', sizeof(node));
}
void insert(BST* &head, int x){
if(head == NULL){
node[cnt].key = x;
node[cnt].parent = p;
head = &node[cnt++];
return;
}
p = head;
if(head->key > x)
insert(head->lchild, x);
else
insert(head->rchild, x);
}
void inorderTraver(BST *head){
if(head == NULL) return;
inorderTraver(head->lchild);
cout<<head->key<<" ";
inorderTraver(head->rchild);
}
BST* search(BST *head, int x){
if(head == NULL) return NULL;
if(head->key == x) return head;
else if(head->key > x)
search(head->lchild, x);
else
search(head->rchild, x);
}
BST* minimum(BST *head){
if(head == NULL) return NULL;
while(head->lchild != NULL)
head = head->lchild;
return head;
}
BST* maximum(BST *head){
if(head == NULL) return NULL;
while(head->rchild != NULL)
head = head->rchild;
return head;
}
BST* successor(BST *head){
if(head->rchild != NULL)
return minimum(head->rchild);
BST *y = head->parent;
while(y!=NULL && y->rchild==head){
head = y;
y = y->parent;
}
return y;
}
BST* predecessor(BST *head){
if(head->lchild != NULL)
return maximum(head->lchild);
BST *y = head->parent;
while(y!=NULL && y->lchild==head){
head = y;
y = y->parent;
}
return y;
}
void delet(BST *z){
if(z->lchild==NULL && z->rchild==NULL){
if(z==head) head = NULL;
else if(z->parent->lchild == z)
z->parent->lchild = NULL;
else
z->parent->rchild = NULL;
}
else if(z->lchild==NULL || z->rchild==NULL){
if(z==head){
if(z->lchild) head = z->lchild;
else head = z->rchild;
head->parent = NULL;
}
else{
if(z->parent->lchild==z && z->lchild){
z->parent->lchild = z->lchild;
z->lchild->parent = z->parent;
}
else if(z->parent->lchild==z && z->rchild){
z->parent->lchild = z->rchild;
z->rchild->parent = z->parent;
}
else if(z->parent->rchild==z && z->lchild){
z->parent->rchild = z->lchild;
z->lchild->parent = z->parent;
}
else{
z->parent->rchild = z->rchild;
z->rchild->parent = z->parent;
}
}
}
else{
BST *s = predecessor(z);
z->key = s->key;
if(s->parent == z)
s->parent->lchild = s->lchild;
else
s->parent->rchild = s->lchild;
if(s->lchild)
s->lchild->parent = s->parent;
}
}
int main(){
freopen("BST.in", "r", stdin);
init();
int x;
while(cin>>x)
insert(head, x);
inorderTraver(head);
cout<<endl;
cout<<"Min: "<<(minimum(head))->key<<endl;
cout<<"Max: "<<(maximum(head))->key<<endl;
fclose(stdin);
return 0;
}