二叉树类BinaryTree

二叉树是结点的有限集合, 该集合或者为空集, 或者是由一个根和两棵互不相交的称为该根的左子树和右子树的二叉树组成.

二叉树可以为空集, 可以有空二叉树, 也可以有空的左子树 或/和 又子树.

二叉树的性质: 1.第i层至多有2^(i - 1)个结点. 2.高度为h的二叉树上至多有2*h - 1个结点. 3.包含n个元素的二叉树高度至少为>=

log2(n + 1)取整. 3.任意一颗二叉树中, 若叶结点的个数为n0, 度为2的结点个数为n2, 则必有n0 = n2 + 1. 


树与二叉树区别: 1.树不能为空树, 二叉树可以为空. 2.树的子树之间是无序的, 其子树不分次序. 二叉树中结点的子树要分左右子树. 


满二叉树: 高度为h的二叉树恰好有2^h - 1个结点.

完全二叉树: 一棵二叉树中, 只有最下面两层结点的度可以小于2, 并且最下面一层的叶结点集中在靠左的若干位置上.

扩充二叉树(2 - 树): 除叶子结点外, 其余结点都必须有两个孩子.


二叉树类部分功能实现代码:

[cpp]  view plain copy
  1. #include "iostream"  
  2. #include "cstdio"  
  3. #include "cstring"  
  4. #include "algorithm"  
  5. using namespace std;  
  6. template <class T>  
  7. struct BTNode  
  8. {  
  9.     /* data */  
  10.     BTNode() { lChild = rChild = NULL; }  
  11.     BTNode(const T& x) {  
  12.         element = x;  
  13.         lChild = rChild = NULL;  
  14.     }  
  15.     BTNode(const T& x, BTNode<T>* l, BTNode<T>* r) {  
  16.         element = x;  
  17.         lChild = rChild = NULL;  
  18.     }  
  19.     T element;  
  20.     BTNode<T>* lChild, rChild;  
  21. };  
  22. template <class T>  
  23. class BinaryTree  
  24. {  
  25. public:  
  26.     BinaryTree() { root = NULL; }  
  27.     ~BinaryTree();  
  28.     bool IsEmpty() const// 判断是否为空, 是返回true  
  29.     void Clear(); // 移去所有结点, 成为空二叉树  
  30.     bool Root(T& x) const// 若二叉树为空, 则x为根的值, 返回true  
  31.     int Size(); // 返回二叉树结点个数  
  32.     void MakeTree(const T& x, BinaryTree<T>& left, BinaryTree<T>& right); // 构造一颗二叉树, 根的值为x, left & right为左右子树  
  33.     void BreakTree(T& x, BinaryTree<T>& left, BinaryTree<T>& right); // 拆分二叉树为三部分, x为根的值, left & right为左右子树  
  34.     void PreOrder(void (*Visit)(T& x)); // 先序遍历二叉树  
  35.     void InOrder(void (*Visit)(T& x)); // 中序遍历二叉树  
  36.     void PostOrder(void (*Visit)(T& x)); // 后序遍历二叉树  
  37.     /* data */  
  38. protected:  
  39.     BTNode<T>* root;  
  40. private:  
  41.     void Clear()(BTNode<T>* &t);  
  42.     void PreOrder(void (*Visit)(T &x), BTNode<T> *t);  
  43.     void InOrder(void (*Visit)(T &x), BTNode<T> *t);  
  44.     void PostOrder(void (*Visit)(T &x), BTNode<T> *t);  
  45. };  
  46. template <class T>  
  47. bool BinaryTree<T>::Root(T &x) const  
  48. {  
  49.     if(root) {  
  50.         x = root -> element;  
  51.         return true;  
  52.     }  
  53.     return false;  
  54. }  
  55. template <class T>  
  56. void BinaryTree<T>::MakeTree(const T& x, BinaryTree<T>& left, BinaryTree<T>& right)  
  57. {  
  58.     if(!root || &left == &right || left.root || right.root) return;  
  59.     x = root -> element;  
  60.     left.root = root -> lChild;  
  61.     right.root = root -> rChild;  
  62.     delete root;  
  63.     root = NULL;  
  64. }  
  65. template <class T>  
  66. void BinaryTree<T>::PreOrder(void (*Visit)(T& x))  
  67. {  
  68.     PreOrder(Visit, root);  
  69. }  
  70. template <class T>  
  71. void BinaryTree<T>::PreOrder(void (*Visit)(T& x), BTNode<T>* t)  
  72. {  
  73.     if(t) {  
  74.         Visit(t -> element);  
  75.         PreOrder(Visit, t -> lChild);  
  76.         PreOrder(Visit, t -> rChild);  
  77.     }  
  78. }  
  79. template <class T>  
  80. int BinaryTree<T>::Size()  
  81. {  
  82.     return Size(root);  
  83. }  
  84. template <class T>  
  85. int BinaryTree<T>::Size(BTNode<T> *t)  
  86. {  
  87.     if(!t) return 0;  
  88.     return Size(t -> lChild) + Size(t -> rChild) + 1;  
  89. }  


树与二叉树区别: 1.树不能为空树, 二叉树可以为空. 2.树的子树之间是无序的, 其子树不分次序. 二叉树中结点的子树要分左右子树. 

版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值