刘收获

导航

AVLTree 平衡树

//测试数据
//第一组:7个输入,测试LL型,40,36,44,32,38,28,24;
//第二组:7个输入,测试RR型,40,36,44,43,48,52,56;
//第三组:7个输入,测试LR型,40,36,44,32,37,38,39;
//第四组:7个输入,测试RL型,40,36,44,48,43,42,41;

#include<iostream>
#define Element int
using namespace std;

typedef struct AVLNode
{
Element data;
int height;
AVLNode* LeftChild,*RightChild;
AVLNode():data(0),height(0),LeftChild(NULL),RightChild(NULL){
}
AVLNode(Element e,int heig,AVLNode* L,AVLNode* R):data(e),height(heig),LeftChild(L),RightChild(R){
}
} AVLNode;

int MaxHeight(int a,int b)
{
return a>b?a:b;
}

int GetHeight(AVLNode* &Node)
{
if(Node == NULL) return -1;
else return Node->height = MaxHeight(GetHeight(Node->LeftChild),GetHeight(Node->RightChild)) + 1;
}

void InorderWalk(AVLNode* Node)
{
if(Node)
{
InorderWalk(Node->LeftChild);
cout<<Node->data<<"("<<Node->height<<") ";
InorderWalk(Node->RightChild);
}
}

AVLNode* Serach(AVLNode* root,Element key)
{
AVLNode* p = root;
while(p && p->data != key)
{
if(key < p->data)
p = p->LeftChild;
else
p = p->RightChild;
}
return p;
}

void LLRotation(AVLNode* &Node)
{
cout<<"LLRotation."<<endl;
AVLNode* p = Node->LeftChild;
Node->LeftChild = p->LeftChild;
p->LeftChild = p->RightChild;
p->RightChild = Node->RightChild;
Node->RightChild = p;
Element temp = p->data; p->data = Node->data; Node->data = temp;
}

void RRRotation(AVLNode* &Node)
{
cout<<"RRRotation."<<endl;
AVLNode* p = Node->RightChild;
Node->RightChild = p->RightChild;
p->RightChild = p->LeftChild;
p->LeftChild = Node->LeftChild;
Node->LeftChild = p;
Element temp = p->data; p->data = Node->data; Node->data = temp;
}

void LRRotation(AVLNode* &Node)
{
cout<<"LRRotation(先RR后LL)."<<endl;
RRRotation(Node->LeftChild);
LLRotation(Node);
}

void RLRotation(AVLNode* &node)
{
cout<<"RLRotation(先LL后RR)."<<endl;
LLRotation(node->RightChild);
RRRotation(node);
}

void Insert(AVLNode* &root,Element e)
{
AVLNode* p = root;
if(p == NULL)
{
AVLNode* pt = new AVLNode(e,0,NULL,NULL);
root = pt;
}
else
{
if(e <= p->data)
{
Insert(p->LeftChild,e);
if(GetHeight(p->LeftChild) - GetHeight(p->RightChild) == 2)
{
if(e < p->LeftChild->data)
{
LLRotation(p);
}
else
{
LRRotation(p);
}
}
p->height = MaxHeight(GetHeight(p->LeftChild),GetHeight(p->RightChild)) + 1;
}
else if(e > p->data)
{
Insert(p->RightChild,e);
if(GetHeight(p->RightChild) - GetHeight(p->LeftChild) == 2)
{
if(e < p->LeftChild->data)
{
RLRotation(p);
}
else
{
RRRotation(p);
}
}
p->height = MaxHeight(GetHeight(p->LeftChild),GetHeight(p->RightChild)) + 1;
}
}
}

