PAT (Advanced Level) -1123 Is It a Complete AVL Tree(层序遍历平衡二叉树)

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.

《PAT (Advanced Level) -1123 Is It a Complete AVL Tree(层序遍历平衡二叉树)》《PAT (Advanced Level) -1123 Is It a Complete AVL Tree(层序遍历平衡二叉树)》
《PAT (Advanced Level) -1123 Is It a Complete AVL Tree(层序遍历平衡二叉树)》《PAT (Advanced Level) -1123 Is It a Complete AVL Tree(层序遍历平衡二叉树)》

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

四种情况:

LL(右旋) :调整根结点的左子树指针=左孩子的右子树指针 

RR(左旋):调整根结点的右子树指针=右孩子的左子树指针 

LR(LR旋):先调整根结点左子树 左旋 再把根结点为首的树 右旋

RL(RL旋):先调整根结点右子树 右旋 再把根结点为首的树 左旋

什么时候旋转:插入了新结点 出现左右子树高度差大于1 对应四种情况旋转

层序遍历:借助队列先进先出的特性 先进根结点,进左孩子 ,进右孩子,所以出的时候也是按左右左右的顺序

判断条件:完全二叉树,只允许右下有缺,因此在进左孩子时 遇NULL 标记一下

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

struct node
{
	int data;
	struct node *left,*right;
	node(int v):data(v),left(NULL),right(NULL){}
};

node*R_rotation(node *root)
{
	node *temp=root->left;
	root->left=temp->right;
	temp->right=root;
	return temp;
}
node*L_rotation(node *root)
{
	node *temp=root->right;
	root->right=temp->left;
	temp->left=root;
	return temp;
}
node *LR_rotation(node *root)
{
	root->left=L_rotation(root->left);
	return R_rotation(root);
}
node *RL_rotation(node *root)
{
	root->right=R_rotation(root->right);
	return L_rotation(root);
}
int get_height(node *root)
{
	return root==NULL?0:max(get_height(root->left),get_height(root->right))+1;
}

node *AVL_insert(node *root,int v)
{
	if(root==NULL)return new node(v);
	if(v<root->data)
	{
		root->left=AVL_insert(root->left,v);
		if(abs(get_height(root->left)-get_height(root->right))>1)
		return v<root->left->data?R_rotation(root):LR_rotation(root);
	}
	else
	{
		root->right=AVL_insert(root->right,v);
		if(abs(get_height(root->left)-get_height(root->right))>1)
		return v>root->right->data?L_rotation(root):RL_rotation(root);
	}
	return root;
}
int iscomplete=1,flag=0;
vector<int>levelorder(node *root)
{
	vector<int>v;
	queue<node*>queue;
	queue.push(root);
	while(!queue.empty())
	{
		node *temp=queue.front();
		queue.pop();
		v.push_back(temp->data);
		if(temp->left!=NULL)
		{
			if(flag)iscomplete=0;
			queue.push(temp->left);
		}
		else 
		{
			flag=1;
		}
		if(temp->right!=NULL)
		{
			if(flag)iscomplete=0;
			queue.push(temp->right);
		}
		else 
		{
			flag=1;
		}
	}
	return v;
}

int main()
{
	int n;
	scanf("%d",&n);
	node *root=NULL;
	for(int i=0;i<n;i++)
	{
		int t;
		scanf("%d",&t);	
		root=AVL_insert(root,t);
	}
	vector<int>res=levelorder(root);
	for(int i=0;i<res.size();i++)
	{
		if(i!=0)printf(" ");
		printf("%d",res[i]);
	}
	printf("\n%s\n",iscomplete?"YES":"NO");

}

 

    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/qq_41488703/article/details/82916541
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