C++ 数据结构学习 ---- 二叉树

目录

1. 头文件

1.1 二叉树节点文件

1.2 二叉树文件

2. 二叉树相关函数

2.1 更新高度

2.2 插入

2.3 删除

2.4 子树接入

2.5 分离子树

2.6 左子树深入

2.7 寻找左侧最高可见叶节点

3. 相关算法

3.1 层次遍历算法

3.2 先序遍历算法(3种)

3.3 中序遍历(5种)

3.4 后序遍历算法(2种)

4. 完整代码

5. 运行结果截图


1. 头文件

1.1 二叉树节点文件

#include"Stack.h"
#include"Queue.h"

//BinNode状态与性质的判断
 #define IsRoot(x) (!((x).parent))
 #define IsLChild(x) (!IsRoot(x) && (&(x) == (x).parent->lc))
 #define IsRChild(x) (!IsRoot(x) && (&(x) == (x).parent->rc))
 #define HasParent(x) (!IsRoot(x))
 #define HasLChild(x) ((x).lc)
 #define HasRChild(x) ((x).rc)
 #define HasChild(x) (HasLChild(x) || HasRChild(x)) //至少拥有一个孩子
 #define HasBothChild(x) (HasLChild(x) && HasRChild(x)) //同时拥有两个孩子
 #define IsLeaf(x) (!HasChild(x))
//与BinNode具有特定关系的节点及指针
#define sibling( p ) ( IsLChild( * (p) ) ? (p)->parent->rc : (p)->parent->lc ) /*兄弟*/
#define uncle(x) (sibling((x)->parent)) /*叔叔*/
#define FromParentTo( x ) /*来自父亲的引用*/ \
( IsRoot(x) ? _root : ( IsLChild(x) ? (x).parent->lc : (x).parent->rc ) )






#define BinNodePosi(T) BinNode<T>* //节点位置
#define stature(p) ((p)?(p->height):-1)//节点高度,空树高度为-1
typedef enum{RB_RED=0,RB_BLACK}RBColor;//节点颜色
//节点类
template <typename T> struct BinNode {
	BinNodePosi(T) parent;//父亲
	BinNodePosi(T) lc;//左孩子
	BinNodePosi(T) rc; //右孩子
	T data; //数据
	int height; //高度(通用)
	int npl;//左式堆
	RBColor color;
	//构造函数
	BinNode() :
	   parent(NULL), lc(NULL), rc(NULL), height(0), npl(1), color(RB_RED) { }
	 BinNode(T e, BinNodePosi(T) p = NULL, BinNodePosi(T) lc = NULL, BinNodePosi(T) rc = NULL,
		           int h = 0, int l = 1, RBColor c = RB_RED) :
		     data(e), parent(p), lc(lc), rc(rc), height(h), npl(l), color(c) { }
	//操作接口
	    int size(); //统计当前节点后代总数,亦即以其为根的子树的规模
	    BinNodePosi(T) insertAsLC(T const&); //作为当前节点的左孩子插入新节点
	    BinNodePosi(T) insertAsRC(T const&); //作为当前节点的右孩子插入新节点
	    BinNodePosi(T) succ(); //取当前节点的直接后继
	    template <typename VST> void travLevel(VST&); //子树层次遍历
	    template <typename VST> void travPre(VST&); //子树先序遍历
	    template <typename VST> void travIn(VST&); //子树中序遍历
	    template <typename VST> void travPost(VST&); //子树后序遍历
	 // 比较器、判等器
	   bool operator> (BinNode const& bn) { return data > bn.data; } //大于
	   bool operator< (BinNode const& bn) { return data < bn.data; } //小于
	   bool operator== (BinNode const& bn){ return data == bn.data; } //等于
	   bool operator!= (BinNode const& bn){ return data != bn.data; } //不等于于
	
};
//统计当前节点后代总数,即以其为根的子树规模
template <typename T> int BinNode<T>::size() { 
	  int s = 1; //计入本身
	  if (lc) s += lc->size(); //递归计入左子树规模
	  if (rc) s += rc->size(); //递归计入右子树规模
	  return s;
}
//将e作为当前节点的左孩子插入二叉树
 template <typename T> BinNodePosi(T) BinNode<T>::insertAsLC(T const& e)
 { return lc = new BinNode(e, this); } 
//将e作为当前节点的右孩子插入二叉树
 template <typename T> BinNodePosi(T) BinNode<T>::insertAsRC(T const& e)
 { return rc = new BinNode(e, this); }
//定位节点v的直接后继
template <typename T> BinNodePosi(T) BinNode<T>::succ() { 
 BinNodePosi(T) s = this; //记录后继的临时变量
	     if (rc) { //若有右孩子,则直接后继必在右子树中,具体地就是
	       s = rc; //右子树中
	       while (HasLChild(*s)) s = s->lc; //最靠左(最小)的节点
	 }
	 else { //否则,直接后继应是“将当前节点包含于其左子树中的最低祖先”,具体地就是
		     while (IsRChild(*s)) s = s->parent; //逆向地沿右向分支,不断朝左上方移动
		     s = s->parent; //最后再朝右上方移动一步,即抵达直接后继(如果存在)	
	 }
  return s;
 }


1.2 二叉树文件

注:要现在当前头文件之前引入二叉树节点头文件


#include "BinNode.h"

template <typename T> class BinTree { 
 protected:
	  int _size; BinNodePosi(T) _root; //规模、根节点
	  virtual int updateHeight(BinNodePosi(T) x); //更新节点x的高度
	  void updateHeightAbove(BinNodePosi(T) x); //更新节点x及其祖先的高度
     public:
	  BinTree() : _size(0), _root(NULL) { } //构造函数
	  ~BinTree() { if (0 < _size) remove(_root); } //析构函数
	  int size() const { return _size; } //规模
	  bool empty() const { return !_root; } //判空
	  BinNodePosi(T) root() const { return _root; } //树根
	  BinNodePosi(T) insertAsRoot(T const& e); //插入根节点
	  BinNodePosi(T) insertAsLC(BinNodePosi(T),T const& e ); //插入左孩子
	  BinNodePosi(T) insertAsRC(BinNodePosi(T),T const& e); //插入右孩子
	  BinNodePosi(T) attachAsLC(BinNodePosi(T),BinTree<T> *&T); //接入左子树
	  BinNodePosi(T) attachAsRC(BinNodePosi(T),BinTree<
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值