void Delete(AVLNode* &root,Element key)
{
if(!root)
{
cout<<"No Record!"<<endl;
}
else
{
if(key == root->data)
{
if ( NULL==root->LeftChild && NULL==root->RightChild ) //叶节点
{

delete root;
root = NULL;
}

else if(root->LeftChild != NULL && root->RightChild == NULL)
{
AVLNode* p = root->LeftChild;
root->data = p->data;
root->LeftChild = p->LeftChild;
root->RightChild = p->RightChild;
root->height = MaxHeight(GetHeight(root->LeftChild),GetHeight(root->RightChild));// 重新更新该节点一下的所有结点高度信息
delete root;
root = NULL;
}
else if(root->LeftChild = NULL && root->RightChild != NULL)
{
AVLNode* p = root->RightChild;
root->data = p->data;
root->LeftChild = p->LeftChild;
root->RightChild = p->RightChild;
root->height = MaxHeight(GetHeight(root->LeftChild),GetHeight(root->RightChild));// 重新更新该节点一下的所有结点高度信息
delete root;
root = NULL;
}
else //左右两个孩子
{
AVLNode* p = root->RightChild;
if(p->LeftChild == NULL) //右孩子没有左孩子
{
root->data = p->data;
root->RightChild = p->RightChild;
delete p;
p = NULL;
if(GetHeight(p->RightChild) - GetHeight(p->LeftChild) == 2)
{
if(GetHeight(root->LeftChild->LeftChild) >= GetHeight(p->LeftChild->RightChild))
{
LLRotation(root) ;
}
else
{
LRRotation(root);
}
}
root->height = MaxHeight(GetHeight(root->LeftChild),GetHeight(root->RightChild));// 重新更新该节点一下的所有结点高度信息
}
else //右孩子有左孩子
{
while(p->LeftChild->LeftChild != NULL)
{
p = p->LeftChild;
}
AVLNode* pt = p->LeftChild;
root->data = pt->data;
p->LeftChild = pt->RightChild;
delete pt; pt = NULL;
if( GetHeight( root->LeftChild ) - GetHeight(root->RightChild)>=2 ) // 调整结点,保持平衡。
{
if( GetHeight( root->LeftChild->LeftChild ) >= GetHeight(root->LeftChild->RightChild))
{
LLRotation(root);
}
else
{
LRRotation(root);
}

}
root->height = MaxHeight(GetHeight(root->LeftChild),GetHeight(root->RightChild));// 重新更新该节点一下的所有结点高度信息

}
}
}


else if( key<root->data ) //在左子树删除数据
{
Delete(root->LeftChild,key);
if( GetHeight( root->LeftChild ) - GetHeight(root->RightChild)<=-2 ) // 调整结点,保持平衡。
{
if( GetHeight( root->RightChild->LeftChild ) >= GetHeight( root->RightChild->RightChild) )
{
RLRotation(root);
}
else
{
RRRotation(root);
}

}
root->height = MaxHeight(GetHeight(root->LeftChild),GetHeight(root->RightChild));// 重新更新该节点一下的所有结点高度信息
}


else //在右子树删除数据
{
Delete(root->RightChild,key);
if( GetHeight( root->LeftChild ) - GetHeight(root->RightChild)>=2) // 调整结点,保持平衡。
{
if( GetHeight( root->LeftChild->LeftChild ) >= GetHeight(root->LeftChild->RightChild) )
{
LLRotation(root);
}
else
{
LRRotation(root);
}
}
root->height = MaxHeight(GetHeight(root->LeftChild),GetHeight(root->RightChild)); // 重新更新该节点一下的所有结点高度信息
}

}
}

int main()
{
AVLNode *root=NULL;
int num;
Element e;
///////////创建平衡二叉树
cout<<"创建平衡二叉树,请输入节点数:"<<endl;
cin>>num;
cout<<"输入"<<num<<"个节点的数值,以空格或者回车符分开:"<<endl;
while(num--)
{

cin>>e;
Insert(root,e);
cout<<"此时根节点数据为:"<<root->data<<"("<<root->height<<")"<<endl<<"中序遍历结果为:"<<endl;
InorderWalk(root); cout<<endl<<endl;

}
cout<<"创建平衡二叉树完毕,中序遍历输出为:"<<endl;
InorderWalk(root); cout<<endl<<endl;
//////////插入数据
cout<<"请输入要插入的数"<<endl;
cin>>e;
Insert(root,e);
cout<<"此时根节点数据为:"<<root->data<<"("<<root->height<<")"<<endl;
cout<<"插入新元素后的平衡二叉树,中序遍历输出为:"<<endl;
InorderWalk(root);cout<<endl<<endl;
///////////删除结点
cout<<"输入你选择删除的数"<<endl;
cin>>e;
Delete(root,e);
cout<<"删除操作后的二叉搜索树"<<endl;
InorderWalk(root); cout<<endl<<endl;
///////////删除结点
cout<<"输入你选择删除的数"<<endl;
cin>>e;
Delete(root,e);
cout<<"删除操作后的二叉搜索树"<<endl;
InorderWalk(root); cout<<endl<<endl;
//////////查找结点
cout<<endl<<"输入要查找的数,系统找到则输出:"<<endl;
cin>>e;
AVLNode *p=Serach(root,e);
if (p) cout<<p->data<<endl;
else cout<<"没有找到该数"<<endl;
return 0;

}

posted on 2016-09-28 10:31  沉疴  阅读(211)  评论(0编辑  收藏  举报