【C】平衡二叉树(AVL)

1066. Root of AVL Tree (25)

时间限制
100 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 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 ythe 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结构体中添加height元素
//这里是设了一个全局变量,再使用createAVL使初始化
//如果不设,可以在主函数中直接node *root=NULL;不使用createAVL
#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
struct node{
	int data,height;
	node *lchild,*rchild;
}*root;
node* newnode(int data){//新建数值为data的节点
	node* Node=new node;
	Node->data=data;
	Node->height=1;//初始高度为1
	Node->lchild=Node->rchild=NULL;
	return Node;
}
int getHeight(node *root){//获取树高
	if(root==NULL) return 0;
	return root->height;
}

void updateHeight(node *root){//更新树高
	root->height=max(getHeight(root->lchild),getHeight(root->rchild))+1;
}//注意这里是void而不是int!!!

int getBalanceFactor(node *root){//获得平衡因子
	return getHeight(root->lchild)-getHeight(root->rchild);
}
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 data){
	if(root==NULL){
		root=newnode(data);
		return;
	}
	if(data<root->data){
		insert(root->lchild,data);
		updateHeight(root);
		if(getBalanceFactor(root)==2){//左-右=2,LL或LR
			if(getBalanceFactor(root->lchild)==1){//LL
				R(root);
			}
			else if(getBalanceFactor(root->lchild)==-1){//LR
				L(root->lchild);//先左旋左子树
				R(root);
			}
		}
	}//由于插入左子树,只能是LL或LR
	else{
		insert(root->rchild,data);
		updateHeight(root);
		if(getBalanceFactor(root)==-2){//RR或RL
			if(getBalanceFactor(root->rchild)==-1){//RR
				L(root);
			}
			else if(getBalanceFactor(root->rchild)==1){//RL
				R(root->rchild);
				L(root);
			}
		}
	}
}
node* createAVL(int data[],int n){
	node* root=NULL;
	int i;
	for(i=0;i<n;i++){
		insert(root,data[i]);
	}
	return root;
}
int main(){
	int n,data;
	int i;
	scanf("%d",&n);
	//node *root=NULL;
	for(i=0;i<n;i++){
		scanf("%d",&data);
		insert(root,data);
	}
	//root=createAVL(data,n);
	printf("%d",root->data);
	return 0;
}

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:
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<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
struct node{
	int data;
	int height;
	node *lchild,*rchild;
};
node *newnode(int data){
	node *t=new node;
	t->lchild=t->rchild=NULL;
	t->data=data;
	t->height=1;
	return t;
}
int max(int a,int b){
	return a>b?a:b;
}
int getHeight(node *t){
	if(t==NULL) return 0;
	else return t->height;
}
void updateHeight(node *t){
	t->height=max(getHeight(t->lchild),getHeight(t->rchild))+1;
}
int getBalanceFactor(node *t){
	return getHeight(t->lchild)-getHeight(t->rchild);
}
void R(node *&root){
	node *temp=root->lchild;
	root->lchild=temp->rchild;
	temp->rchild=root;
	updateHeight(root);
	updateHeight(temp);
	root=temp;
}
void L(node *&root){
	node *temp;
	temp=root->rchild;
	root->rchild=temp->lchild;
	temp->lchild=root;
	updateHeight(root);
	updateHeight(temp);
	root=temp;
}
void insert(node *&root,int data){
	if(root==NULL){
		root=newnode(data);
		return;
	}
	if(root->data>data){//左子树
		insert(root->lchild,data);
		updateHeight(root);
		if(getBalanceFactor(root)==2){
			if(getBalanceFactor(root->lchild)==1){//LL
				R(root);
			}
			else if(getBalanceFactor(root->lchild)==-1){//LR
				L(root->lchild);
				R(root);
			}
		}
	}
	else{
		insert(root->rchild,data);
		updateHeight(root);
		if(getBalanceFactor(root)==-2){
			if(getBalanceFactor(root->rchild)==-1){//RR
				L(root);
			}
			else if(getBalanceFactor(root->rchild)==1){//RL
				R(root->rchild);
				L(root);
			}
		}
	}
}
bool bfs_ifceng(node *root){
	queue<node*> q;
	q.push(root);
	while(!q.empty()){
		node *p=q.front();
		q.pop();
		if(p->lchild) q.push(p->lchild);
		else{
			while(!q.empty()){
				node *t=q.front();
				if(t->lchild||t->rchild) return false;
				q.pop();
			}
		}
		if(p->rchild){
			if(p->lchild) q.push(p->rchild);
			else return false;//有右孩子无左孩子
		}
		else{
			while(!q.empty()){
				node *t=q.front();
				if(t->lchild||t->rchild) return false;
				q.pop();
			}
		}
	}
	return true;
}
void print_ceng(node *root){
	queue<node*> q;
	q.push(root);
	printf("%d",root->data);
	int f=1;
	while(!q.empty()){
		node *t=q.front();
		q.pop();
		if(f==1) f=0;
		else printf(" %d",t->data);
		if(t->lchild) q.push(t->lchild);
		if(t->rchild) q.push(t->rchild);
	}
}
int main(){
	int n,i;
	cin>>n;
	int data;
	cin>>data;
	node *root=newnode(data);
	for(i=1;i<n;i++){
		cin>>data;
		insert(root,data);
	}
	print_ceng(root);
	if(bfs_ifceng(root)) printf("\nYES");
	else printf("\nNO");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值