树  0.1
数据结构_第6章
test0.hh
Go to the documentation of this file.
1 
14 #ifndef INCLUDE_TEST0_HH_
15 #define INCLUDE_TEST0_HH_
16 
17 #include "Tree.h"
18 #include <ctime>
19 #include <fstream>
20 #include <iostream>
21 
22 namespace Tree
23 {
24 
39 static bool GenTreeData(const char *_file_path = "./test0.txt", const char *empty_flag = "@")
40 {
41  std::string file_path(_file_path);
42 
43  // 调用DOS命令,询问是否删除文件
44  std::string cmd = std::string("DEL /P \"") + file_path + '"';
45  system("@echo on");
46  system("echo We are trying to delete some files, which will be created later.");
47  system("pause");
48  system(cmd.c_str());
49  // system((std::string("IF NOT EXIST ") + '"' + file_path + "\" (").c_str());
50  // system("echo File has been deleted!");
51  // system(") ELSE (");
52  // system("echo Fail to delete files!");
53  // system(")");
54  // system("pause");
55 
56  // 检查文件是否存在且可读
57  std::ifstream fin(file_path.c_str(), std::ios_base::in);
58  if (fin.good())
59  {
60  std::cerr << "文件已存在!\n";
61  fin.close();
62 
63  system("pause");
64  return true;
65  }
66 
67  // 文件不存在或不可读,尝试新建
68  std::ofstream fout(file_path.c_str(), std::ios_base::out);
69  if (fout.fail())
70  {
71  std::cerr << "无写权限,文件生成失败!\n";
72 
73  system("pause");
74  return false;
75  }
76  fout << "A\n"
77  << "L\tC\n"
78  << "B\tE\t" << empty_flag << "\tD\n"
79  << empty_flag << '\t' << empty_flag << '\t' << empty_flag << '\t' << empty_flag << '\t' << "W\t" << empty_flag << '\n'
80  << empty_flag << "\tX\n"
81  << empty_flag << '\t' << empty_flag << std::endl;
82 
83  // 检查是否生成成功
84  if (fout.good())
85  {
86  system("echo File created successfully!");
87  fout.close();
88  system("pause");
89  return true;
90  }
91 
92  std::cerr << "无法生成文件!\n";
93  system("pause");
94  return false;
95 }
96 
107 template <typename T, typename Comparator>
108 static void print_test_result(const binaryTree<T, Comparator> &binary_tree, const typename binaryTree<T, Comparator>::value_type &flag, std::ostream &out = std::cout)
109 {
110  out << "二叉树的规模(递归 非递归):\n"
111  << binary_tree.size() << ' ' << binary_tree.size_loop();
112  out << "\n二叉树的高(深)度(递归 非递归),从0起:\n"
113  << binary_tree.height() << ' ' << binary_tree.height_loop();
114  out << "\n前序遍历(递归):\n";
115  binary_tree.preOrder(out);
116  out << "\n前序遍历(非递归):\n";
117  binary_tree.preOrder_loop(out);
118  out << "\n中序遍历(递归):\n";
119  binary_tree.inOrder(out);
120  out << "\n中序遍历(非递归):\n";
121  binary_tree.inOrder_loop(out);
122  out << "\n后序遍历(递归):\n";
123  binary_tree.postOrder(out);
124  out << "\n后序遍历(非递归):\n";
125  binary_tree.postOrder_loop(out);
126  out << "\n层次遍历:\n";
127  binary_tree.levelOrder(out);
128  out << "\n层次打印(调用lchild(), rchild(), root()等API):\n";
129  printBinaryTree(binary_tree, flag, out);
130 }
131 
132 } // namespace Tree
133 
134 #endif /* INCLUDE_TEST0_HH_ */
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::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
Tree
自定义的树类都在Tree名字空间内
Definition: binaryTree.hh:27
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::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::postOrder
void postOrder(std::ostream &out=std::cout) const
后序遍历(递归版本)
Definition: binaryTree.hh:984
Tree.h
Tree::binaryTree::preOrder
void preOrder(std::ostream &out=std::cout) const
前序遍历(递归版本)
Definition: binaryTree.hh:831
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
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