定义一个类模板:
Template < class或者也可以用typename T >
class类名{
//类定义......
};
说明:其中,template是声明各模板的关键字,表示声明一个模板,模板参数可以是一个,也可以是多个。
例如
template <class Type>
class stack_class
{
private:
int max_len;//空间大小
int len;//当前元素个数
Type *bottom;//头指针
Type *top;//栈顶的指针
public:
stack_class();
stack_class(stack_class<Type> & arg);
~stack_class();
bool IsEmpty();//是否为空,是返回1
void Push(const Type & arg);//栈插入元素
Type Pop();
};
template <class Key ,class Type>
class rbtree_class{
private:
node<Key,Type> *root;
node<Key,Type> *nil;
int size;
public:
rbtree_class();
void NodeCpy(node<Key,Type>* &,node<Key,Type>* &,node<Key,Type>* & );
rbtree_class(rbtree_class<Key,Type> &);
void NodeDel(node<Key,Type> * );
~rbtree_class();
//首先定义左旋转与右旋转 左旋把节点的右孩子(存在)转到它位置,自己变成其左孩子,右旋相反,后面分析要用到这两个操作
void LeftRotate(node<Key,Type> *);
void RightRotate(node<Key,Type> *);
//然后定义fixup维持红黑树的性质
void RbInsertFixup(node<Key,Type> * &);
bool Insert(const Key &,const Type &);
void Show(node<Key,Type> *);
void ShowAll();//输出所有元素(按续)与颜色
node<Key,Type> * MinNode(node<Key,Type> *);//从某节点开始向下找最小Key值的节点
node<Key,Type> * Min();
node<Key,Type> * Replace(node<Key,Type> * des,node<Key,Type> * src);//src带上自己的子树替代des,父母节点相关的替代,
node<Key,Type> * SearchNode(node<Key,Type> *,const Key &);
node<Key,Type> * Search(const Key &);//搜寻key,返回这个key的节点的指针 不存在返回nil
void RbDeleteFixup(node<Key,Type> * &);
bool Delete(const Key&);
};