PAT甲級1123

1123 Is It a Complete AVL Tree (30 分)

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.

F1.jpg F2.jpg
F3.jpg F4.jpg

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:

5
88 70 61 63 65

Sample Output 1:

70 63 88 61 65
YES

Sample Input 2:

8
88 70 61 96 120 90 65 68

Sample Output 2:

88 65 96 61 70 90 120 68
NO
#include<bits/stdc++.h>
using namespace std;

struct Node{
	int wei, high;
	Node* lchild;
	Node* rchild;
}*root;

Node* newNode(int wei){
	Node* node = new Node;
	node->wei = wei;
	node->high = 1;
	node->lchild = NULL;
	node->rchild = NULL;
	return node;
}

int geth(Node* root){
	if(root == NULL)
		return 0;
	return root->high;
}

void updateh(Node* root){
	root->high = max(geth(root->lchild), geth(root->rchild)) + 1;
}

int getb(Node* root){
	return geth(root->lchild) - geth(root->rchild);
}

void L(Node* &root){
	Node* temp = root->rchild;
	root->rchild = temp->lchild;
	temp->lchild = root;
	updateh(root);
	updateh(temp);
	root = temp;
}

void R(Node* &root){
	Node* temp = root->lchild;
	root->lchild = temp->rchild;
	temp->rchild = root;
	updateh(root);
	updateh(temp);
	root = temp;
}

void insert(Node* &root, int wei){
	if(root == NULL){
		root = newNode(wei);
		return;
	}
	if(wei < root->wei){
		insert(root->lchild, wei);
		updateh(root);
		if(getb(root) == 2){
			if(getb(root->lchild) == 1){
				R(root);
			}
			else if(getb(root->lchild) == -1){
				L(root->lchild);
				R(root);
			}
		}
	}
	else{
		insert(root->rchild, wei);
		updateh(root);
		if(getb(root) == -2){
			if(getb(root->rchild) == -1){
				L(root);
			}
			else if(getb(root->rchild) == 1){
				R(root->rchild);
				L(root);
			}
		}
	}
}


void levelorder(Node* root){
	queue<Node*> q;
	vector<int> ans;
	q.push(root);
	bool f1 = true, f2 =true;
	while(!q.empty()){
		Node* top = q.front();
		q.pop();
		ans.push_back(top->wei);
		if(f1 && (top->lchild == NULL || top->rchild == NULL)){
			f1 = false;
			if(top->lchild == NULL && top->rchild != NULL)
				f2 = false;
		}	
		else if(!f1 && f2 && (top->lchild != NULL || top->rchild != NULL)) 
			f2 = false;
		if(top->lchild != NULL)
			q.push(top->lchild);
		if(top->rchild != NULL)
			q.push(top->rchild);
	}
	for(int i = 0; i < ans.size(); i++)
	if(i != ans.size()-1)
		printf("%d ", ans[i]);
	else
		printf("%d\n", ans[i]);
	if(f2)
		printf("YES");
	else
		printf("NO");
}

int main(){
	int n, wei;
	scanf("%d", &n);
	root = NULL;
	for(int i = 0; i < n; i++){
		scanf("%d", &wei);
		insert(root, wei);
	}
	levelorder(root);
	return 0;
}

 

發佈了117 篇原創文章 · 獲贊 15 · 訪問量 7301
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章