36 static bool GenTreeData(
const char *_file_path =
"./test0.txt",
const char *empty_flag =
"@");
48 template <
typename T,
typename Comparator>
51 int main(
int argc,
char const *argv[])
59 timeinfo = localtime(&rawtime);
62 const std::string full_path_exec{argv[0]};
63 std::string::size_type found = full_path_exec.find_last_of(
"/\\", std::string::npos);
64 const std::string exec_path = full_path_exec.substr(0, found + 1);
65 const std::string exec_filename = full_path_exec.substr(found + 1, std::string::npos);
73 std::cout <<
"文件:" << data_file_full_path <<
"生成失败!\n";
78 std::ifstream fin(data_file_full_path.c_str(), std::ios_base::in);
81 std::cout <<
"文件:" << data_file_full_path <<
"读取失败!\n";
91 std::ofstream fout(data_file_full_path.c_str(), std::ios_base::app);
94 std::cerr <<
"文件:" << data_file_full_path <<
"写入失败!\n";
99 std::cout <<
"\nCurrent local time and date: " << asctime(timeinfo) <<
'\n';
100 fout <<
"\nCurrent local time and date: " << asctime(timeinfo) <<
'\n';
108 auto tree_swaplr = temp_tree;
109 std::cout <<
"\n交换左右结点后:\n";
110 fout <<
"\n交换左右结点后:\n";
117 auto tree_swaplr_and_prune = temp_tree;
118 std::cout <<
"\n剪枝后:\n";
124 std::cout <<
"\ntree_swaplr == tree_swaplr_and_prune?\n"
125 << std::boolalpha << (tree_swaplr == tree_swaplr_and_prune);
126 fout <<
"\ntree_swaplr == tree_swaplr_and_prune?\n"
127 << std::boolalpha << (tree_swaplr == tree_swaplr_and_prune);
128 std::cout <<
"\ntree_original == tree_swaplr_and_prune?\n"
129 << std::boolalpha << (tree_original == tree_swaplr_and_prune);
130 fout <<
"\ntree_original == tree_swaplr_and_prune?\n"
131 << std::boolalpha << (tree_original == tree_swaplr_and_prune);
132 std::cout <<
"\ntree_original == tree_swaplr?\n"
133 << std::boolalpha << (tree_original == tree_swaplr);
134 fout <<
"\ntree_original == tree_swaplr?\n"
135 << std::boolalpha << (tree_original == tree_swaplr);
136 std::cout <<
"\ntree_original == tree_original?\n"
137 << std::boolalpha << (tree_original == tree_original);
138 fout <<
"\ntree_original == tree_original?\n"
139 << std::boolalpha << (tree_original == tree_original);
140 std::cout <<
"\ntree_swaplr == tree_swaplr?\n"
141 << std::boolalpha << (tree_swaplr == tree_swaplr);
142 fout <<
"\ntree_swaplr == tree_swaplr?\n"
143 << std::boolalpha << (tree_swaplr == tree_swaplr);
144 std::cout <<
"\ntree_swaplr_and_prune == tree_swaplr_and_prune?\n"
145 << std::boolalpha << (tree_swaplr_and_prune == tree_swaplr_and_prune);
146 fout <<
"\ntree_swaplr_and_prune == tree_swaplr_and_prune?\n"
147 << std::boolalpha << (tree_swaplr_and_prune == tree_swaplr_and_prune) <<
'\n';
150 std::cout <<
"\nis_symmetrical_tree(tree_original, tree_swaplr)?\n"
151 << std::boolalpha << Tree::binaryTree<std::string>::is_symmetrical_tree(tree_original, tree_swaplr);
152 fout <<
"\nis_symmetrical_tree(tree_original, tree_swaplr)?\n"
153 << std::boolalpha << Tree::binaryTree<std::string>::is_symmetrical_tree(tree_original, tree_swaplr);
154 std::cout <<
"\nis_symmetrical_tree(tree_swaplr, tree_swaplr)?\n"
155 << std::boolalpha << Tree::binaryTree<std::string>::is_symmetrical_tree(tree_swaplr, tree_swaplr);
156 fout <<
"\nis_symmetrical_tree(tree_swaplr, tree_swaplr)?\n"
157 << std::boolalpha << Tree::binaryTree<std::string>::is_symmetrical_tree(tree_swaplr, tree_swaplr);
158 std::cout <<
"\nis_symmetrical_tree(tree_original, tree_original)?\n"
159 << std::boolalpha << Tree::binaryTree<std::string>::is_symmetrical_tree(tree_original, tree_original);
160 fout <<
"\nis_symmetrical_tree(tree_original, tree_original)?\n"
161 << std::boolalpha << Tree::binaryTree<std::string>::is_symmetrical_tree(tree_original, tree_original) <<
'\n';
165 auto tree_copy_construct = tree_swaplr_and_prune;
166 std::cout <<
"\n(复制构造)auto tree_copy_construct = tree_swaplr_and_prune;\n";
167 fout <<
"\n(复制构造)auto tree_copy_construct = tree_swaplr_and_prune;\n";
171 auto tree_move_construct = std::move(tree_swaplr_and_prune);
172 std::cout <<
"\n(移动构造)auto tree_move_construct = std::move(tree_swaplr_and_prune);\n";
173 fout <<
"\n(移动构造)auto tree_move_construct = std::move(tree_swaplr_and_prune);\n";
177 std::cout <<
"\n(使用过期的对象)print_test_result(tree_swaplr_and_prune, \"@\", std::cout);\n";
178 fout <<
"\n(使用过期的对象)print_test_result(tree_swaplr_and_prune, \"@\", fout);\n";
183 std::cout <<
"\n(向过期的对象移动赋值)tree_swaplr_and_prune = Tree::binaryTree(tree_move_construct);\n";
184 fout <<
"\n(向过期的对象移动赋值)tree_swaplr_and_prune = Tree::binaryTree(tree_move_construct);\n";
189 tree_copy_assign = tree_swaplr_and_prune;
190 std::cout <<
"\n(赋值)tree_copy_assign = tree_swaplr_and_prune;\n";
191 fout <<
"\n(赋值)tree_copy_assign = tree_swaplr_and_prune;\n";
197 std::cout <<
"\nIt took me " << t <<
" clicks (" << ((float)t) / CLOCKS_PER_SEC <<
" seconds).\n";
198 fout <<
"\nIt took me " << t <<
" clicks (" << ((float)t) / CLOCKS_PER_SEC <<
" seconds).\n";
207 std::string file_path(_file_path);
210 std::string cmd = std::string(
"DEL /P \"") + file_path +
'"';
212 system(
"echo We are trying to delete some files, which will be created later.");
217 std::ifstream fin(file_path.c_str(), std::ios_base::in);
220 std::cerr <<
"文件已存在!\n";
228 std::ofstream fout(file_path.c_str(), std::ios_base::out);
231 std::cerr <<
"无写权限,文件生成失败!\n";
238 << empty_flag <<
'\t' << empty_flag <<
'\t' << empty_flag <<
'\t' << empty_flag <<
'\t' << empty_flag <<
'\t' << empty_flag <<
'\t' <<
"H\tI\n"
239 << empty_flag <<
'\t' << empty_flag <<
'\t' << empty_flag <<
'\t' << empty_flag << std::endl;
244 system(
"echo File created successfully!");
250 std::cerr <<
"无法生成文件!\n";
255 template <
class T,
typename Comparator>
258 out <<
"二叉树的规模(递归 非递归):\n"
260 out <<
"\n二叉树的高(深)度(递归 非递归),从0起:\n"
263 out <<
"\n度为2的结点的个数是:\n"
266 out <<
"\n是否为满二叉树:\n"
269 out <<
"\n是否为完全二叉树:\n"
271 out <<
"\n前序遍历(递归):\n";
273 out <<
"\n前序遍历(非递归):\n";
275 out <<
"\n中序遍历(递归):\n";
277 out <<
"\n中序遍历(非递归):\n";
279 out <<
"\n后序遍历(递归):\n";
281 out <<
"\n后序遍历(非递归):\n";
285 out <<
"\n层次打印(调用lchild(), rchild(), root()等API):\n";