33 static bool GenTreeData(
const char *_file_path =
"./test0.txt",
const char *empty_flag =
"@");
47 template <
typename T,
typename Comparator>
50 int main(
int argc,
char const *argv[])
58 timeinfo = localtime(&rawtime);
61 const std::string full_path_exec{argv[0]};
62 std::string::size_type found = full_path_exec.find_last_of(
"/\\", std::string::npos);
63 const std::string exec_path = full_path_exec.substr(0, found + 1);
64 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';
106 binary_tree.swaplr();
107 std::cout <<
"\n交换左右结点后:\n";
108 fout <<
"\n交换左右结点后:\n";
114 std::cout <<
"\nIt took me " << t <<
" clicks (" << ((float)t) / CLOCKS_PER_SEC <<
" seconds).\n";
115 fout <<
"\nIt took me " << t <<
" clicks (" << ((float)t) / CLOCKS_PER_SEC <<
" seconds).\n";
124 std::string file_path(_file_path);
127 std::string cmd = std::string(
"DEL /P \"") + file_path +
'"';
129 system(
"echo We are trying to delete some files, which will be created later.");
134 std::ifstream fin(file_path.c_str(), std::ios_base::in);
137 std::cerr <<
"文件已存在!\n";
145 std::ofstream fout(file_path.c_str(), std::ios_base::out);
148 std::cerr <<
"无写权限,文件生成失败!\n";
155 <<
"B\tE\t" << empty_flag <<
"\tD\n"
156 << empty_flag <<
'\t' << empty_flag <<
'\t' << empty_flag <<
'\t' << empty_flag <<
'\t' <<
"W\t" << empty_flag <<
'\n'
157 << empty_flag <<
"\tX\n"
158 << empty_flag <<
'\t' << empty_flag << std::endl;
163 system(
"echo File created successfully!");
169 std::cerr <<
"无法生成文件!\n";
174 template <
class T,
typename Comparator>
177 out <<
"二叉树的规模(递归 非递归):\n"
179 out <<
"\n二叉树的高(深)度(递归 非递归),从0起:\n"
181 out <<
"\n前序遍历(递归):\n";
183 out <<
"\n前序遍历(非递归):\n";
185 out <<
"\n中序遍历(递归):\n";
187 out <<
"\n中序遍历(非递归):\n";
189 out <<
"\n后序遍历(递归):\n";
191 out <<
"\n后序遍历(非递归):\n";
195 out <<
"\n层次打印(调用lchild(), rchild(), root()等API):\n";