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';
151 std::cout <<
"\nIt took me " << t <<
" clicks (" << ((float)t) / CLOCKS_PER_SEC <<
" seconds).\n";
152 fout <<
"\nIt took me " << t <<
" clicks (" << ((float)t) / CLOCKS_PER_SEC <<
" seconds).\n";
161 std::string file_path(_file_path);
164 std::string cmd = std::string(
"DEL /P \"") + file_path +
'"';
166 system(
"echo We are trying to delete some files, which will be created later.");
171 std::ifstream fin(file_path.c_str(), std::ios_base::in);
174 std::cerr <<
"文件已存在!\n";
182 std::ofstream fout(file_path.c_str(), std::ios_base::out);
185 std::cerr <<
"无写权限,文件生成失败!\n";
193 << empty_flag <<
'\t' << empty_flag <<
'\t' << empty_flag <<
'\t' << empty_flag <<
'\t' << empty_flag <<
'\t' << empty_flag <<
'\t' <<
"H\tI\n"
194 << empty_flag <<
'\t' << empty_flag <<
'\t' << empty_flag <<
'\t' << empty_flag << std::endl;
199 system(
"echo File created successfully!");
205 std::cerr <<
"无法生成文件!\n";
210 template <
class T,
typename Comparator>
213 out <<
"二叉树的规模(递归 非递归):\n"
215 out <<
"\n二叉树的高(深)度(递归 非递归),从0起:\n"
218 out <<
"\n度为2的结点的个数是:\n"
221 out <<
"\n是否为满二叉树:\n"
224 out <<
"\n是否为完全二叉树:\n"
226 out <<
"\n前序遍历(递归):\n";
228 out <<
"\n前序遍历(非递归):\n";
230 out <<
"\n中序遍历(递归):\n";
232 out <<
"\n中序遍历(非递归):\n";
234 out <<
"\n后序遍历(递归):\n";
236 out <<
"\n后序遍历(非递归):\n";
240 out <<
"\n层次打印(调用lchild(), rchild(), root()等API):\n";