课程笔记 13:数据结构(清华) 二叉树

本文介绍了二叉树模板类的实现,包括构造、析构函数,以及插入、删除、遍历等重要接口。详细讲解了如何更新节点高度、插入节点作为左右孩子以及子树的接入和删除操作。此外,还涵盖了子树的分离和比较操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在二叉树节点模板类的基础上,可以进一步定义二叉树模板类。

#include "BinNode.h" //引用二叉树结点模板类

template <typename T> class BinTree

{

pretected:

int _size; //规模

BinNodePosi(T) _root; //根节点

virtual int updateHeight(BinNodePosi(T) x); //更新当前节点高度

void updateHeightAbove(BinNodePosi(T) x); //更新所有祖先节点高度

public:

/*--构造与析构函数--*/

BinTree(): _size(0), _root(NULL) {}

~BinTree() { if (_size > 0) 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) x, T const &e); //作为左孩子节点插入

BinNodePosi(T) insertAsRC(BinNodePosi(T) x, T const &e); //作为右孩子节点插入

BinNodePosi(T) attachaAsLC(BinNodePosi(T) x, BinTree <T>* &S); //作为左子树插入

BinNodePosi(T) attachaAsRC(BinNodePosi(T) x, BinTree <T>* &S); //作为右子树插入

int remove(BinNodePosi(T) x); //删除节点

BinTree<T>* secede(BinNodePosi(T) x); //删除子树

/*--遍历接口--*/

template <typename VST> void travLevel(VST&visit) //层次遍历

{ if (_root) _root.travLevel(visit); }

template <typename VST> void travPre(VST&visit) //先序遍历

{ if (_root) _root.travPre(visit); }

template <typename VST> void travIn(VST&visit) //中序遍历

{ if (_root) _root.travIn(visit); }

template <typename VST> void travPost(VST&visit) //后序遍历

{ if (_root) _root.travPost(visit); }

/*--判断器、比较器--*/

bool operator>(BinTree<T> const& t)

{ return _root && t._root && lt(_root, t._root); }

bool operator<(BinTree<T> const& t)

{ return _root && t._root && lt(_root, t._root); }

bool operator==(BinTree<T> const& t) 

{ return _root && t._root && (_root == t._root); }

bool operator>=(BinTree<T> const& t) 

{ return _root && t._root && (_root >= t._root); }

bool operator<=(BinTree<T> const& t) 

{ return _root && t._root && (_root <= t._root); }

}


以下将对部分重要接口的实现作详细说明。

1.高度更新

template <typename T> int BinTree::updateHeight(BinNodePosi(T) x)

{ return x.height = max(stature(x.lchild), stature(x.rchild)) + 1; }

template <typename T> int BinTree::updateHeight(BinNodePosi(T) x)

{ while (x) { updateHeight(x); x = x.parent; } }

2.节点插入

template <typename T> BinNodePosi(T) BinTree<T>::insertAsRoot(T const &e)

{ _size ++; return _root = new BinNode<T>(e); }

template <typename T> BinNodePosi(T) BinTree<T>::insertAsLC(BinNodePosi(T) x, T const &e)

{ _size ++; x.insertAsLC(e); updateHeightAbove(x); return x.lchild; }

template <typename T> BinNodePosi(T) BinTree<T>::insertAsRC(BinNodePosi(T) x, T const &e)

{ _size ++; x.insertAsRC(e); updateHeightAbove(x); return x.rchild; }

3.子树接入

template <typename T>

BinNodePosi(T) attachaAsLC(BinNodePosi(T) x, BinTree <T>* &S)

{

if ( x.lchild = S.root ) x.lchild.parent = x;

_size += S._size; updateHeightAbove(x);

S.root = NULL; S.size = 0; release(S); S = NULL;

return x;

}

BinNodePosi(T) attachaAsRC(BinNodePosi(T) x, BinTree <T>* &S)

{

if ( x.rchild = S.root ) x.rchild.parent = x;

_size += S._size; updateHeightAbove(x);

S.root = NULL; S.size = 0; release(S); S = NULL;

return x;

}

4.子树删除

template <typename T>

int BinTree<T>::remove(BinNodePosi(T) x)

{

IsLChild(x) ? x.parent.lChild = NULL: x.parent.rChild = NULL;

updateHeightAbove(x.parent);

int n = removeAt(x); return n;

}

template <typename T>

int removeAt(BinNodePosi(T) x)

{

if (x) return 0;

int n = 1 + removeAt(x.lChild) +  removeAt(x.rChild);

release(x); 

return n;

}

5.子树分离

template <typename T>

BinTree<T>* BinTree::secede(BinNodePosi(T) x)

{

IsLChild(x) ? x.parent.lChild = NULL: x.parent.rChild = NULL;

updateHeightAbove(x.parent);

BinTree<T>* S = new BinTree<T>;

S.root = x;

x.parent = NULL;

S._size = x._size;

return S;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值