不知道哪里出了错,报错 if else没配对,空的执行语句,错误指向removeAt里面
#ifndef __BST_H__
#define __BST_H__
#include "BinTree.h"
using namespace std;
//词条
template<typename K,typename V> struct Entry{
K key;
V value;
Entry(K k=K(),V v=V()):key(k),value(e){};
bool operator<(Entry<K,V> const& e){return key<e.key;}
bool operator>(Entry<K,V> const& e){return key>e.key;}
bool operator==(Entry<K,V> const& e){return key==e.key;}
bool operator!=(Entry<K,V> const& e){return key!=e.key;}
};
template <typename T>
class BST:public BinTree<T>{
protected:
BinNodePosi(T) _hot;
public:
virtual BinNodePosi(T) & search(const T& e);//查找
virtual BinNodePosi(T) insert(const T& e);//插入
virtual bool remove(const T& e);//删除
};
//查找,等于或假想节点
template <typename T>
BinNodePosi(T)& BST<T>::search(const T& e)
{return searchIn(_root,e,_hot=NULL);}
template <typename T>
static BinNodePosi(T) & searchIn(BinNodePosi(T) & v,const T& e,BinNodePosi(T) & hot){
if(!v||(e==v->data))
return v;
hot=v;
return searchIn(((e<v->data)?v->lc:v->rc),e,hot);
}
//插入
template<typename T>
BinNodePosi(T) BST<T>::insert(const T& e)
{
BinNodePosi(T) & x=search(e);//连孩子
if(x) return x;
x=new BinNode<T>(e,_hot);//连父亲
updateHeightAbove(x);
return x;
}
//删除
template <typename T>
bool BST<T>::remove(const T &e)
{
BinNodePosi(T) & x=search(e);
if(!x) return false;
removeAt(x,_hot);
_size--;
updateHeightAbove(_hot);
return true;
}
template<typename T>
void release(T const& e){e=-1;}
template <typename T>
void release(BinNodePosi(T) x){delete x;}
template <typename T>
static BinNodePosi(T) removeAt(BinNodePosi(T) & x,BinNodePosi(T) & hot){
BinNodePosi(T) w=x;
BinNodePosi(T) succ=NULL;
if(!HasLChild(*x))//覆盖
succ=x=x->rc;
else if(!HasRChild(*x))
succ=x=x->lc;
else {
w=w->succ();
swap(x->data,w->data);
BinNodePosi(T) u=w->parent;
((u==x)?u->rc:u->lc)=succ=w->rc;//右孩子覆盖,中序遍历下
}
//记录
hot=w->parent;
if(succ)
succ->parent=hot;
//释放
release(w->data);
release(w);
return succ;
}
#endif
BinTree
#ifndef __BinTree_H__
#define __BinTree_H__
#include "BinNode.h"
#include"Stack.h"
#include"Vector.h"
#include<algorithm>
template <typename T>
class BinTree{
protected:
int _size;
BinNodePosi(T) _root;
virtual int updateHeight(BinNodePosi(T) x);
void updateHeightAbove(BinNodePosi(T) 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(){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);
int remove(BinNodePosi(T) x);
//遍历
template <typename VST>
void travPre1(VST& visit){if(_root) _root->travPre_R(this,visit);}
template <typename VST>
void travIn1(VST& visit){if(_root) _root->travIn_R(this,visit);}
template <typename VST>
void travPost1(VST& visit){if(_root) _root->travPost_R(this,visit);}
template <typename VST>
void travPre2(VST& visit){if(_root) _root->travPre_I2(this,visit);}
template <typename VST>
void travIn2(VST& visit){if(_root) _root->travIn_I1(this,visit);}
//比较器
bool operator<(BinTree<T> const &t)
{return _root&&t._root&&It(_root,t.root);}
bool operator>(BinTree<T> const &t)
{return _root&&t._root&&!It(_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);}
};
//更新高度
template <typename T>
int BinTree<T>::updateHeight(BinNodePosi(T) x)
{
return x->height=1+max(stature(x->lc),stature(x->rc));
}
template<typename T>
void BinTree<T>::updateHeightAbove(BinNodePosi(T) x)
{
while(x)
{
updateHeight(x);
x->parent;
}
}
//节点插入
//根
template <typename T>
BinNodePosi(T) BinTree<T>::insertAsRoot(T const& e)
{
_size=1;
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->lc;
}
//右孩子
template <typename T>
BinNodePosi(T) BinTree<T>::insertAsRC(BinNodePosi(T) x,T const& e)
{
_size++;
x->insertAsRC(e);
updateHeightAbove(x);
return x->lc;
}
//递归版先序
template<typename T,typename VST>
void traPre_R(BinNodePosi(T) x,VST& visit)
{
if(!x) return;
visit(x->data);
travPre_R(x->lc,visit);
travPre_R(x->rc,visit);
}
//后
template <typename T,typename VST>
void travPost_R(BinNodePosi(T) x,VST& visit)
{
if(!x) return;
travPost_R(x->lc,visit);
travPost_R(x->rc,visit);
visit(x->data);
}
//中
template <typename T,typename VST>
void travIn_R(BinNodePosi(T) x,VST& visit)
{
if(!x) return;
travPost_R(x->lc,visit);
visit(x->data);
travPost_R(x->rc,visit);
}
//迭代版先序
template <typename T,typename VST>
static void visitAlongLeftBranch(BinNodePosi(T) x,VST& visit,Stack<BinNodePosi(T)> &S){
while(x)
{
visit(x->data);
S.push(x->rc);//右孩子入栈
x=x->lc;
}
}
template<typename T,typename VST>
void travPre_I2(BinNodePosi(T) x,VST& visit)
{
Stack<BinNodePosi(T)> S;
while(true)
{
visitAlongLeftBranch(x,visit,S);
if(S.empty())
break;
x=S.pop();//弹出下一起点
}
}
//迭代版中序
template<typename T>
static void goAlongLeftBranch(BinNodePosi(T) x,Stack<BinNodePosi(T)>& S)
{
while(x)
{
S.push(x);
x=x->lc;
}
}
template<typename T,typename VST>
void travIn_I1(BinNodePosi(T) x,VST& visit)
{
Stack<BinNodePosi(T)> S;
while(true)
{
goAlongLeftBranch(x,S);
if(S.empty())
break;
x=S.pop();
visit(x->data);
x=x->rc;
}
}
//后继(中序)
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;
}
return s;
}
#endif __BinTree_H__
BinNode:
#ifndef __BinNode_H__
#define __BinNode_H__
#include<iostream>
using namespace std;
#define BinNodePosi(T) BinNode<T>*
#define stature(p)((p)?(p)->height:-1)//节点高度(空树高度为-1)
typedef enum{RB_RED,RB_BLACK} RBColor;//节点颜色
//*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));
#define sibling(p) (IsLChild(*(p)) ? (p)->parent->rc:(p)->paren->lc);//兄弟
#define uncle(x) (IsLChild(*((x)->parent)) ? (x)->parent->parent->rc:(x)->parent->parent->lc);//叔叔
#define FromParentTo(x)(IsRoot(x)?_root:(IsLChild(x)?(x).parent->lc:(x).parent->rc))//父亲的引用
template <typename T> struct BinNode{
T data;
BinNodePosi(T) parent;
BinNodePosi(T) lc;
BinNodePosi(T) rc;
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 & e);
BinNodePosi(T) insertAsRC(T const & e);
BinNodePosi(T) succ();
template <typename VST> void travPre(VST& visit);//先
template <typename VST> void traIn(VST& visit);
template <typename VST> void traPost(VST& visit);
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> BinNodePosi(T) BinNode<T>::insertAsLC(T const &e)
{
return lc=new BinNode(e,this);
}
//右孩子插入
template<typename T> BinNodePosi(T) BinNode<T>::insertAsRC(T const &e)
{
return rc=new BinNode(e,this);
}
#endif