树  0.1
数据结构_第6章
ch6_1.cc
Go to the documentation of this file.
1 
14 #include "binaryTree.hh"
15 #include <ctime>
16 #include <fstream>
17 #include <string>
18 
33 static bool GenTreeData(const char *_file_path = "./test0.txt", const char *empty_flag = "@");
34 
47 template <typename T, typename Comparator>
48 static void print_test_result(const Tree::binaryTree<T, Comparator> &binary_tree, const typename Tree::binaryTree<T, Comparator>::value_type &flag, std::ostream &out = std::cout);
49 
50 int main(int argc, char const *argv[])
51 {
52  // time
53  clock_t t = clock();
54  time_t rawtime;
55  struct tm *timeinfo;
56 
57  time(&rawtime); // Get the current calendar time
58  timeinfo = localtime(&rawtime); // Convert time_t to tm as local time
59 
60  // 获取文件路径,参考:http://www.cplusplus.com/reference/string/string/find_last_of/
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);
65  const std::string data_file_name("ch6_1.result");
66 
67  // 生成数据文件,然后从文件中读取数据,依此建立一棵二叉树
68  std::string data_file_full_path{exec_path + data_file_name}; // 数据文件的绝对地址
69 
70  // 生成数据文件
71  if (!GenTreeData(data_file_full_path.c_str(), "@"))
72  {
73  std::cout << "文件:" << data_file_full_path << "生成失败!\n";
74  return 1;
75  }
76 
77  // 读数据文件
78  std::ifstream fin(data_file_full_path.c_str(), std::ios_base::in);
79  if (fin.fail())
80  {
81  std::cout << "文件:" << data_file_full_path << "读取失败!\n";
82  return 1;
83  }
84 
85  // 建立二叉树
86  Tree::binaryTree<std::string> binary_tree{};
87  binary_tree.createTree("@", fin);
88  fin.close();
89 
90  // 准备将结果写入数据文件
91  std::ofstream fout(data_file_full_path.c_str(), std::ios_base::app);
92  if (fout.fail())
93  {
94  std::cerr << "文件:" << data_file_full_path << "写入失败!\n";
95  return 1;
96  }
97 
98  // printf("Current local time and date: %s\n", asctime(timeinfo)); // Convert tm structure to string
99  std::cout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
100  fout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
101 
102  print_test_result(binary_tree, "@", std::cout);
103  print_test_result(binary_tree, "@", fout);
104 
105  // ch6_1
106  binary_tree.swaplr();
107  std::cout << "\n交换左右结点后:\n";
108  fout << "\n交换左右结点后:\n";
109  print_test_result(binary_tree, "@", std::cout);
110  print_test_result(binary_tree, "@", fout);
111 
112  t = clock() - t;
113  // printf("\nIt took me %ld clicks (%f seconds).\n", t, ((float)t) / CLOCKS_PER_SEC);
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";
116 
117  fout.close();
118  system("pause");
119  return 0;
120 }
121 
122 bool GenTreeData(const char *_file_path, const char *empty_flag)
123 {
124  std::string file_path(_file_path);
125 
126  // 调用DOS命令,询问是否删除文件
127  std::string cmd = std::string("DEL /P \"") + file_path + '"';
128  system("@echo on");
129  system("echo We are trying to delete some files, which will be created later.");
130  system("pause");
131  system(cmd.c_str());
132 
133  // 检查文件是否存在且可读
134  std::ifstream fin(file_path.c_str(), std::ios_base::in);
135  if (fin.good())
136  {
137  std::cerr << "文件已存在!\n";
138  fin.close();
139 
140  system("pause");
141  return true;
142  }
143 
144  // 文件不存在或不可读,尝试新建
145  std::ofstream fout(file_path.c_str(), std::ios_base::out);
146  if (fout.fail())
147  {
148  std::cerr << "无写权限,文件生成失败!\n";
149 
150  system("pause");
151  return false;
152  }
153  fout << "A\n"
154  << "L\tC\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;
159 
160  // 检查是否生成成功
161  if (fout.good())
162  {
163  system("echo File created successfully!");
164  fout.close();
165  system("pause");
166  return true;
167  }
168 
169  std::cerr << "无法生成文件!\n";
170  system("pause");
171  return false;
172 }
173 
174 template <class T, typename Comparator>
175 void print_test_result(const Tree::binaryTree<T, Comparator> &binary_tree, const typename Tree::binaryTree<T, Comparator>::value_type &flag, std::ostream &out)
176 {
177  out << "二叉树的规模(递归 非递归):\n"
178  << binary_tree.size() << ' ' << binary_tree.size_loop();
179  out << "\n二叉树的高(深)度(递归 非递归),从0起:\n"
180  << binary_tree.height() << ' ' << binary_tree.height_loop();
181  out << "\n前序遍历(递归):\n";
182  binary_tree.preOrder(out);
183  out << "\n前序遍历(非递归):\n";
184  binary_tree.preOrder_loop(out);
185  out << "\n中序遍历(递归):\n";
186  binary_tree.inOrder(out);
187  out << "\n中序遍历(非递归):\n";
188  binary_tree.inOrder_loop(out);
189  out << "\n后序遍历(递归):\n";
190  binary_tree.postOrder(out);
191  out << "\n后序遍历(非递归):\n";
192  binary_tree.postOrder_loop(out);
193  out << "\n层次遍历:\n";
194  binary_tree.levelOrder(out);
195  out << "\n层次打印(调用lchild(), rchild(), root()等API):\n";
196  Tree::printBinaryTree(binary_tree, flag, out);
197 }
GenTreeData
static bool GenTreeData(const char *_file_path="./test0.txt", const char *empty_flag="@")
生成一个文件,内容是一棵二叉树的层次遍历
Definition: ch6_1.cc:122
Tree::binaryTree::inOrder_loop
void inOrder_loop(std::ostream &out=std::cout) const
中序遍历(非递归版本)
Definition: binaryTree.hh:848
Tree::binaryTree::value_type
T value_type
类型别名定义
Definition: binaryTree.hh:108
Tree::binaryTree::preOrder_loop
void preOrder_loop(std::ostream &out=std::cout) const
前序遍历(非递归版本)
Definition: binaryTree.hh:769
print_test_result
static void print_test_result(const Tree::binaryTree< T, Comparator > &binary_tree, const typename Tree::binaryTree< T, Comparator >::value_type &flag, std::ostream &out=std::cout)
(测试用)将二叉树类的多种方法的输出插到输出流
Definition: ch6_1.cc:175
Tree::binaryTree::height_loop
size_type height_loop() const
返回二叉树的高度(非递归版本)
Definition: binaryTree.hh:1201
Tree::binaryTree::size
size_type size() const
返回二叉树的结点数(递归版本)
Definition: binaryTree.hh:1312
Tree::binaryTree::levelOrder
void levelOrder(std::ostream &out=std::cout) const
层次遍历
Definition: binaryTree.hh:990
data_file_name
const std::string data_file_name("ch6_3.result")
保存测试数据的文件名
Tree::binaryTree::inOrder
void inOrder(std::ostream &out=std::cout) const
中序遍历(递归版本)
Definition: binaryTree.hh:906
Tree::binaryTree::size_loop
size_type size_loop() const
返回二叉树的结点数(非递归版本)
Definition: binaryTree.hh:1318
Tree::binaryTree::height
size_type height() const
返回二叉树的高度(递归版本)
Definition: binaryTree.hh:1195
Tree::binaryTree::postOrder_loop
void postOrder_loop(std::ostream &out=std::cout) const
后序遍历(非递归版本)
Definition: binaryTree.hh:923
Tree::binaryTree::postOrder
void postOrder(std::ostream &out=std::cout) const
后序遍历(递归版本)
Definition: binaryTree.hh:984
Tree::binaryTree::createTree
void createTree(const value_type &flag=value_type{}, std::istream &in=std::cin)
Create a Tree object.
Definition: binaryTree.hh:1324
main
int main(int argc, char const *argv[])
Definition: ch6_1.cc:50
Tree::binaryTree::preOrder
void preOrder(std::ostream &out=std::cout) const
前序遍历(递归版本)
Definition: binaryTree.hh:831
binaryTree.hh
Tree::binaryTree
用二叉链表实现的二叉树类
Definition: binaryTree.hh:53
Tree::printBinaryTree
void printBinaryTree(const binaryTree< T, Comparator > &bin_tree, const typename binaryTree< T, Comparator >::value_type &flag, std::ostream &out=std::cout)
输出一棵二叉树
Definition: binaryTree.hh:1426