注意模板类,声明和定义在同一个文件里Tree.h
/***********avl**********//
//Tree.h//
#ifndef AVL_BLOCK
#define AVL_BLOCK
template<typename T>
class Treenode{
public:
T val;
int height;
int freq;
Treenode*lson;
Treenode*rson;
Treenode(int h=0):lson(NULL),rson(NULL),height(h),freq(1){}
};//节点的声明
//下面是AVL类的声明
template<typename T>
class AVLtree{
private:
Treenode<T>*root;
void insert(Treenode<T>*&r,T x);
Treenode<T>*find(Treenode<T>*r,T x);
void deletenode(Treenode<T>*&r,T x);
void inorder(Treenode<T>*r);
int get_height(Treenode<T>*r);
void singleRotaLeft(Treenode<T>*&r);
void singleRotaRight(Treenode<T>*&r);
void DoubleRotaLeft(Treenode<T>*&r);
void DoubleRotaRight(Treenode<T>*&r);
void destroy(Treenode<T>*&r);
public:
AVLtree():root(NULL){}
int Get_height();//求高度的接口
void Insert(T x);//插入接口
Treenode<T>*find(T x);//查找接口
void Delete(T x);//删除接口
void travel();//遍历接口
void Destroy();//
};
//AVL的定义,带模板的必须与声明在同一个文件
//************AVL*******************//
template<typename T>
int AVLtree<T>::get_height(Treenode<T>*r){
if(r){return r->height;}
return -1;
}
template<typename T>
int AVLtree<T>::Get_height(){
return get_height(root);
}
template<typename T>
void AVLtree<T>::singleRotaLeft(Treenode<T>*&r){
Treenode<T>*n=r->lson;
r->lson=n->rson;
n->rson=r;
r->height=max(get_height(r->lson),get_height(r->rson))+1;
n->height=max(get_height(n->lson),r->height)+1;
r=n;
}
template<typename T>
void AVLtree<T>::singleRotaRight(Treenode<T>*&r){
Treenode<T>*n=r->rson;
r->rson=n->lson;
n->lson=r;
r->height=max(get_height(r->lson),get_height(r->rson))+1;
n->height=max(get_height(n->lson),r->height)+1;
r=n;
}
template<typename T>
void AVLtree<T>::DoubleRotaLeft(Treenode<T>*&r){
singleRotaRight(r->lson);
singleRotaLeft(r);
}
template<typename T>
void AVLtree<T>::DoubleRotaRight(Treenode<T>*&r){
singleRotaLeft(r->rson);
singleRotaRight(r);
}
template<typename T>
void AVLtree<T>::insert(Treenode<T>*&r,T x){
if(r==NULL){r=new Treenode<T>();r->val=x;return;}
if(r->val>x){
insert(r->lson,x);
if(get_height(r->lson)-get_height(r->rson)==2)
{if(x<r->lson->val)singleRotaLeft(r);
else DoubleRotaLeft(r);
}
}//左子树
else if(r->val<x){
insert(r->rson,x);
if(get_height(r->rson)-get_height(r->lson)==2)
{if(x>r->rson->val)singleRotaRight(r);
else DoubleRotaRight(r);
}
}//右子树
else ++(r->freq);
r->height=max(get_height(r->lson),get_height(r->rson))+1;
}
template<typename T>
void AVLtree<T>::Insert(T x){
insert(root,x);
}
template<typename T>
Treenode<T>*AVLtree<T>::find(Treenode<T>*r,T x){
if(r==NULL)return NULL;
if(r->val>x)return find(r->lson,x);
else if(r->val<x)return find(r->rson,x);
else return r;
}
template<typename T>
Treenode<T>* AVLtree<T>::find(T x){
return find(root,x);
}
template<typename T>
void AVLtree<T>::deletenode(Treenode<T>*&r,T x){
if(r==NULL)return;
if(x<r->val){
deletenode(r->lson,x);
if(get_height(r->rson)-get_height(r->lson)==2)
{if(r->rson->lson&&get_height(r->rson->lson)>get_height(r->rson->rson))DoubleRotaRight(r);
else singleRotaRight(r);
}
}
else if(x>r->val){
deletenode(r->rson,x);
if(get_height(r->lson)-get_height(r->rson)==2)
{
if(r->lson->rson&&get_height(r->lson->rson)>get_height(r->lson->lson))DoubleRotaLeft(r);
else singleRotaLeft(r);
}
}
else{
Treenode<T>*tmp=NULL;
if(r->lson&&r->rson){tmp=r->rson;while(tmp->lson){tmp=tmp->lson;}
r->val=tmp->val;r->freq=tmp->freq;deletenode(r->rson,tmp->val);
if(get_height(r->lson)-get_height(r->rson)==2){
if(r->lson->rson&&get_height(r->lson->rson)>get_height(r->lson->lson))DoubleRotaLeft(r);
else singleRotaLeft(r);
}
}
else if(r->lson==NULL&&r->rson==NULL){
delete(r);
r=NULL;//避免指针成为空悬指针
}
else{tmp=r;
if(r->lson==NULL)r=r->rson;
else r=r->lson;
delete(tmp);tmp=NULL;
}
}
if(r==NULL)return;
r->height=max(get_height(r->lson),get_height(r->rson))+1;
}
template<typename T>
void AVLtree<T>::Delete(T x){
deletenode(root,x);
}
template<typename T>
void AVLtree<T>::inorder(Treenode<T>*r){
if(r==NULL)return;
inorder(r->lson);
cout<<r->val<<" ";
inorder(r->rson);
}
template<typename T>
void AVLtree<T>::travel(){
inorder(root);
}
template<typename T>
void AVLtree<T>::destroy(Treenode<T>*&r){
if(r==NULL)return;
destroy(r->lson);
destroy(r->rson);
free(r);
}
template<typename T>
void AVLtree<T>::Destroy(){
destroy(root);
}
#endif//AVL
测试程序:
include<iostream>
#include<stdlib.h>
#include<string.h>
#include"Tree.h"
//main.cpp
static int arr[]={
3,2,1,4,5,6,7,16,15,14,13,12,11,10,8,9};
using namespace std;
int main(){
int i;
AVLtree<int>*tree=new AVLtree<int>();
int len=sizeof(arr)/sizeof(int);
cout<<"依次添加:"<<endl;
for(i=0;i<len;i++){
cout<<arr[i]<<" ";
tree->Insert(arr[i]);
}
cout<<"中序遍历:"<<endl;
tree->travel();
i=8;
cout<<"删除节点i=8"<<endl;
tree->Delete(i);
cout<<"删除之后中序遍历"<<endl;
tree->travel();
tree->Destroy();
cout<<endl;
}