-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc-code.c
154 lines (140 loc) · 3.18 KB
/
c-code.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
#include <stdio.h>
#include <stdlib.h>
struct Node {
int key;
struct Node *right;
struct Node *left;
};
struct Node *search( struct Node *T, int k ) {
if ( T == NULL || k == T->key ) {
return T;
}
if ( k < T->key ) {
return search( T->left, k );
}
else
return search( T->right, k );
}
struct Node *insert(struct Node *T, int k) {
if ( T == NULL ){
T = (struct Node*)malloc(sizeof(struct Node));
T->key = k;
}
else if (k < T->key){
T->left = insert(T->left, k);
}
else {
T->right = insert(T->right, k);
}
return T;
}
void inorderTraverse( struct Node *T ) {
if ( T != NULL ) {
inorderTraverse( T->left );
printf("%d, ", T->key);
inorderTraverse( T->right );
}
}
void postorderTraverse( struct Node *T ) {
if ( T != NULL ) {
postorderTraverse( T->left );
postorderTraverse( T->right );
printf("%d, ", T->key);
}
}
void preorderTraverse( struct Node *T ) {
if ( T != NULL ) {
printf("%d, ", T->key);
preorderTraverse( T->left );
preorderTraverse( T->right );
}
}
struct Node *getMinRec( struct Node *T ) {
if ( T == NULL ) {
return NULL;
}
if ( T->left != NULL ) {
return getMinRec( T->left );
}
return T;
}
struct Node *getMin( struct Node *T ) {
if ( T == NULL ) {
return NULL;
}
while ( T->left != NULL ) {
T = T->left;
}
return T;
}
struct Node *getMax( struct Node *T ) {
if ( T == NULL ) {
return NULL;
}
while ( T->right != NULL ) {
T = T->right;
}
return T;
}
struct Node *getMaxRec( struct Node *T ) {
if ( T == NULL ) {
return NULL;
}
if ( T->right != NULL ) {
return getMaxRec( T->right );
}
return T;
}
struct Node *delete( struct Node *T, int k) {
if ( T == NULL ) {
return T;
}
if (k < T->key) {
T->left = delete(T->left, k);
}
else if (k > T->key) {
T->right = delete(T->right, k);
}
else {
if (T->left == NULL) {
struct Node *tmp = (struct Node*)malloc(sizeof(struct Node));
tmp = T->right;
free(T);
return tmp;
}
else if (T->right == NULL) {
struct Node *tmp = (struct Node*)malloc(sizeof(struct Node));
tmp = T->left;
free(T);
return tmp;
}
struct Node *tmp = (struct Node*)malloc(sizeof(struct Node));
tmp = getMin(T->right);
T->key = tmp->key;
T->right = delete(T->right, tmp->key);
}
return T;
}
int main(void) {
struct Node *root = NULL;
root = insert(root, 83);
root = insert(root, 74);
root = insert(root, 7);
root = insert(root, 4);
root = insert(root, 78);
root = insert(root, 93);
root = insert(root, 93);
root = insert(root, -84);
root = insert(root, 4);
root = insert(root, 400);
inorderTraverse(root);
printf("\n");
delete(root, 83);
delete(root, 4);
delete(root, 93);
delete(root, 93);
delete(root, 400);
inorderTraverse(root);
printf("\n");
return 0;
}