header.h template<class type> class AATree { private: struct Node { type data; Node *left; Node *right; int level; Node(const type&d,Node *lt=NULL,Node *rt=NULL,int l=1){data=d;left=lt;right=rt;level=l;} }; Node *root; public: AATree(){root=NULL;} ~AATree(){makeEmpty(root);} bool find(const type&x){return find(x,root);} void insert(const type&x){insert(x,root);} void remove(const type&x){remove(x,root);} private: bool find(const type&x,Node *t); void insert(const type&x,Node *t); void remove(const type&x,Node *t); void LL(Node *t); void RR(Node *t); void makeEmpty(Node *&t); }; //================================== template<class type> bool AATree<type>::find(const type&x,Node *t) { if(t==NULL) return false; else if(x>t->data) return find(x,t->right); else if(x<t->data) return find(x,t->left); else return true; } //================================== template<class type> void AATree<type>::insert(const type&x,Node *t) { if(t==NULL) t=new Node(x); else if(x>t->data) insert(x,t->right); else if(x<t->data) insert(x,t->left); else return; LL(t); RR(t); } //================================== template<class type> void AATree<type>::remove(const type&x,Node *t) { if(t==NULL) return; else if(x>t->data) remove(x,t->right); else if(x<t->data) remove(x,t->left); else if(t->left!=NULL && t->right!=NULL) { Node *tt=t->right; while(tt->left!=NULL) tt=tt->left; t->data=tt->data; remove(t->data,t->right); } else { Node *old=t; t=(t->left!=NULL)?t->left:t->right; delete old; return; } LL(t); if(t->right!=NULL) RR(t->right); if(t->right!=NULL && t->right->right!=NULL) LL(t->right->right); RR(t); if(t->right!=NULL) RR(t->right); } //================================== template<class type> void AATree<type>::LL(Node *t) { if(t->left!=NULL && t->level==t->left->left->level) { Node *tt=t->left; t->left=tt->right; tt->right=t; t=tt; } } //================================== template<class type> void AATree<type>::RR(Node *t) { if(t->right!=NULL && t->right->right!=NULL && t->right->level==t->level && t->right->right->level==t->level) { Node *tt=t->right; t->right=tt->left; tt->left=t; t=tt; t->level++; } } //================================== template<class type> void AATree<type>::makeEmpty(Node *&t) { if(t!=NULL) { makeEmpty(t->left); makeEmpty(t->right); delete t; } t=NULL; } AA.cpp #include <iostream> #include "header.h" using namespace std; int main() { int i=0; int a[]={10,8,6,21,87,56,4,0,11,3,22,7,5,34,1,2,9}; AATree<int> AAT; int len=sizeof(a)/sizeof(int); for(i=0;i<len;i++) AAT.insert(a[i]); cout<<"find 2 is "<<(AAT.find(2)?"true":"false")<<endl; AAT.remove(2); cout<<"after remove 2,find 2 is "<<(AAT.find(2)?"true":"false")<<endl; cout<<"find 3 is "<<(AAT.find(3)?"true":"false")<<endl; AAT.remove(3); cout<<"after remove 3,find 3 is "<<(AAT.find(2)?"true":"false")<<endl; cout<<"find 56 is "<<(AAT.find(56)?"true":"false")<<endl; AAT.remove(56); cout<<"after remove 56,find 56 is "<<(AAT.find(2)?"true":"false")<<endl; cout<<"find 34 is "<<(AAT.find(34)?"true":"false")<<endl; AAT.remove(34); cout<<"after remove 34,find 34 is "<<(AAT.find(2)?"true":"false")<<endl; cout<<"find 87 is "<<(AAT.find(87)?"true":"false")<<endl; AAT.remove(87); cout<<"after remove 87,find 87 is "<<(AAT.find(2)?"true":"false")<<endl; return 0; }