树
0.1
数据结构_第6章
|
用二叉链表实现的二叉树类 More...
#include <binaryTree.hh>
Classes | |
struct | BinNode |
二叉链表的结点类 More... | |
Public Types | |
typedef T | value_type |
类型别名定义 More... | |
typedef value_type & | reference |
数据的引用 More... | |
typedef size_t | size_type |
计数器类型 More... | |
Public Member Functions | |
binaryTree () | |
Construct a new binary Tree object. More... | |
binaryTree (const value_type &x) | |
Construct a new binary Tree object. More... | |
binaryTree (const binaryTree &rhs) | |
Construct a new binary Tree object 拷贝构造函数 More... | |
binaryTree & | operator= (const binaryTree &rhs) |
赋值运算符重载 More... | |
binaryTree (binaryTree &&rhs) | |
Construct a new binary Tree object 移动构造函数 More... | |
binaryTree & | operator= (binaryTree &&rhs) |
移动赋值运算符 More... | |
~binaryTree () | |
Destroy the binary Tree object. More... | |
void | clear () |
清空二叉树 函数返回后,对象的成员root_将被置nullptr More... | |
bool | empty () const |
判断二叉树是否为空树 More... | |
value_type | root (const value_type &flag) const |
返回根结点的数据 More... | |
value_type | lchild (const value_type &x, const value_type &flag) const |
获得数据为x的结点的左child More... | |
value_type | rchild (const value_type &x, const value_type &flag) const |
获得数据为x的结点的右child More... | |
void | delLeft (const value_type &x) |
删除左subtree More... | |
void | delRight (const value_type &x) |
删除右subtree More... | |
void | preOrder (std::ostream &out=std::cout) const |
前序遍历(递归版本) More... | |
void | preOrder_loop (std::ostream &out=std::cout) const |
前序遍历(非递归版本) More... | |
void | inOrder (std::ostream &out=std::cout) const |
中序遍历(递归版本) More... | |
void | inOrder_loop (std::ostream &out=std::cout) const |
中序遍历(非递归版本) More... | |
void | postOrder (std::ostream &out=std::cout) const |
后序遍历(递归版本) More... | |
void | postOrder_loop (std::ostream &out=std::cout) const |
后序遍历(非递归版本) More... | |
void | levelOrder (std::ostream &out=std::cout) const |
层次遍历 More... | |
void | createTree (const value_type &flag=value_type{}, std::istream &in=std::cin) |
Create a Tree object. More... | |
value_type | parent (value_type x, const value_type &flag) const |
返回数据为x的结点的parent结点 More... | |
size_type | height () const |
返回二叉树的高度(递归版本) More... | |
size_type | height_loop () const |
返回二叉树的高度(非递归版本) More... | |
size_type | size () const |
返回二叉树的结点数(递归版本) More... | |
size_type | size_loop () const |
返回二叉树的结点数(非递归版本) More... | |
void | swaplr () |
交换左右子树 More... | |
size_type | CountDegreeTwo () const |
统计树中度为2的结点的个数 More... | |
bool | isCompleteTree () const |
返回是否完全二叉树 More... | |
bool | isFullBinaryTree () const |
是否满二叉树 More... | |
Static Public Member Functions | |
static bool | is_symmetrical_tree (const binaryTree &lhs, const binaryTree &rhs) |
判断两棵二叉树是否互为镜像 两棵二叉树#1和#2互为镜像,如果二者皆为空树;或二者根结点的值相同,且#1的左、右子树与#2的右、左子树互为镜像 More... | |
Public Attributes | |
const typedef value_type & | const_reference |
数据的常量引用 More... | |
Private Member Functions | |
BinNode * | find (const value_type &x, BinNode *root) const |
在以root为根的树中(前序)查找数据为x的结点,返回指向该结点的指针 More... | |
void | clear (BinNode *&root) |
清空以root为根结点的树,包括清除根结点 More... | |
BinNode * | clone (BinNode *_root) const |
Internal method to clone subtree. More... | |
void | preOrder (BinNode *root, std::ostream &out=std::cout) const |
前序遍历以root为根结点的树(递归版本) More... | |
void | inOrder (BinNode *root, std::ostream &out=std::cout) const |
中序遍历以root为根结点的树(递归版本) More... | |
void | postOrder (BinNode *root, std::ostream &out=std::cout) const |
后序遍历以root为根结点的树(递归版本) More... | |
size_type | size (BinNode *_root) const |
返回以指定结点为根结点的二叉树的规模(递归版本) More... | |
size_type | size_loop (BinNode *_root) const |
返回以指定结点为根结点的二叉树的规模(非递归版本) More... | |
size_type | height (BinNode *_root) const |
返回以指定结点为根结点的二叉树的高度(递归版本) More... | |
size_type | height_loop (BinNode *_root) const |
返回以指定结点为根结点的二叉树的高度(非递归版本) More... | |
size_type | CountDegreeTwo (BinNode *_root) const |
统计树中度为2的结点的个数 More... | |
void | swaplr (BinNode *_root) |
交换左右子树 More... | |
Static Private Member Functions | |
static bool | is_symmetrical_subtree (BinNode *root1, BinNode *root2) |
Internal method to 判断两棵二叉树是否相互对称 More... | |
static bool | value_type_less (const value_type &lhs, const value_type &rhs) |
比较value_type类型对象的大小。 More... | |
static bool | value_type_equal (const value_type &lhs, const value_type &rhs) |
比较value_type类型对象的大小。 More... | |
static bool | value_type_less_equal (const value_type &lhs, const value_type &rhs) |
比较value_type类型对象的大小。 More... | |
static bool | is_equal_subtree (BinNode *_root1, BinNode *_root2) |
判断两棵二叉树是否相同 "相同"是指:两棵树的形状及对应结点的值相等 More... | |
Private Attributes | |
BinNode * | root_ |
二叉树的根结点 More... | |
Static Private Attributes | |
static Comparator | is_less_than_ = Comparator{} |
比较器(function object) More... | |
Friends | |
bool | operator== (const binaryTree &lhs, const binaryTree &rhs) |
比较两棵二叉树是否相同 "相同"是指: More... | |
void | printBinaryTree (const binaryTree &bin_tree, const typename binaryTree ::value_type &flag, std::ostream &out) |
用二叉链表实现的二叉树类
T | 数据类型 |
Comparator | 数据类型的比较器类,默认为std::less<T> |
若类型T重载了operator<,则Comparator的默认类型std::less<T>将调用operator<,故实例化时不必指定tparam2;
否则,要求指定模板参数Comparator,which is a function object, 重载了bool operator(T t1, T t2):当语义上t1 < t2时返回true。
要求对T重载了operator<< 和 operator>>。建议每个对象的输出在一行以内
Definition at line 53 of file binaryTree.hh.
typedef value_type& Tree::binaryTree< T, Comparator >::reference |
数据的引用
Definition at line 109 of file binaryTree.hh.
typedef size_t Tree::binaryTree< T, Comparator >::size_type |
计数器类型
Definition at line 111 of file binaryTree.hh.
typedef T Tree::binaryTree< T, Comparator >::value_type |
|
inline |
|
inlineexplicit |
Construct a new binary Tree object.
创建一棵带有根节点的树
x | 根结点的数据 |
Definition at line 169 of file binaryTree.hh.
|
inline |
Construct a new binary Tree object 拷贝构造函数
rhs | 源对象 |
Definition at line 177 of file binaryTree.hh.
|
inline |
Construct a new binary Tree object 移动构造函数
rhs | 源对象,右值 |
Definition at line 198 of file binaryTree.hh.
Tree::binaryTree< T, Comparator >::~binaryTree |
Destroy the binary Tree object.
Definition at line 654 of file binaryTree.hh.
void Tree::binaryTree< T, Comparator >::clear |
清空二叉树 函数返回后,对象的成员root_将被置nullptr
Definition at line 648 of file binaryTree.hh.
References Tree::binaryTree< T, Comparator >::BinNode::data, Tree::binaryTree< T, Comparator >::BinNode::left, and Tree::binaryTree< T, Comparator >::BinNode::right.
|
private |
清空以root为根结点的树,包括清除根结点
root | 指向目标树的根结点的指针 |
函数执行完毕后,root将被置为nullptr
Definition at line 636 of file binaryTree.hh.
|
private |
Internal method to clone subtree.
_root | 指向源二叉树的根结点的指针 |
Definition at line 660 of file binaryTree.hh.
|
inline |
统计树中度为2的结点的个数
Definition at line 409 of file binaryTree.hh.
Referenced by print_test_result().
|
private |
void Tree::binaryTree< T, Comparator >::createTree | ( | const value_type & | flag = value_type{} , |
std::istream & | in = std::cin |
||
) |
Create a Tree object.
flag | "空结点"的特殊标记 |
in | 输入流对象,可以是cin, 或者ifstream类的对象等 |
@ details 该方法首先调用clear()清空二叉树。 然后要从输入流(标准/文件流)按层次顺序输入在建二叉树各结点的值,依此建立一棵二叉树
输入flag表示"空结点"
Definition at line 1324 of file binaryTree.hh.
Referenced by main().
void Tree::binaryTree< T, Comparator >::delLeft | ( | const value_type & | x | ) |
删除左subtree
x | 目标结点。以该结点的左child为根的树将被删除 |
Definition at line 1031 of file binaryTree.hh.
Referenced by main().
void Tree::binaryTree< T, Comparator >::delRight | ( | const value_type & | x | ) |
删除右subtree
x | 目标结点。以该节点的右child为根的树将被删除 |
Definition at line 1041 of file binaryTree.hh.
Referenced by main().
bool Tree::binaryTree< T, Comparator >::empty |
|
private |
在以root为根的树中(前序)查找数据为x的结点,返回指向该结点的指针
x | 目标数据 |
root | 目标二叉树的根结点的地址 |
Definition at line 1015 of file binaryTree.hh.
binaryTree< T, Comparator >::size_type Tree::binaryTree< T, Comparator >::height |
返回二叉树的高度(递归版本)
The height of n_i is the length of the longest path from n_i to a leaf. Thus all leaves are at height 0.
The height of a tree is equal to the height of the root.
Definition at line 1195 of file binaryTree.hh.
Referenced by print_test_result(), and Tree::print_test_result().
|
private |
返回以指定结点为根结点的二叉树的高度(递归版本)
_root | 指向根结点的指针 |
Definition at line 1093 of file binaryTree.hh.
binaryTree< T, Comparator >::size_type Tree::binaryTree< T, Comparator >::height_loop |
返回二叉树的高度(非递归版本)
The height of n_i is the length of the longest path from n_i to a leaf. Thus all leaves are at height 0.
The height of a tree is equal to the height of the root.
Definition at line 1201 of file binaryTree.hh.
Referenced by print_test_result(), and Tree::print_test_result().
|
private |
返回以指定结点为根结点的二叉树的高度(非递归版本)
_root | 指向根结点的指针 |
Definition at line 1104 of file binaryTree.hh.
|
private |
中序遍历以root为根结点的树(递归版本)
root | 指向目标树的根结点的指针 |
out | 输出流对象,可以是标准输出流或文件流等 |
Definition at line 837 of file binaryTree.hh.
void Tree::binaryTree< T, Comparator >::inOrder | ( | std::ostream & | out = std::cout | ) | const |
中序遍历(递归版本)
中序打印二叉树各结点的数据到标准输出设备
out | 输出流对象,可以是标准输出流或文件流等 |
Definition at line 906 of file binaryTree.hh.
Referenced by print_test_result(), and Tree::print_test_result().
void Tree::binaryTree< T, Comparator >::inOrder_loop | ( | std::ostream & | out = std::cout | ) | const |
中序遍历(非递归版本)
中序打印二叉树各结点的数据到标准输出设备
out | 输出流对象,可以是标准输出流或文件流等 |
Definition at line 848 of file binaryTree.hh.
Referenced by print_test_result(), and Tree::print_test_result().
|
staticprivate |
判断两棵二叉树是否相同 "相同"是指:两棵树的形状及对应结点的值相等
_root1 | 指向二叉树#1的根结点的指针 |
_root2 | 指向二叉树#2的根结点的指针 |
Definition at line 684 of file binaryTree.hh.
|
staticprivate |
Internal method to 判断两棵二叉树是否相互对称
root1 | 指向二叉树#1的根结点的指针 |
root2 | 指向二叉树#2的根结点的指针 |
Definition at line 668 of file binaryTree.hh.
|
inlinestatic |
判断两棵二叉树是否互为镜像 两棵二叉树#1和#2互为镜像,如果二者皆为空树;或二者根结点的值相同,且#1的左、右子树与#2的右、左子树互为镜像
lhs | 二叉树#1 |
rhs | 二叉树#2 |
Definition at line 437 of file binaryTree.hh.
bool Tree::binaryTree< T, Comparator >::isCompleteTree |
返回是否完全二叉树
至此,当前结点处理完毕。 若当前结点有空孩子,则它是层序上的首个度非2结点。 从而是完全二叉树iff.其后(不含)的结点全为叶子结点。
函数只可能在此if中返回
Definition at line 711 of file binaryTree.hh.
References Tree::binaryTree< T, Comparator >::BinNode::left, and Tree::binaryTree< T, Comparator >::BinNode::right.
Referenced by print_test_result().
bool Tree::binaryTree< T, U >::isFullBinaryTree |
是否满二叉树
Definition at line 699 of file binaryTree.hh.
References Queue::seqQueue< T >::push().
Referenced by print_test_result().
binaryTree< T, Comparator >::value_type Tree::binaryTree< T, Comparator >::lchild | ( | const value_type & | x, |
const value_type & | flag | ||
) | const |
获得数据为x的结点的左child
x | 目标结点的数据 |
flag | 无左child时返回的标记 |
Definition at line 1051 of file binaryTree.hh.
void Tree::binaryTree< T, Comparator >::levelOrder | ( | std::ostream & | out = std::cout | ) | const |
层次遍历
层次打印二叉树各结点的数据到标准输出设备
out | 输出流对象,可以是标准输出流或文件流等 |
Definition at line 990 of file binaryTree.hh.
Referenced by print_test_result(), and Tree::print_test_result().
|
inline |
移动赋值运算符
rhs | 源对象,右值 |
Definition at line 206 of file binaryTree.hh.
|
inline |
|
inline |
返回数据为x的结点的parent结点
x | 目标结点的数据 |
flag | parent不存在时返回的特殊标记 |
Definition at line 358 of file binaryTree.hh.
|
private |
后序遍历以root为根结点的树(递归版本)
root | 指向目标树的根结点的指针 |
out | 输出流对象,可以是标准输出流或文件流等 |
Definition at line 912 of file binaryTree.hh.
void Tree::binaryTree< T, Comparator >::postOrder | ( | std::ostream & | out = std::cout | ) | const |
后序遍历(递归版本)
后序打印二叉树各结点的数据到标准输出设备
out | 输出流对象,可以是标准输出流或文件流等 |
Definition at line 984 of file binaryTree.hh.
Referenced by print_test_result(), and Tree::print_test_result().
void Tree::binaryTree< T, Comparator >::postOrder_loop | ( | std::ostream & | out = std::cout | ) | const |
后序遍历(非递归版本)
后序打印二叉树各结点的数据到标准输出设备
out | 输出流对象,可以是标准输出流或文件流等 |
连续递归调用,归入一个stage,直接反序进栈即可
Definition at line 923 of file binaryTree.hh.
Referenced by print_test_result(), and Tree::print_test_result().
|
private |
前序遍历以root为根结点的树(递归版本)
root | 指向目标树的根结点的指针 |
out | 输出流对象,可以是标准输出流或文件流等 |
Definition at line 758 of file binaryTree.hh.
void Tree::binaryTree< T, Comparator >::preOrder | ( | std::ostream & | out = std::cout | ) | const |
前序遍历(递归版本)
前序打印二叉树各结点的数据到标准输出设备
out | 输出流对象,可以是标准输出流或文件流等 |
Definition at line 831 of file binaryTree.hh.
Referenced by print_test_result(), and Tree::print_test_result().
void Tree::binaryTree< T, Comparator >::preOrder_loop | ( | std::ostream & | out = std::cout | ) | const |
前序遍历(非递归版本)
前序打印二叉树各结点的数据到标准输出设备
out | 输出流对象,可以是标准输出流或文件流等 |
引用类型的形参不需要记录。 只需记录BinNode* _root。 首次调用时,实参就是对象的根结点root_; 该细节应被封装,故提供给用户的函数形参表不设_root。 递归版本是有限次tail recursion,不必记录stage。
首次调用时,实参就是对象的根结点root_; 该细节应被封装,故提供给用户的函数形参表不设_root。
函数体内的递归调用返回后,唯一的行为是新的递归调用, 故不必保存断点(分stage),直接将所有tail recursions按反序进栈即可。 注意到若某次递归调用的实参为0将导致空操作,所以若为空则不必进栈 (导致两次访问,可选)。
Definition at line 769 of file binaryTree.hh.
Referenced by print_test_result(), and Tree::print_test_result().
binaryTree< T, Comparator >::value_type Tree::binaryTree< T, Comparator >::rchild | ( | const value_type & | x, |
const value_type & | flag | ||
) | const |
获得数据为x的结点的右child
x | 目标结点的数据 |
flag | 无右child时返回的标记 |
Definition at line 1061 of file binaryTree.hh.
Referenced by Tree::printBinaryTree().
binaryTree< T, Comparator >::value_type Tree::binaryTree< T, Comparator >::root | ( | const value_type & | flag | ) | const |
返回根结点的数据
flag | 为空树时返回的特殊标记 |
Definition at line 627 of file binaryTree.hh.
binaryTree< T, Comparator >::size_type Tree::binaryTree< T, Comparator >::size |
返回二叉树的结点数(递归版本)
Definition at line 1312 of file binaryTree.hh.
Referenced by print_test_result(), and Tree::print_test_result().
|
private |
返回以指定结点为根结点的二叉树的规模(递归版本)
_root | 指向根结点的指针 |
Definition at line 1207 of file binaryTree.hh.
binaryTree< T, Comparator >::size_type Tree::binaryTree< T, Comparator >::size_loop |
返回二叉树的结点数(非递归版本)
Definition at line 1318 of file binaryTree.hh.
Referenced by print_test_result(), and Tree::print_test_result().
|
private |
返回以指定结点为根结点的二叉树的规模(非递归版本)
_root | 指向根结点的指针 |
https://www.codeproject.com/Articles/418776/How-to-replace-recursive-functions-using-stack-and
Definition at line 1220 of file binaryTree.hh.
|
inline |
交换左右子树
_root | 指向树的根结点的指针 |
Definition at line 404 of file binaryTree.hh.
Referenced by main().
|
private |
|
inlinestaticprivate |
比较value_type类型对象的大小。
lhs | 左运算数 |
rhs | 右运算数 |
Definition at line 538 of file binaryTree.hh.
|
inlinestaticprivate |
比较value_type类型对象的大小。
lhs | 左运算数 |
rhs | 右运算数 |
Definition at line 528 of file binaryTree.hh.
|
inlinestaticprivate |
比较value_type类型对象的大小。
lhs | 左运算数 |
rhs | 右运算数 |
Definition at line 548 of file binaryTree.hh.
|
friend |
比较两棵二叉树是否相同 "相同"是指:
不能在此声明下述友元(invalid use of incomplete type), 原因:类的内部类型value_type未声明。 只需把友元声明置于typedef后即可
T | 数据类型 |
Comparator | 数据类型的比较器类,默认为std::less<T> |
若类型T重载了operator<,则Comparator的默认类型std::less<T>将调用operator<,故实例化时不必指定tparam2;
否则,要求指定模板参数Comparator,which is a function object, 重载了bool operator(T t1, T t2):当语义上t1 < t2时返回true。
lhs | 二叉树#1 |
rhs | 二叉树#2 |
Definition at line 1462 of file binaryTree.hh.
|
friend |
const typedef value_type& Tree::binaryTree< T, Comparator >::const_reference |
数据的常量引用
Definition at line 110 of file binaryTree.hh.
|
staticprivate |
比较器(function object)
Definition at line 518 of file binaryTree.hh.
|
private |
二叉树的根结点
Definition at line 151 of file binaryTree.hh.