萌新刚学平衡树1/2天,样例输出1 1,求help!
  • 板块P2073 送花
  • 楼主STUDENT00
  • 当前回复8
  • 已保存回复8
  • 发布时间2022/11/27 19:24
  • 上次更新2023/10/27 01:10:39
查看原帖
萌新刚学平衡树1/2天,样例输出1 1,求help!
658786
STUDENT00楼主2022/11/27 19:24

RT,码风良好的AVL平衡树代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef struct Node{
	int key,value,height;
	Node *left,*right;
} BTNode;
int height(BTNode *N){
	if(N==NULL) return 0;
	return N->height;
}
BTNode *newNode(int key,int value){
	BTNode *node=(BTNode*)malloc(sizeof(Node));
	node->key=key;
	node->value=value;
	node->height=1;
	node->left=node->right=NULL;
	return node;
}
BTNode *ll_rotate(BTNode *y){
	BTNode *x=y->left;
	y->left=x->right;
	x->right=y;
	y->height=max(height(y->left),height(y->right))+1;
	x->height=max(height(x->left),height(x->right))+1;
	return x;
}
BTNode *rr_rotate(BTNode *y){
	BTNode *x=y->right;
	y->right=x->left;
	x->left=y;
	y->height=max(height(y->left),height(y->right))+1;
	x->height=max(height(x->left),height(x->right))+1;
	return x;
}
BTNode *lr_rotate(BTNode *y){
	BTNode *x=y->left;
	y->left=rr_rotate(x);
	return ll_rotate(y);
}
BTNode *rl_rotate(BTNode *y){
	BTNode *x=y->right;
	y->right=ll_rotate(x);
	return rr_rotate(y);
}
int getBalance(BTNode *N){
	if(N==NULL) return 0;
	return height(N->left)-height(N->right);
}
BTNode *insert(BTNode *node,int key,int value){
	if(node==NULL) return newNode(key,value);
	if(key<node->key) insert(node->left,key,value);
	else if(key>node->key) insert(node->right,key,value);
	else return node;
	node->height=max(height(node->left),height(node->right));
	int balance=getBalance(node);
	if(balance>1&&key<node->left->key) return ll_rotate(node);
	else if(balance>1&&key>node->left->key) return lr_rotate(node);
	else if(balance<-1&&key<node->right->key) return rl_rotate(node);
	else if(balance<-1&&key>node->right->key) return rr_rotate(node);
	return node; 
}
BTNode *delete_expensive(BTNode *node){
	if(node==NULL) return node;
	delete_expensive(node->right);
	if(node==NULL) return node;
	node->height=max(height(node->left),height(node->right))+1;
	int balance=getBalance(node);
	if(balance>1&&getBalance(node->left)>1) return ll_rotate(node);
	else if(balance>1&&getBalance(node->left)<-1) return lr_rotate(node);
	else if(balance<-1&&getBalance(node->right)>1) return rl_rotate(node);
	else if(balance<-1&&getBalance(node->right)<-1) return rr_rotate(node);
	return node; 
}
BTNode *delete_cheap(BTNode *node){
	if(node==NULL) return node;
	delete_cheap(node->left);
	if(node==NULL) return node;
	node->height=max(height(node->left),height(node->right))+1;
	int balance=getBalance(node);
	if(balance>1&&getBalance(node->left)>1) return ll_rotate(node);
	else if(balance>1&&getBalance(node->left)<-1) return lr_rotate(node);
	else if(balance<-1&&getBalance(node->right)>1) return rl_rotate(node);
	else if(balance<-1&&getBalance(node->right)<-1) return rr_rotate(node);
	return node; 
}
int query_w(BTNode *node){
	if(node==NULL) return 0;
	return (node->key)+query_w(node->left)+query_w(node->right);
}
int query_c(BTNode *node){
	if(node==NULL) return 0;
	return (node->value)+query_c(node->left)+query_c(node->right);
}
int main(){
	BTNode *root=NULL;
	int op=0;
	while(op!=-1){
		scanf("%d",&op);
		if(op==1){
			int w,c;
			scanf("%d%d",&w,&c);
			root=insert(root,c,w);
		}else if(op==2) root=delete_expensive(root);
		else if(op==3) root=delete_cheap(root);
	}
	printf("%d %d",query_w(root),query_c(root));
	return 0;
}
2022/11/27 19:24
加载中...