#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
typedef struct Node{
int val;
int height;
Node *lc;
Node *rc;
Node(int v=INF, int h=1){
val=v;
height=h;
lc=NULL;
rc=NULL;
}
}Node;
int getHeight(Node *root){
return root==NULL?0:(*root).height;
}
void avlShow(Node *root){
if(root==NULL)
return;
queue<Node> que;
while(!que.empty())
que.pop();
que.push(*root);
int cnt = getHeight(root);
while(!que.empty()){
Node t = que.front();
if(cnt==0)
break;
printf("%5d:\t", cnt);
queue<Node> next;
while(!next.empty())
next.pop();
while(!que.empty()){
if(t.val!=INF)
printf(" %d", t.val);
else
printf(" -1");
if(t.lc!=NULL)
next.push(*(t.lc));
else
next.push(Node());
if(t.rc!=NULL)
next.push(*(t.rc));
else
next.push(Node());
que.pop();
if(!que.empty())
t = que.front();
}
printf("\n");
cnt--;
while(!next.empty()){
que.push(next.front());
next.pop();
}
}
}
Node* leftLeftRotation(Node *root){
Node *lc = (*root).lc;
(*root).lc = (*lc).rc;
(*lc).rc = root;
(*root).height = max(getHeight((*root).lc), getHeight((*root).rc))+1;
(*lc).height = max(getHeight((*lc).lc), getHeight((*lc).rc));
return lc;
}
Node* rightRightRotation(Node *root){
Node *rc = (*root).rc;
(*root).rc = (*rc).lc;
(*rc).lc = root;
(*root).height = max(getHeight((*root).lc), getHeight((*root).rc))+1;
(*rc).height = max(getHeight((*rc).lc), getHeight((*rc).rc));
return rc;
}
Node* leftRightRotation(Node *root){
(*root).lc = rightRightRotation((*root).lc);
return leftLeftRotation(root);
}
Node* rightLeftRotation(Node *root){
(*root).rc = leftLeftRotation((*root).rc);
return rightRightRotation(root);
}
Node* valInsert(Node *root, int key){
if(root==NULL)
root = new Node(key, 0);
else{
if(key<(*root).val){
(*root).lc = valInsert((*root).lc, key);
if(getHeight(root->lc)-getHeight(root->rc)==2){
if(key<(root->lc->val))
root = leftLeftRotation(root);
else
root = leftRightRotation(root);
}
}
else{
if(key>(*root).val){
(*root).rc = valInsert((*root).rc, key);
if(getHeight(root->rc)-getHeight(root->lc)==2){
if(key>(root->rc->val))
root = rightRightRotation(root);
else
root = rightLeftRotation(root);
}
}
}
}
(*root).height = max(getHeight(root->lc), getHeight(root->rc))+1;
return root;
}
Node* getMax(Node *root){
if(root==NULL)
return NULL;
while(root->rc!=NULL)
root = root->rc;
return root;
}
Node* getMin(Node *root){
if(root==NULL)
return NULL;
while(root->lc!=NULL)
root=root->lc;
return root;
}
Node* valRemove(Node *root, int key){
if(root==NULL)
return NULL;
if(key<root->val){
root->lc = valRemove(root->lc, key);
if(getHeight(root->rc)-getHeight(root->lc)==2){
if(getHeight(root->rc->lc)>getHeight(root->rc->rc))
root = rightLeftRotation(root);
else
root = rightRightRotation(root);
}
}
else{
if(key>root->val){
root->rc = valRemove(root->rc, key);
if(getHeight(root->lc)-getHeight(root->rc)==2){
if(getHeight(root->lc->rc)>getHeight(root->lc->lc))
root = leftRightRotation(root);
else
root = leftLeftRotation(root);
}
}
else{
if((root->lc!=NULL) && (root->rc!=NULL)){
if(getHeight(root->lc)>getHeight(root->rc)){
Node *maxNode = getMax(root->lc);
root->val = maxNode->val;
root->lc = valRemove(root->lc, root->val);
}
else{
Node *minNode = getMin(root->rc);
root->val = minNode->val;
root->rc = valRemove(root->rc, root->val);
}
}
else{
Node *tmp = root;
root = (root->lc!=NULL)?root->lc:root->rc;
delete tmp;
}
}
}
(*root).height = max(getHeight(root->lc), getHeight(root->rc))+1;
return root;
}
void test1(){
Node *root=NULL;
root = valInsert(root, 8);
root = valInsert(root, 4);
root = valInsert(root, 2);
root = valInsert(root, 12);
root = valInsert(root, 6);
root = valInsert(root, 5);
avlShow(root);
}
void test2(){
Node *root=NULL;
root = valInsert(root, 8);
root = valInsert(root, 4);
root = valInsert(root, 12);
root = valInsert(root, 2);
avlShow(root);
valRemove(root, 4);
avlShow(root);
}
int main()
{
// test1();
test2();
return 0;
}
AVL树的实现-c++
最新推荐文章于 2024-07-15 15:17:48 发布