国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

PAT甲級1123

2019-11-08 02:56:30
字體:
來源:轉載
供稿:網友

1123. Is It a Complete AVL Tree (30)

時間限制400 ms內存限制65536 kB代碼長度限制16000 B判題程序Standard作者CHEN, Yue

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 PRoperty. Figures 1-4 illustrate the rotation rules.

    

    

Now given a sequence of insertions, you are supposed to output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<= 20). Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. Then in the next line, print "YES" if the tree is complete, or "NO" if not.

Sample Input 1:
588 70 61 63 65Sample Output 1:
70 63 88 61 65YESSample Input 2:
888 70 61 96 120 90 65 68Sample Output 2:
88 65 96 61 70 90 120 68NO
#include<cstdio>#include<algorithm>#include<queue>using namespace std;struct Node{	int data;	int height;	Node*l, *r;	Node():height(1),l(NULL),r(NULL){}};int getHeight(Node*n){	if (!n)return 0;	return n->height;}int getBalanceFactor(Node*n){	return getHeight(n->l) - getHeight(n->r);}void updateHeight(Node*n){	n->height = max(getHeight(n->l), getHeight(n->r)) + 1;}void L(Node*&n){	Node*temp = n->r;	n->r = temp->l;	temp->l = n;	updateHeight(n);	updateHeight(temp);	n = temp;}void R(Node*&n){	Node*temp = n->l;	n->l = temp->r;	temp->r = n;	updateHeight(n);	updateHeight(temp);	n = temp;}void insert(Node*&n,int x)//修改了指針本身要加引用{	if (!n)	{		n = new Node;		n->data = x;		return;	}	if (x < n->data)	{		insert(n->l, x);		updateHeight(n);		if (getBalanceFactor(n) == 2)		{			if (getBalanceFactor(n->l) == 1)			{				R(n);			}			else if (getBalanceFactor(n->l) == -1)			{				L(n->l);				R(n);			}		}	}	else 	{		insert(n->r, x);		updateHeight(n);		if (getBalanceFactor(n) == -2)		{			if (getBalanceFactor(n->r) == -1)			{				L(n);			}			else if (getBalanceFactor(n->r) == 1)			{				R(n->r);				L(n);			}		}	}}int maxindex = -1;void dfs(Node*n,int index){	if (!n)return;	maxindex = max(maxindex, index);	dfs(n->l, 2 * index);	dfs(n->r, 2 * index+1);}void levelOrder(Node*root){	queue<Node*> Q;	Q.push(root);	bool first = true;	while (!Q.empty())	{		Node*n = Q.front();		Q.pop();		if (first)		{			printf("%d", n->data);			first = false;		}		else			printf(" %d", n->data);		if (n->l)Q.push(n->l);		if (n->r)Q.push(n->r);	}}int main(){	int N;	scanf("%d", &N);	Node*root = NULL;	int x;	for (int i = 0; i < N; i++)	{		scanf("%d", &x);		insert(root, x);	}	levelOrder(root);	printf("/n");	dfs(root, 1);	if (maxindex == N)	{		printf("YES");	}	else		printf("NO");	return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金坛市| 南昌市| 葫芦岛市| 新乐市| 江北区| 安西县| 伊通| 荣昌县| 古蔺县| 招远市| 马尔康县| 高碑店市| 安福县| 龙江县| 庆城县| 封开县| 乌鲁木齐市| 襄垣县| 南汇区| 余干县| 古丈县| 资阳市| 安仁县| 蓝田县| 泰安市| 上虞市| 卢氏县| 武乡县| 巫山县| 安塞县| 和顺县| 同江市| 德化县| 西昌市| 宿州市| 孝义市| 宁津县| 简阳市| 玛沁县| 潍坊市| 偃师市|