AVL树递归实现
递归在算法上比较容易实现,但是由于函数调用要额外开辟和释放系统栈空间,所以效率没有非递归方式高,但是非递归方式实现起来比较困难。
如果对大量数据建立AVL树,且要求查找效率的话,还是用非递归方式---循环。
说明:程序运行后,列出节点元素前的“=”符号表示节点的深度。“*”表示该节点是根节点。
//============================================================================
// Name : AVL_BTree.cpp
// Author : dxp
// Version : 1.0
// Copyright : hpu
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
class AVL_Tree
{
typedef int KEY;
private:
struct TNode
{
TNode * left;
TNode * right;
int height;
KEY key;
};
TNode * Troot;
public:
AVL_Tree();
void Del_tree();
void Del_tree_elem(TNode*&);
void SingleLeft_Rotate(TNode * &);
void SingleRight_Rotate(TNode * &);
void DoubleLeft_Rotate(TNode * &);
void DoubleRight_Rotate(TNode * &);
void Visit(TNode *,int);
void Visit_elem( TNode * ,int);
void MakeEmpty();
void Find_elem(KEY,TNode * &,TNode*&);
void Insert_elem(KEY,TNode* &);
void Delete_elem(KEY , TNode *&);
void Delete(KEY);
TNode* Findmax_elem(TNode *);
TNode* Findmin_elem(TNode *);
void FindMin();
void FindMax();
void List();
void Insert(KEY);
int GetHeight(TNode* &);
int MAX_height(TNode* &);
void Find(KEY);
TNode* Getroot()
{
return Troot;
}
~AVL_Tree()
{
Del_tree();
}
};
#define SINGLE_LEFT
int main()
{
AVL_Tree tree;
for (int i=1;i<=100;i++)
tree.Insert(i);
tree.List();
tree.Find(56);
tree.FindMin();
tree.FindMax();
return 0;
}
AVL_Tree::AVL_Tree()
{
Troot=NULL;
}
int AVL_Tree:: MAX_height(TNode* & root)
{
if (GetHeight(root->left)>GetHeight( root->right))
return GetHeight(root->left);
else
return GetHeight(root->right);
}
void AVL_Tree::SingleLeft_Rotate(TNode * & root)
{
TNode* tmp=root->left;
root->left=tmp->right;
tmp->right=root;
root=tmp;
root->right->height=MAX_height(root->right)+1;
root->height=MAX_height(root)+1;
}
void AVL_Tree::SingleRight_Rotate(TNode * & root)
{
TNode* tmp=root->right;
root->right=tmp->left;
tmp->left=root;
root=tmp;
root->left->height=MAX_height(root->left)+1;
root->height=MAX_height(root)+1;
}
void AVL_Tree::DoubleLeft_Rotate(TNode *& root)
{
SingleRight_Rotate(root->left);
SingleLeft_Rotate(root);
}
void AVL_Tree::DoubleRight_Rotate(TNode *& root)
{
}
int AVL_Tree::GetHeight(TNode * &root)
{
if (root==NULL) return -1;
else return root->height;
}
void AVL_Tree::Insert_elem(KEY Ckey,TNode* & root)
{
if (root==NULL)
{
root=new TNode;
if (root==NULL)
{
cout<<"have no memory to used!"<<endl;
exit(1);
}
else
{
root->left=NULL;
root->right=NULL;
root->height=0;
root->key=Ckey;
}
}
else
if (Ckey< root->key)
{
AVL_Tree::Insert_elem(Ckey,root->left);
#ifdef SINGLE_LEFT
if (GetHeight(root->left) - GetHeight(root->right)==2)
if (Ckey<root->left->key)
SingleLeft_Rotate(root);
else
DoubleLeft_Rotate(root);
#endif
}
else
if (root->key<Ckey)
{
AVL_Tree::Insert_elem(Ckey,root->right);
if (GetHeight(root->left) - GetHeight(root->right)==-2)
if (Ckey > root->right->key)
SingleRight_Rotate(root);
else
DoubleRight_Rotate(root);
}
root->height=MAX_height(root)+1;
}
void AVL_Tree:: Visit_elem(TNode* cp,int lay)
{
if (cp!=NULL)
{
if (lay==1) printf("*");
else
printf(" ");
for (int i=0;i<lay;i++)
printf("=");
printf(" %2d:%d\n", cp->key,cp->height);
}
}
void AVL_Tree::Visit(TNode* p,int i)
{
i++;
if (p==NULL)
{
return;
}
Visit_elem(p,i);
AVL_Tree::Visit(p->left,i);
AVL_Tree::Visit(p->right,i);
}
void AVL_Tree::List()
{
if (Troot==NULL)
cout<<"List failed: the AVL_tree not exist!"<<endl;
Visit(Troot,0);
}
void AVL_Tree::Insert(KEY key)
{
AVL_Tree::Insert_elem(key,Troot);
}
void AVL_Tree::Find_elem(KEY Ckey,TNode* &root,TNode* & P_key)
{
if (root==NULL)
{
return ;
}
if (root->key == Ckey)
{
P_key= root;
return ;
}
AVL_Tree::Find_elem(Ckey,root->left,P_key);
AVL_Tree::Find_elem(Ckey,root->right,P_key);
}
void AVL_Tree::Find(KEY Ckey)
{
TNode * p=NULL;
AVL_Tree::Find_elem(Ckey,Troot,p);
if (p!=NULL)
cout<<"Find the element: "<< p->key<<":"<<p->height<<endl;
else
{
cout<<"Find failed: Can't find the element: " <<Ckey<<endl;
}
}
AVL_Tree::TNode* AVL_Tree::Findmin_elem(TNode *root)
{
if (root==NULL) return NULL;
while (root->left)
{
root=root->left;
}
return root;
}
void AVL_Tree::FindMin()
{
TNode * p=AVL_Tree::Findmin_elem(Troot);
if (p==NULL)
cout<<"this is the min element!"<<endl;
else
cout<<"the min element is: "<<p->key<<endl;
}
AVL_Tree::TNode* AVL_Tree::Findmax_elem(TNode *root)
{
while (root->right)
{
root=root->right;
}
return root;
}
void AVL_Tree::FindMax()
{
TNode * p=AVL_Tree::Findmax_elem(Troot);
if (p==NULL)
cout<<"this is the max element!"<<endl;
else
cout<<"the max element is: "<<p->key<<endl;
}
void AVL_Tree::Delete_elem( KEY Ckey,TNode *& root)
{
if (root==NULL)
{
// cout<<"can't find the element: "<<endl;
return;
}
else
{
if (Ckey< root->key) AVL_Tree:: Delete_elem(Ckey,root->left);
else if (Ckey > root->key ) AVL_Tree::Delete_elem(Ckey, root->right);
else
{
if (root->left && root->right)
{
TNode * P_min=Findmin_elem(root->right);
root->key=P_min->key;
AVL_Tree::Delete_elem(Ckey,root->right);
}
else
{
TNode* tmp=root;
if (root->left==NULL) root=root->right;
else if (root->right==NULL) root=root->left;
delete tmp;
}
}
}
}
void AVL_Tree::Delete(KEY Ckey)
{
TNode * P_key=NULL;
AVL_Tree::Find_elem(Ckey,Troot,P_key);
if (P_key==NULL)
cout<<"Delete faild: Can't find the element: "<<Ckey<<endl;
else AVL_Tree::Delete_elem(Ckey,Troot);
}
void AVL_Tree::Del_tree_elem(TNode*& root)
{
if (root==NULL)
return ;
AVL_Tree::Del_tree_elem(root->right);
AVL_Tree::Del_tree_elem(root->left);
delete root;
root=NULL;
}
void AVL_Tree::Del_tree()
{
AVL_Tree::Del_tree_elem(Troot);
}