PAT 1066 平衡树

#include<iostream>
#include<algorithm>
#include<string>
#include<malloc.h>
#include<cstring>
#include<vector>
using namespace std;
#define Max(x,y) ((x)>(y)?(x):(y))
#define ABS(x) ((x)>0?(x):-(x))
struct Node{
	Node* l,*r;
	int val,h;
	Node(int x){
		l=r=NULL;
		h=1;val=x;
	}
	int getH(Node*p){
		return p==NULL?0:p->h;
	}
	bool updateH(){
		int lh=getH(l),rh=getH(r);
		h=Max(lh,rh)+1;
		return ABS(lh-rh)>1;
	}
};
Node *LLRotate(Node* p){
	Node *q=p->l;
	p->l=q->r;
	q->r=p;
	p->updateH();q->updateH();
	return q;
}
Node *RRRotate(Node*p){
	Node *q=p->r;
	p->r=q->l;
	q->l=p;
	p->updateH();q->updateH();
	return q;
}
Node* LRRotate(Node*p){
	p->l=RRRotate(p->l);
	return LLRotate(p);
}
Node* RLRotate(Node*p){
	p->r=LLRotate(p->r);
	return RRRotate(p);
}
Node* build(Node* p,int x){
	if(p==NULL)
		return new Node(x);
	if(p->val>x){
		chooseLeft=true;
		p->l=build(p->l,x);
		if(p->updateH()){
			if(p->getH(p->l->l)>p->getH(p->l->r))
				p=LLRotate(p);
			else p=LRRotate(p);
		}
	}
	else if(p->val<x){
		chooseLeft=false;
		p->r=build(p->r,x);
		if(p->updateH()){
			if(p->getH(p->r->l)>p->getH(p->r->r))
				p=RLRotate(p);
			else p=RRRotate(p);
		}
	}
	p->updateH();
	return p;
}
int main()
{
	int i,n,x;
	scanf("%d",&n);
	Node *root=NULL;
	for(i=0;i<n;++i){
		scanf("%d",&x);
		root=build(root,x);
	}
	printf("%d\n",root->val);
	return 0;
}

时间: 2024-08-16 17:44:01

PAT 1066 平衡树的相关文章

PAT 1066. 图像过滤(15)

图像过滤是把图像中不重要的像素都染成背景色,使得重要部分被凸显出来.现给定一幅黑白图像,要求你将灰度值位于某指定区间内的所有像素颜色都用一种指定的颜色替换. 输入格式: 输入在第一行给出一幅图像的分辨率,即两个正整数M和N(0 < M, N <= 500),另外是待过滤的灰度值区间端点A和B(0 <= A < B <= 255).以及指定的替换灰度值.随后M行,每行给出N个像素点的灰度值,其间以空格分隔.所有灰度值都在[0, 255]区间内. 输出格式: 输出按要求过滤后的图

PAT 1066 Root of AVL Tree[AVL树][难]

1066 Root of AVL Tree (25)(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore

PAT 1066 Root of AVL Tree

#include <cstdio> #include <cstdlib> class Node { public: Node* L; Node* R; int height; int data; Node(int val, Node* l = NULL, Node* r = NULL, int h = 0): data(val), L(l), R(r), height(h) {} }; inline int height(Node* node) { if (node == NULL

PAT-1066 Root of AVL Tree(解题报告)

第一次做关于平衡树的题目 第一次做的时候 忘记判断是否是空节点 第二次做,发现LR平衡自己理解错了 对于链式结构理解还是不够深刻,乖乖看书去 题目链接 PAT 1066 附上关于平衡树的四种旋转操作     代码如下: #include<stdio.h> typedef int ElementType; typedef struct AVLTreeNode *AVLTree; typedef struct AVLTreeNode{ ElementType Data; AVLTree Left;

1066. Root of AVL Tree (25)【AVL树】——PAT (Advanced Level) Practise

题目信息 1066. Root of AVL Tree (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more tha

PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

1066 Root of AVL Tree (25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this

PAT甲级题解-1066. Root of AVL Tree (25)

我嗯,这道题比较讨厌,非常难,需要用到马尔科夫随便链.高斯混合复制模型.神经网络还需要用到本人的模拟退火算法进行优化搜索,为了搜索文章避免陷入局部优化,还得用到一些Tabu Search代码比较多写的有点乱七八糟,有的还点长,都贴到laji的网站如了.布料布衣价格上涨,扣子费用降低.这样导致过程很难算,有难度,但还是有点意思的,建议自省反省考虑下,嗯么. #include <iostream> #include <cstdio> #include <algorithm>

PAT (Advanced Level) 1066. Root of AVL Tree (25)

AVL树的旋转.居然1A了.... 了解旋转方式之后,数据较小可以当做模拟写. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using name

PAT 乙级 1066. 图像过滤(15)

图像过滤是把图像中不重要的像素都染成背景色,使得重要部分被凸显出来.现给定一幅黑白图像,要求你将灰度值位于某指定区间内的所有像素颜色都用一种指定的颜色替换. 输入格式: 输入在第一行给出一幅图像的分辨率,即两个正整数M和N(0 < M, N <= 500),另外是待过滤的灰度值区间端点A和B(0 <= A < B <= 255).以及指定的替换灰度值.随后M行,每行给出N个像素点的灰度值,其间以空格分隔.所有灰度值都在[0, 255]区间内. 输出格式: 输出按要求过滤后的图