-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search_tree.c
215 lines (179 loc) · 4 KB
/
binary_search_tree.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NODES_COUNT 1000
struct Node {
int key;
struct Node* parent;
struct Node* left;
struct Node* right;
};
struct BST {
struct Node* root;
};
struct BST* tree_initialize( struct Node* );
void inorder_tree_walk( struct Node* );
struct Node* tree_search( struct Node*, int );
struct Node* iterative_tree_search( struct Node*, int );
struct Node* tree_minimum( struct Node* );
struct Node* tree_maximum( struct Node* );
struct Node* tree_successor( struct Node* );
struct Node* tree_predecessor( struct Node* );
void tree_insert( struct BST*, struct Node* );
void tree_delete( struct BST*, struct Node* );
int main( void ) {
struct BST* tree = tree_initialize( NULL );
for ( int i = 0; i < NODES_COUNT; i++ ) {
int k = floor( random() );
struct Node* node = malloc( sizeof( struct Node ) );
node->key = k;
node->parent = NULL;
node->left = NULL;
node->right = NULL;
tree_insert( tree, node );
}
// inorder_tree_walk( tree->root );
struct Node* s = iterative_tree_search( tree->root, 2143124030 );
if ( s != NULL ) {
printf( "2143124030 exist\n" );
}
else {
printf( "2143124030 not exist\n" );
}
tree_delete( tree, s );
struct Node* ss = iterative_tree_search( tree->root, 2143124030 );
if ( ss != NULL ) {
printf( "2143124030 exist\n" );
}
else {
printf( "2143124030 not exist\n" );
}
return 0;
}
struct BST* tree_initialize( struct Node* root ) {
struct BST* bst = malloc( sizeof( struct BST ) );
bst->root = root;
return bst;
}
void inorder_tree_walk( struct Node* root ) {
if ( root != NULL ) {
inorder_tree_walk( root->left );
printf( "node with key %d\n", root->key );
inorder_tree_walk( root->right );
}
}
struct Node* tree_search( struct Node* root, int k ) {
if ( root == NULL || root->key == k ) {
return root;
}
if ( k > root->key ) {
tree_search( root->right, k );
}
else {
tree_search( root->left, k );
}
}
struct Node* iterative_tree_search( struct Node* root, int k ) {
while ( root != NULL && root->key != k ) {
if ( k > root->key ) {
root = root->right;
}
else {
root = root->left;
}
}
return root;
}
struct Node* tree_minimum( struct Node* root ) {
while ( root != NULL && root->left != NULL ) {
root = root->left;
}
return root;
}
struct Node* tree_maximum( struct Node* root ) {
while ( root != NULL && root->right != NULL ) {
root = root->right;
}
return root;
}
struct Node* tree_successor( struct Node* x ) {
if ( x->right != NULL ) {
return tree_minimum( x->right );
}
struct Node* y = x->parent;
while ( y != NULL && y->left != x ) {
x = y;
y = y->parent;
}
return y;
}
struct Node* tree_predecessor( struct Node* x ) {
if ( x->left != NULL ) {
return tree_maximum( x->left );
}
struct Node* y = x->parent;
while ( y != NULL && y->right != x ) {
x = y;
y = y->parent;
}
return y;
}
void tree_insert( struct BST* tree, struct Node* z ) {
struct Node* x = tree->root;
struct Node* y = NULL;
while ( x != NULL ) {
y = x;
if ( z->key > x->key ) {
x = x->right;
}
else {
x = x->left;
}
}
z->parent = y;
if ( y == NULL ) {
tree->root = z;
}
else {
if ( z->key > y->key ) {
y->right = z;
}
else {
y->left = z;
}
}
}
void tree_delete( struct BST* tree, struct Node* z ) {
struct Node* toBeSplicedOut;
if ( z->left == NULL || z->right == NULL ) {
toBeSplicedOut = z;
}
else {
toBeSplicedOut = tree_successor( z );
}
struct Node* nonNullChild;
if ( toBeSplicedOut->left != NULL ) {
nonNullChild = toBeSplicedOut->left;
}
else {
nonNullChild = toBeSplicedOut->right;
}
if ( nonNullChild != NULL ) {
nonNullChild->parent = toBeSplicedOut->parent;
}
if ( toBeSplicedOut->parent == NULL ) {
tree->root = nonNullChild;
}
else {
if ( toBeSplicedOut == (toBeSplicedOut->parent)->left ) {
(toBeSplicedOut->parent)->left = nonNullChild;
}
else {
(toBeSplicedOut->parent)->left = nonNullChild;
}
}
if ( toBeSplicedOut != z ) {
z->key = toBeSplicedOut->key;
}
free( toBeSplicedOut );
}