-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCatalogTree.cpp
262 lines (210 loc) · 6.5 KB
/
CatalogTree.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//
// Created by 28320 on 2021/5/5.
//
#include "CatalogTree.h"
CatalogTree::CatalogTree() {
currentNode = &root;
}
CatalogTree::~CatalogTree() {
// 将除了根节点之外的节点,都删除掉
CatalogNode *cur = root.child;
erase(cur); // 删掉整个的二叉结构
}
vector<CatalogNode *> CatalogTree::getChildren() {
vector<CatalogNode *> res;
CatalogNode *cur = currentNode->child;
if (cur == nullptr) return res;
else cur = cur->sibling;
while (cur != nullptr) {
res.push_back(cur);
cur = cur->sibling;
}
return res;
}
void CatalogTree::eraseChild(const string &theRPath) {
// 先将这个节点的孩子节点所在的二叉树全都删掉
CatalogNode *pre = currentNode->child;
// 如果没有孩子,他却要删???那咱就直接返回,反正没得删
if (pre == nullptr) return;
CatalogNode *cur = pre->sibling;
while (cur != nullptr && cur->rPath != theRPath) {
cur = cur->sibling;
pre = pre->sibling;
}
if (cur == nullptr)
return; // 没有找到这个节点
erase(cur->child);
// 把前后链表前后连起来
pre->sibling = cur->sibling;
// 把这个节点的空间释放
delete cur;
}
void CatalogTree::insertChild(const string &theRPath) {
if (currentNode->child == nullptr) {
// 则给孩子链表设置一个头节点
// 没有必要让他指向父亲
// 字符串 @
// 没有孩子
currentNode->child = new CatalogNode;
currentNode->child->rPath = "@"; // 专门表示头节点
}
ascendInsert(currentNode->child, currentNode, theRPath);
}
void CatalogTree::setPath(const string &thePath) {
CatalogNode *cur = nullptr;
if (thePath[0] == '/') { // 这是一个绝对路径
CatalogNode *newCurrentNode = &root;
vector<string> tmpPath = split(thePath);
for (auto &x: tmpPath) {
cur = newCurrentNode->child;
while (cur != nullptr && cur->rPath != x)
cur = cur->sibling;
if (cur == nullptr) return;
else newCurrentNode = cur;
}
currentNode = newCurrentNode;
} else { // 相对路径
cur = currentNode->child;
while (cur != nullptr && cur->rPath != thePath)
cur = cur->sibling;
if (cur != nullptr)
currentNode = cur; // 设置当前的路径为thePath
}
}
void CatalogTree::setPathWithParent() {
if (currentNode->parent == nullptr) return; // 根节点,直接返回
currentNode = currentNode->parent; // 更新当前节点
}
vector<string> CatalogTree::getFPath() {
CatalogNode *cur = currentNode;
vector<string> res;
while (cur != nullptr) {
res.push_back(cur->rPath);
cur = cur->parent;
}
reverse(res.begin(), res.end());
return res;
}
void CatalogTree::preSave(CatalogNode *cur) {
if (cur != nullptr) {
output << cur->rPath << '\n'; // 输出这个名字
preSave(cur->child);
preSave(cur->sibling);
} else {
output << "-1\n";
}
}
bool CatalogTree::save(const string &filePath) {
output.open(filePath);
if (!output.is_open()) return false;
vector<string> fullPath = getFPath(); // 先记下所在的路径
preSave(root.child); // root的child为根的二叉树,打印到
// for (int i = 1; i < fullPath.size(); i++)
// output << fullPath[i] << ' ';
output.close();
return true;
}
void CatalogTree::preLoad(CatalogNode *&cur) {
string tmp;
input >> tmp;
cout << "read : " << tmp << endl;
if (tmp == "-1") return; // 意味着读取失败
cur = new CatalogNode;
cur->rPath = tmp;
preLoad(cur->child);
preLoad(cur->sibling);
}
void CatalogTree::updatePar(CatalogNode *cur) {
if (cur != nullptr) {
currentNode = cur;
vector<CatalogNode *> tmp = getChildren();
for (auto &item: tmp)
item->parent = cur;
updatePar(cur->child);
updatePar(cur->sibling);
}
}
bool CatalogTree::load(const string &filePath) {
// 删掉之前的
erase(root.child);
input.open(filePath);
if (!input.is_open()) return false;
// 不断new出其他的节点
preLoad(root.child);
// 更新全体的父亲节点
updatePar(&root);
// // 恢复currentNode
// currentNode = &root;
// string tmp;
// while (input >> tmp) {
// setPath(tmp);
// }
input.close();
return true;
}
void CatalogTree::erase(CatalogNode *cur) {
// 后序思想
// if (cur != nullptr) {
// CatalogNode *theCur = cur->child;
// while (theCur != nullptr) {
// erase(theCur);
// theCur = theCur->sibling;
// }
// delete cur;
// }
if (cur != nullptr) {
erase(cur->child);
erase(cur->sibling);
delete cur;
}
}
void CatalogTree::ascendInsert(CatalogNode *theHead, CatalogNode *thePar, const string &theRPath) {
// 插入这个节点到链表,并且设置这个节点的父亲
// 如果这个节点已经存在,不做任何操作
CatalogNode *pre = theHead;
CatalogNode *cur = theHead->sibling;
while (cur != nullptr && cur->rPath < theRPath) {
cur = cur->sibling;
pre = pre->sibling;
}
if (cur != nullptr && cur->rPath == theRPath)
return; // 这个节点已经存在,直接返回
// 插入一个新的节点
CatalogNode *tmp = new CatalogNode;
tmp->rPath = theRPath;
tmp->parent = thePar;
tmp->sibling = cur;
pre->sibling = tmp;
}
vector<string> CatalogTree::split(const string &s) {
// 分成多个相对路径
vector<string> res;
string tmp;
for (int i = 1; i < s.size(); i++) {
if (s[i] != '/')
tmp += s[i];
else {
res.push_back(tmp);
tmp = "";
}
}
res.push_back(tmp);
return res;
}
void CatalogTree::draw() {
printBT("", &root, false);
}
void CatalogTree::printBT(const string &prefix, const CatalogNode *node, bool isLeft) {
if (node != nullptr) {
std::cout << prefix;
std::cout << (isLeft ? "├── " : "");
// print the value of the node
if (node == &root)
std::cout << "/" << std::endl;
else
std::cout << node->rPath << std::endl;
// enter the next tree level - left and right branch
printBT(prefix + (isLeft ? "│ " : "│ "), node->child, true);
printBT(prefix + (isLeft ? "│ " : ""), node->sibling, false);
}
}