-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeStructure.cpp
More file actions
164 lines (147 loc) · 4.22 KB
/
TreeStructure.cpp
File metadata and controls
164 lines (147 loc) · 4.22 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//2018203043_최세린
//Visucal C++ 2017
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;
//방향을 의미하는 배열이다.
int V[4][2] = { { -1, 0 },{ 1, 0 },{ 0, 1 },{ 0, -1 } };
int Center(int n) { return n <= 1 ? 0 : 2 * Center(n / 4) + 1; }
int Depth(int n) { return n <= 7 ? 1 : 2 * Depth(n / 4); }
void rotated(vector<char> v, int i, int depth) {
int width = 0;
if (i < v.size())
{
rotated(v, 2 * i + 2, depth + 1);//재귀호출을 통해서 right subtree의 node에 접근
width = depth * 2 + 1;
cout.width(width);
cout << v[i] << endl;
rotated(v, 2 * i + 1, depth + 1);//재귀호출을 통해서 left subtree의 node에 접근
}
}
void not_rotated(vector<char> v, int depth) {
int j = 1;
int width;
for (int k = 0; k < v.size(); k++)
{
if (k == 0) {
width = pow(2, depth);
cout.width(width);
cout << v[0] << endl;
--depth;
}
//k의 parent가 모두 왼쪽이고, k도 왼쪽일 경우
else if (k == pow(2, j) - 1) {
width = pow(2, depth);
cout.width(width);
cout << v[k];
}
//출력되는 라인의 첫번재 요소가 아닌 경우
else {
width = pow(2, depth + 1);
cout.width(width);
cout << v[k];
//k의 parent가 모두 오른쪽이고, k도 오른쪽일 경우
if (k == pow(2, j + 1) - 2) {
cout << endl;
++j; --depth;
}
}
}
cout << endl;
}
char** H_array(int size) {//인자로 넘겨받은 size로 이차원 동적배열을 할당
char **arr = new char*[size];
for (int i = 0; i < size; ++i) {
arr[i] = new char[size];
memset(arr[i], 0, sizeof(char)*size); // 메모리 공간을 0으로 초기화
}
return arr;
}
char ** H(char** H_tree, vector<char>& v, int node, int i, int j, int d, int U, int D, int R, int L) {
if (node > v.size()) return NULL;
H_tree[i][j] = v[node - 1];
if (2 * node <= v.size()) {
H_tree[i + d * V[L][0]][j + d * V[L][1]] = v[2 * node - 1];
H(H_tree, v, 4 * node, i + d * (V[L][0] + V[U][0]),
j + d * (V[L][1] + V[U][1]), d / 2, D, U, L, R);
H(H_tree, v, 4 * node + 1, i + d * (V[L][0] + V[D][0]),
j + d * (V[L][1] + V[D][1]), d / 2, U, D, R, L);
}
if (2 * node + 1 <= v.size()) {
H_tree[i + d * V[R][0]][j + d * V[R][1]] = v[2 * node];
H(H_tree, v, 4 * node + 2, i + d * (V[R][0] + V[D][0]),
j + d * (V[R][1] + V[D][1]), d / 2, U, D, R, L);
H(H_tree, v, 4 * node + 3, i + d * (V[R][0] + V[U][0]),
j + d * (V[R][1] + V[U][1]), d / 2, D, U, L, R);
}
return H_tree;
}
void h_tree(char**& H_tree, vector<char> v, int size) {
//H함수를 호출하여 h-tree형태의 배열로 저장한 이중포인터를 htree에 대입
char**htree = H(H_tree, v, 1, Center(v.size()), Center(v.size()), Depth(v.size()), 0, 1, 2, 3);
//htree를 출력
for (int i = 0; i < size; ++i) {
//cout << i << " ";
for (int j = 0; j < size; ++j) {
if (j == 0)
cout.width(1);
else
cout.width(2);
cout << htree[i][j];
}
cout << endl;
}
//메모리 해제
for (int i = 0; i < size; ++i) { delete[] htree[i]; } delete[] htree;
}
int main(void) {
char i;
vector<char> v;
string s;
int lmt = 0;
int depth = 1;
int hSize = 1;
int hhSize = 0;
while (cin >> s) {
++lmt;
if (s == "DEL" && v.size() != 0) {
pop_heap(v.begin(), v.end());//0번째 요소를 가장 뒤로 보낸 후, 나머지 원소들을 heap으로 정렬
v.pop_back();//가장 마지막 요소 제거
}
else if (s == "INS"&& cin >> i)
{
if (((int)i > 47 && (int)i < 58) || ((int)i > 64 && (int)i < 91) || ((int)i > 96 && (int)i < 123) || (int)i == 63)
{
v.push_back(i);//i를 벡터의 마지막에 push
push_heap(v.begin(), v.end());//heap으로 정렬
}
}
else if (s == "EOI") {
for (int k = 1; k < v.size(); k++)//v의 depth를 구함
if (k == pow(2, depth) - 1) {
++depth;
}
--depth;
cout << "1. rotated form" << endl;
rotated(v, 0, 0);
cout << endl;
cout << "2. not-rotated form" << endl;
not_rotated(v, depth);
cout << endl;
cout << "3. H-tree form" << endl << endl;
for (int k = 1; k < Depth(v.size()); k++)
if (k == pow(2, hSize) - 1) {
++hSize;
}
hhSize = pow(2, hSize + 1) - 1;//동적배열 사이즈
char** H_tree = H_array(hhSize);//동적배열 할당
h_tree(H_tree, v, hhSize);
break;
}
if (lmt == 200)
break;
}
return 0;
}