-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.cpp
153 lines (125 loc) · 2.96 KB
/
map.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
146
147
148
149
150
151
152
153
// ConsoleApplication1.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
struct entry {
int key; // 학번
char* value; // 이름
// (2)번 - 추가하면 Map에 update도 바꾸기
// int gender; // 성별
// 여자:0, 남자:1
};
struct tree { entry* data; tree* left; tree* right; };
tree* node(entry* e, tree* tl, tree* tr) {
return new tree({ e, tl, tr });
}
entry* minBST(tree* t) {
if (t == NULL) return NULL;
tree* p = t;
while (p->left) p = p->left;
return p->data;
}
entry* maxBST(tree* t) {
if (t == NULL) return NULL;
tree* p = t;
while (p->right) p = p->right;
return p->data;
}
int isBST(tree* t) {
if (t == NULL) return 1;
if (t->left &&
maxBST(t->left) >= t->data)
return 0;
if (t->right &&
minBST(t->right) <= t->data)
return 0;
return isBST(t->left) && isBST(t->right);
}
/*
void printBST(tree* t) {
if (t == NULL) { cout << "{}"; }
else {
cout << t->data << " -> { ";
printBST(t->left);
cout << ", ";
printBST(t->right);
cout << "}";
}
}
*/
entry* searchBST(int n, tree* t) {
if (t == NULL) return NULL; // 없다
if (n < t->data->key)
return searchBST(n, t->left);
if (n > t->data->key)
return searchBST(n, t->right);
// if (n == t->data)
return t->data; // 있다
}
tree* insertBST(entry* e, tree* t) {
if (t == NULL) return node(e, NULL, NULL);
if (e->key < t->data->key) {
if (t->left == NULL)
t->left = node(e, NULL, NULL);
else
insertBST(e, t->left);
return t;
}
if (e->key > t->data->key) {
if (t->right == NULL)
t->right = node(e, NULL, NULL);
else
insertBST(e, t->right);
return t;
}
// if (n == t->data)
return t;
}
struct Map { // 학번을 이름에 대응
tree* t;
entry* find(int k) {
return searchBST(k, t);
}
entry* update(int k, char* v) {
entry* p = find(k);
if (p) p->value = v;
else {
p = new entry({k,v});
t = insertBST(p, t);
}
return p;
}
// (1)번
// entry* remove(int k);
// 실제로 트리에서 존재하는
// 엔트리이면 그걸 리턴하고, 만약에
// k라는 키가 없으면 NULL을 리턴
};
int main(void)
{
Map m = { NULL };
m.update(1998010101, "김동성");
m.update(2009000000, "김연아");
m.update(1972000111, "차범근");
m.update(1988000111, "서정원");
m.update(1999000111, "차두리");
m.update(2000999999, "이천수");
m.update(1990999999, "김수녕");
entry* p1 = m.find(2009000000);
if (p1) cout << p1->value << endl;
else cout << 2009000000 << " not found" << endl;
entry* p2 = m.find(2007010101);
if (p2) cout << p2->value << endl;
else cout << 2007010101 << " not found" << endl;
m.update(2007010101, "김자연");
entry* p3 = m.find(2007010101);
if (p3) cout << p3->value << endl;
else cout << 2007010101 << " not found" << endl;
m.update(2007010101, "김자인");
entry* p4 = m.find(2007010101);
if (p4) cout << p4->value << endl;
else cout << 2007010101 << " not found" << endl;
getchar();
return 0;
}