树  0.1
数据结构_第6章
test0.cc
Go to the documentation of this file.
1 /****************************************************
2  * @file test0.cc
3  * @author Guorui Wei (313017602@qq.com)
4  * @brief 二叉链表实现的二叉树类(binaryTree)的基本功能测试
5  * @version 0.1
6  * @date 2020-04-19
7  * @note 读取文件路径,参考:http://www.cplusplus.com/reference/string/string/find_last_of/
8  *
9  * @copyright Copyright (c) 2020
10  *
11  * See the file LICENSE in the top directory of this distribution for
12  * more information.
13  *
14  * @note Reference
15  * -# http://www.cplusplus.com/reference/string/string/find_last_of/
16  * -# http://www.cplusplus.com/reference/ctime/clock/
17  * -# http://www.cplusplus.com/reference/ctime/localtime/
18  *
19  ****************************************************/
20 
21 #include "test0.hh"
22 
37 int main(int argc, char const *argv[])
38 {
39  // time
40  clock_t t = clock();
41  time_t rawtime;
42  struct tm *timeinfo;
43 
44  // 获取文件路径,参考:http://www.cplusplus.com/reference/string/string/find_last_of/
45  const std::string full_path_exec{argv[0]};
46  std::string::size_type found = full_path_exec.find_last_of("/\\", std::string::npos);
47  const std::string exec_path = full_path_exec.substr(0, found);
48  const std::string exec_filename = full_path_exec.substr(found + 1, std::string::npos);
49  const std::string data_file_name("\\test0.result");
50 
51  // 边界测试:仅有根结点的树
52  Tree::binaryTree<std::string> binary_tree{"r"};
53  binary_tree.delLeft("r");
54  binary_tree.delRight("r");
55 
56  Tree::print_test_result(binary_tree, "@", std::cout);
57 
58  // 生成数据文件,然后从文件中读取数据,依此建立一棵二叉树
59  std::string data_file_full_path{exec_path + data_file_name}; // 数据文件的绝对地址
60 
61  // 生成数据文件
62  if (!Tree::GenTreeData(data_file_full_path.c_str(), "@"))
63  {
64  std::cout << "文件:" << data_file_full_path << "生成失败!\n";
65  return 1;
66  }
67 
68  // 读数据文件
69  std::ifstream fin(data_file_full_path.c_str(), std::ios_base::in);
70  if (fin.fail())
71  {
72  std::cout << "文件:" << data_file_full_path << "读取失败!\n";
73  return 1;
74  }
75 
76  // 建立二叉树
77  binary_tree.createTree("@", fin);
78  fin.close();
79 
80  // 准备将结果写入数据文件
81  std::ofstream fout(data_file_full_path.c_str(), std::ios_base::app);
82  if (fout.fail())
83  {
84  std::cerr << "文件:" << data_file_full_path << "写入失败!\n";
85  return 1;
86  }
87 
88  time(&rawtime); // Get the current calendar time
89  timeinfo = localtime(&rawtime); // Convert time_t to tm as local time
90  // printf("Current local time and date: %s\n", asctime(timeinfo)); // Convert tm structure to string
91 
92  std::cout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
93  fout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
94 
95  Tree::print_test_result(binary_tree, "@", std::cout);
96  Tree::print_test_result(binary_tree, "@", fout);
97 
98  binary_tree.delLeft("L");
99  binary_tree.delRight("C");
100  binary_tree.delLeft("C");
101 
102  std::cout << "\n剪枝后:\n";
103  Tree::print_test_result(binary_tree, "@", std::cout);
104  fout << "\n剪枝后:\n";
105  Tree::print_test_result(binary_tree, "@", fout);
106 
107  t = clock() - t;
108  // printf("\nIt took me %ld clicks (%f seconds).\n", t, ((float)t) / CLOCKS_PER_SEC);
109  std::cout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
110  fout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
111 
112  fout.close();
113  system("pause");
114  return 0;
115 }
test0.hh
生成一个文件,内容是一棵二叉树的层次遍历(deprecated)
main
int main(int argc, char const *argv[])
二叉链表实现的二叉树类(binaryTree)的测试例程
Definition: test0.cc:37
data_file_name
const std::string data_file_name("ch6_3.result")
保存测试数据的文件名
Tree::print_test_result
static void print_test_result(const binaryTree< T, Comparator > &binary_tree, const typename binaryTree< T, Comparator >::value_type &flag, std::ostream &out=std::cout)
(测试用)将一棵二叉树的遍历结果插到输出流
Definition: test0.hh:108
Tree::binaryTree::delLeft
void delLeft(const value_type &x)
删除左subtree
Definition: binaryTree.hh:1031
Tree::binaryTree
用二叉链表实现的二叉树类
Definition: binaryTree.hh:53
Tree::GenTreeData
static bool GenTreeData(const char *_file_path="./test0.txt", const char *empty_flag="@")
生成一个文件,内容是一棵二叉树的层次遍历
Definition: test0.hh:39