BinTree::构造、析构、插入、删除

来源:互联网 发布:室内灯光设计 知乎 编辑:程序博客网 时间:2024/06/04 17:53

构造析构:

   BinTree() : _size(0), _root(NULL) { } //构造函数   ~BinTree() { if (0 < _size) remove(_root); } //析构函数

插入:

   BinNodePosi(T) insertAsRoot(T const & e); //插入根节点   BinNodePosi(T) insertAsLC(BinNodePosi(T) x, T const & e); //e作为x的左孩子(原无)插入   BinNodePosi(T) insertAsRC(BinNodePosi(T) x, T const & e); //e作为x的右孩子(原无)插入   BinNodePosi(T) attachAsLC(BinNodePosi(T) x, BinTree<T>* &T); //T作为x左子树接入   BinNodePosi(T) attachAsRC(BinNodePosi(T) x, BinTree<T>* &T); //T作为x右子树接入

插入根节点:

template <typename T> BinNodePosi(T) BinTree<T>::insertAsRoot(T const& e){ _size = 1; return _root = new BinNode<T>(e); } //将e当作根节点插入空的二叉树

插入左右孩子:

template <typename T> //将e作为当前节点的左孩子插入二叉树BinNodePosi(T) BinNode<T>::insertAsLC(T const & e) {  return lChild = new BinNode(e, this);  }template <typename T> //将e作为当前节点的右孩子插入二叉树BinNodePosi(T) BinNode<T>::insertAsRC(T const & e) {  return rChild = new BinNode(e, this);  }#define stature(p) ((p) ? (p)->height : -1) //节点高度(与“空树高度为-1”的约定相统一)template <typename T> int BinTree<T>::updateHeight(BinNodePosi(T) x) //更新节点x高度{ return x->height = 1 + max(stature(x->lChild), stature(x->rChild)); } //具体规则因树不同而异template <typename T> void BinTree<T>::updateHeightAbove(BinNodePosi(T) x) //更新v及祖先的高度{ while (x) { updateHeight(x); x = x->parent; } } //可优化:一旦高度未变,即可终止template <typename T> BinNodePosi(T) BinTree<T>::insertAsLC(BinNodePosi(T) x, T const& e){ _size++; x->insertAsLC(e); updateHeightAbove(x); return x->lChild; } //e插入为x的左孩子template <typename T> BinNodePosi(T) BinTree<T>::insertAsRC(BinNodePosi(T) x, T const& e){ _size++; x->insertAsRC(e); updateHeightAbove(x); return x->rChild; } //e插入为x的右孩子

插入左右子树:

template <typename T> //二叉树子树接入算法:将S当作节点x的左子树接入,S本身置空BinNodePosi(T) BinTree<T>::attachAsLC(BinNodePosi(T) x, BinTree<T>* &S) { //x->lChild == NULL   if (x->lChild = S->_root) x->lChild->parent = x; //接入   _size += S->_size; updateHeightAbove(x); //更新全树规模与x所有祖先的高度   S->_root = NULL; S->_size = 0; release(S); S = NULL; return x; //释放原树,返回接入位置}template <typename T> //二叉树子树接入算法:将S当作节点x的右子树接入,S本身置空BinNodePosi(T) BinTree<T>::attachAsRC(BinNodePosi(T) x, BinTree<T>* &S) { //x->rChild == NULL   if (x->rChild = S->_root) x->rChild->parent = x; //接入   _size += S->_size; updateHeightAbove(x); //更新全树规模与x所有祖先的高度   S->_root = NULL; S->_size = 0; release(S); S = NULL; return x; //释放原树,返回接入位置}

删除以结点为根的子树:

#define FromParentTo(x) ( \   IsRoot(x) ? _root : ( \   IsLChild(x) ? (x).parent->lChild : (x).parent->rChild \   ) \) //来自父亲的指针template <typename T> //删除二叉树中位置x处的节点及其后代,返回被删除节点的数值int BinTree<T>::remove(BinNodePosi(T) x) { //assert: x为二叉树中的合法位置   FromParentTo(*x) = NULL; //切断来自父节点的指针   updateHeightAbove(x->parent); //更新祖先高度   int n = removeAt(x); _size -= n; return n; //删除子树x,更新规模,返回删除节点总数}template <typename T> //删除二叉树中位置x处的节点及其后代,返回被删除节点的数值static int removeAt(BinNodePosi(T) x) { //assert: x为二叉树中的合法位置   if (!x) return 0; //递归基:空树   int n = 1 + removeAt(x->lChild) + removeAt(x->rChild); //递归释放左、右子树   release(x->data); release(x); return n; //释放被摘除节点,并返回删除节点总数}

子树提出:

template <typename T> //二叉树子树分离算法:将子树x从当前树中摘除,将其封装为一棵独立子树返回BinTree<T>* BinTree<T>::secede(BinNodePosi(T) x) { //assert: x为二叉树中的合法位置   FromParentTo(*x) = NULL; //切断来自父节点的指针   updateHeightAbove(x->parent); //更新原树中所有祖先的高度   BinTree<T>* S = new BinTree<T>; S->_root = x; x->parent = NULL; //新树以x为根   S->_size = x->size(); _size -= S->_size; return S; //更新规模,返回分离出来的子树}
0 0
原创粉丝点击