PAT甲级-1066 Root of AVL Tree (25分)

点击链接PAT甲级-AC全解汇总

题目:
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 tell the root of the resulting AVL tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤20) which is the total number of keys to be inserted. 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, print the root of the resulting AVL tree in one line.

Sample Input 1:

5
88 70 61 96 120

Sample Output 1:

70

Sample Input 2:

7
88 70 61 96 120 90 65

Sample Output 2:

88

题意:
输出avl的root

虽然就25分的题,但是考察最基础的数据结构算法,还是很顶的
参考了算法笔记才写出来,论基本功的重要性

我的代码:

#include<bits/stdc++.h>
using namespace std;

struct node{
	int v,height;
	node *lchild;
	node *rchild;
};

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

int getheight(node* root){
	if(root == NULL) return 0;
	return root->height;
}

int getbalancefactor(node* root){
	return getheight(root->lchild) - getheight(root->rchild);
}

void updateheight(node* root){
	root->height = max(getheight(root->lchild),getheight(root->rchild)) + 1;
}

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

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

void insert(node* &root,int v){
	if(root == NULL){
		root = newNode(v);
		return;
	}
	if(v <root->v){
		insert(root->lchild,v);
		updateheight(root);
		if(getbalancefactor(root) == 2){
			if(getbalancefactor(root->lchild) == 1){
				R(root);
			}
			else if(getbalancefactor(root->lchild) == -1){
				L(root->lchild);
				R(root);
			}
		}
	}
	else{
		insert(root->rchild,v);
		updateheight(root);
		if(getbalancefactor(root) == -2){
			if(getbalancefactor(root->rchild) == -1){
				L(root);
			}
			else if(getbalancefactor(root->rchild) == 1){
				R(root->rchild);
				L(root);
			}
		}
	}
}

int main(){
    node* root = NULL;
    int N;
    cin>>N;
    for(int i=0;i<N;i++)
    {
        int t;
        cin>>t;
        insert(root,t);
    }
	cout<<root->v<<endl;
}

面向测试点编程: 意外发现的,这位老哥实属优秀啊!

扫描二维码关注公众号,回复: 11567745 查看本文章
#include <bits/stdc++.h>
using namespace std;

int main(int argc, char const *argv[])
{
	int n;
	cin >> n;
	int node[21];
	for (int i = 1; i <= n; ++i)
	{
		cin >> node[i];
	}
	sort(node+1, node+n+1);
	if(n==11) cout << node[5] << endl;
	else if(n%2==0) cout << node[n/2+2] << endl;
	else cout << node[(n+1)/2] << endl;
	return 0;
}

原文链接:满分试探法

猜你喜欢

转载自blog.csdn.net/qq_34451909/article/details/105952328