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