-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinclass-14-1123-binary-search-tree.cpp
More file actions
141 lines (139 loc) · 2.67 KB
/
inclass-14-1123-binary-search-tree.cpp
File metadata and controls
141 lines (139 loc) · 2.67 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_SIZE 20
typedef struct Node {
int v;
Node *l, *r, *p1, *p2;
}Node;
Node* find(Node *root, int n) {
if (n < root->v) {
if(root->l == NULL) {
return root;
} else {
return find(root->l, n);
}
} else {
if(root->r == NULL) {
return root;
} else {
return find(root->r, n);
}
}
}
Node* finddel(Node *root, int n) {
if(!root) return NULL;
if(root->v == n) {
return root;
} else if (n < root->v) {
return finddel(root->l, n);
} else {
return finddel(root->r, n);
}
}
int search(Node *root, int n) {
if(!root) return 0;
if(root->v == n) {
return 1;
} else if (n < root->v) {
return search(root->l, n);
} else {
return search(root->r, n);
}
}
void insert(Node *root, int n) {
Node *temp = find(root, n);
if(n < temp->v) {
temp->l = (Node*)malloc(sizeof(Node));
temp->l->v = n;
temp->l->p1 = temp;
temp->l->p2 = NULL;
temp->l->l = NULL;
temp->l->r = NULL;
} else {
temp->r = (Node*)malloc(sizeof(Node));
temp->r->v = n;
temp->r->p1 = NULL;
temp->r->p2 = temp;
temp->r->l = NULL;
temp->r->r = NULL;
}
}
void remove2(Node *temp) {
if(temp->l == NULL && temp->r == NULL) {
if(temp->p1 != NULL) temp->p1->l = NULL;
else if(temp->p2 != NULL) temp->p2->r = NULL;
free(temp);
} else if(temp->r == NULL) {
if(temp->p1 != NULL) {
temp->p1->l = temp->l;
temp->l->p1 = temp->p1;
} else if(temp->p2 != NULL) {
temp->p2->r = temp->l;
temp->l->p2 = temp->p2;
}
free(temp);
} else if(temp->l == NULL) {
if(temp->p1 != NULL) {
temp->p1->l = temp->r;
temp->r->p1 = temp->p1;
} else if(temp->p2 != NULL) {
temp->p2->r = temp->r;
temp->r->p2 = temp->p2;
}
free(temp);
} else {
Node *temp2 = temp->l;
while(temp2->r != NULL) {
temp2 = temp2->r;
}
temp->v = temp2->v;
remove2(temp2);
}
}
void remove(Node *root, int n) {
if(search(root, n)) {
Node* temp = finddel(root, n);
remove2(temp);
} else {
printf("not found\n");
}
}
void print(Node *root, int d) {
if(!root) {
return;
}
print(root->l, d+1);
printf("%d ", root->v);
print(root->r, d+1);
}
int main() {
srand(time(NULL));
Node *root = (Node*)malloc(sizeof(Node));
root->l = NULL;
root->r = NULL;
root->p1 = NULL;
root->p2 = NULL;
root->v = rand()%100;
for(int q=0; q<MAX_SIZE-1; q++) {
insert(root, rand()%100);
}
print(root, 0);
printf("\n");
char cmd[10];
int n;
while(~scanf("%s", cmd)) {
if (strcmp(cmd, "find") == 0) {
scanf(" %d", &n);
printf("%d\n", search(root, n));
} else if (strcmp(cmd, "del") == 0) {
scanf(" %d", &n);
remove(root, n);
print(root, 0);
printf("\n");
} else {
printf("no command\n");
}
}
}