国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄢陵县| 石泉县| 连南| 盱眙县| 静海县| 奉化市| 商城县| 苍溪县| 辉南县| 类乌齐县| 涞水县| 宝清县| 光泽县| 信丰县| 禹城市| 汶上县| 宜川县| 林芝县| 长春市| 墨竹工卡县| 垫江县| 朝阳县| 河源市| 本溪市| 孝感市| 论坛| 兰西县| 天等县| 凤冈县| 山东| 梁河县| 宁强县| 阜新| 博客| 大关县| 寻乌县| 英山县| 霍邱县| 丹江口市| 深圳市| 长寿区|