-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_folder.cpp
More file actions
173 lines (163 loc) · 4.14 KB
/
tree_folder.cpp
File metadata and controls
173 lines (163 loc) · 4.14 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
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <string>
#include <queue>
using namespace std;
class Folder{
public:
friend class Tree;
private:
Folder *child[10];
File *file[10];
int child_num = 0;
int file_num = 0;
string name;
};
class File{
public:
friend class Tree;
private:
string name;
int index;
};
class BinaryTree{
public:
friend class Tree;
private:
BinaryTree *left;
BinaryTree *right;
string name;
int index;
};
class Tree{
public:
Tree();
void input(Folder *ptr, string name);
void inputfile(Folder *ptr, string name);
void output(Folder *ptr);
void turntobinarytree(Folder *ptr);
Folder *find(Folder *ptr);
Folder *head;
Folder *tail;
BinaryTree *headb;
BinaryTree *tailb;
private:
int rnull = 0;
int lnull = 0;
};
Tree::Tree(){
head = NULL;
tail = NULL;
}
void Tree::input(Folder *ptr, string name){
if (tail == NULL) {
ptr = new Folder;
ptr->name = name;
tail = head = ptr;
for (int i = 0; i < 10; i++) {
ptr->child[i] = NULL;
}
} else {
ptr->child[ptr->child_num] = new Folder;
ptr->child[ptr->child_num]->name = name;
for (int i = 0; i < 10; i++) {
ptr->child[ptr->child_num]->child[i] = NULL;
}
ptr->child_num++;
}
return ;
}
void Tree::inputfile(Folder *ptr, string name){
if (ptr == NULL) {
return ;
}
ptr->file[ptr->file_num] = new File;
ptr->file[ptr->file_num]->name = name;
cout << "input index: ";
cin >> ptr->file[ptr->file_num]->index;
ptr->file_num++;
return ;
}
void Tree::output(Folder *ptr){
if (ptr == NULL) {
return ;
}
cout << "parent: " << ptr->name << endl << "childs: ";
for (int i = 0; i < ptr->child_num; i++) {
cout << ptr->child[i]->name << " ";
}
return ;
}
Folder* Tree::find(Folder *ptr){
string dir;
char slash;
cin >> slash >> dir;
if (dir == ptr->name) {
while (ptr->child_num > 0) {
cin >> slash >> dir;
for (int i = 0; i < ptr->child_num; i++) {
if (dir == ptr->child[i]->name) {
ptr = ptr->child[i];
}
}
}
cout << "location: " << ptr->name;
return ptr;
} else {
cout << "false location: " << ptr->name;
return tail;
}
}
void Tree::turntobinarytree(Folder *ptr){//not finish, cant connect right child with parent in binary tree.
queue<Folder*> temp;
BinaryTree *root = headb;
headb = new BinaryTree;
temp.push(ptr);
while (!temp.empty()) {
ptr = temp.front();
tailb->name = ptr->name;
while (ptr->child_num > 0) {
temp.push(ptr->child[ptr->child_num]);
if (ptr->child_num == 0) {
tailb->left = new BinaryTree;
tailb->left->name = ptr->child[ptr->child_num]->name;
root = tailb;
} else {
tailb->right = new BinaryTree;
tailb->right->name = ptr->child[ptr->child_num]->name;
tailb = tailb->right;
}
ptr->child_num++;
}
while (ptr->file_num > 0) {
tailb->right = new BinaryTree;
tailb->right->name = ptr->file[ptr->file_num]->name;
tailb = tailb->right;
ptr->file_num++;
}
temp.pop();
}
return ;
}
int main(){
Tree my_list;
string commend, name;
while (1) {
cin >> commend;
if (commend == "mkdir") {
cin >> name;
my_list.input(my_list.tail, name);
} else if (commend == "mkf") {
cin >> name;
my_list.inputfile(my_list.tail, name);
} else if (commend == "list") {
my_list.output(my_list.tail);
} else if (commend == "cd") {
my_list.tail = my_list.find(my_list.tail);
} else if (commend == "cd.") {
my_list.tail = my_list.head;
} else if (commend == "turn_to_binary_tree") {
my_list.turntobinarytree(my_list.head);
}
}
return 0;
}