树  0.1
数据结构_第6章
ch6_3.cc
Go to the documentation of this file.
1 
14 #include "binaryTree.hh"
15 #include <ctime>
16 #include <fstream>
17 #include <string>
18 
19 // 常量定义
20 const std::string data_file_name("ch6_3.result");
21 
36 static bool GenTreeData(const char *_file_path = "./test0.txt", const char *empty_flag = "@");
37 
48 template <typename T, typename Comparator>
49 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);
50 
51 int main(int argc, char const *argv[])
52 {
53  // time
54  clock_t t = clock();
55  time_t rawtime;
56  struct tm *timeinfo;
57 
58  time(&rawtime); // Get the current calendar time
59  timeinfo = localtime(&rawtime); // Convert time_t to tm as local time
60 
61  // 获取文件路径,参考:http://www.cplusplus.com/reference/string/string/find_last_of/
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);
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  // ch6_3, ch6_4
113  binary_tree.delLeft("G");
114  binary_tree.delRight("G");
115  std::cout << "\n剪枝后:\n";
116  fout << "\n剪枝后:\n";
117  print_test_result(binary_tree, "@", std::cout);
118  print_test_result(binary_tree, "@", fout);
119 
120  t = clock() - t;
121  // printf("\nIt took me %ld clicks (%f seconds).\n", t, ((float)t) / CLOCKS_PER_SEC);
122  std::cout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
123  fout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
124 
125  fout.close();
126  system("pause");
127  return 0;
128 }
129 
130 bool GenTreeData(const char *_file_path, const char *empty_flag)
131 {
132  std::string file_path(_file_path);
133 
134  // 调用DOS命令,询问是否删除文件
135  std::string cmd = std::string("DEL /P \"") + file_path + '"';
136  system("@echo on");
137  system("echo We are trying to delete some files, which will be created later.");
138  system("pause");
139  system(cmd.c_str());
140 
141  // 检查文件是否存在且可读
142  std::ifstream fin(file_path.c_str(), std::ios_base::in);
143  if (fin.good())
144  {
145  std::cerr << "文件已存在!\n";
146  fin.close();
147 
148  system("pause");
149  return true;
150  }
151 
152  // 文件不存在或不可读,尝试新建
153  std::ofstream fout(file_path.c_str(), std::ios_base::out);
154  if (fout.fail())
155  {
156  std::cerr << "无写权限,文件生成失败!\n";
157 
158  system("pause");
159  return false;
160  }
161  fout << "A\n"
162  << "B\tC\n"
163  << "D\tE\tF\tG\n"
164  << empty_flag << '\t' << empty_flag << '\t' << empty_flag << '\t' << empty_flag << '\t' << empty_flag << '\t' << empty_flag << '\t' << "H\tI\n"
165  << empty_flag << '\t' << empty_flag << '\t' << empty_flag << '\t' << empty_flag << std::endl;
166 
167  // 检查是否生成成功
168  if (fout.good())
169  {
170  system("echo File created successfully!");
171  fout.close();
172  system("pause");
173  return true;
174  }
175 
176  std::cerr << "无法生成文件!\n";
177  system("pause");
178  return false;
179 }
180 
181 template <class T, typename Comparator>
182 void print_test_result(const Tree::binaryTree<T, Comparator> &binary_tree, const typename Tree::binaryTree<T, Comparator>::value_type &flag, std::ostream &out)
183 {
184  out << "二叉树的规模(递归 非递归):\n"
185  << binary_tree.size() << ' ' << binary_tree.size_loop();
186  out << "\n二叉树的高(深)度(递归 非递归),从0起:\n"
187  << binary_tree.height() << ' ' << binary_tree.height_loop();
188  // ch6_2
189  out << "\n度为2的结点的个数是:\n"
190  << binary_tree.CountDegreeTwo();
191  // ch6_3
192  out << "\n是否为满二叉树:\n"
193  << std::boolalpha << binary_tree.isFullBinaryTree();
194  // ch6_4
195  out << "\n是否为完全二叉树:\n"
196  << std::boolalpha << binary_tree.isCompleteTree();
197  out << "\n前序遍历(递归):\n";
198  binary_tree.preOrder(out);
199  out << "\n前序遍历(非递归):\n";
200  binary_tree.preOrder_loop(out);
201  out << "\n中序遍历(递归):\n";
202  binary_tree.inOrder(out);
203  out << "\n中序遍历(非递归):\n";
204  binary_tree.inOrder_loop(out);
205  out << "\n后序遍历(递归):\n";
206  binary_tree.postOrder(out);
207  out << "\n后序遍历(非递归):\n";
208  binary_tree.postOrder_loop(out);
209  out << "\n层次遍历:\n";
210  binary_tree.levelOrder(out);
211  out << "\n层次打印(调用lchild(), rchild(), root()等API):\n";
212  Tree::printBinaryTree(binary_tree, flag, out);
213 }
GenTreeData
static bool GenTreeData(const char *_file_path="./test0.txt", const char *empty_flag="@")
生成一个文件,内容是一棵二叉树的层次遍历
Definition: ch6_3.cc:130
Tree::binaryTree::isFullBinaryTree
bool isFullBinaryTree() const
是否满二叉树
Definition: binaryTree.hh:699
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::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
Tree::binaryTree::isCompleteTree
bool isCompleteTree() const
返回是否完全二叉树
Definition: binaryTree.hh:711
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
main
int main(int argc, char const *argv[])
Definition: ch6_3.cc:51
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