1123 Is It a Complete AVL Tree(30 分)AVL树的建立和层序遍历以及判断是不是完全二叉树

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.

《1123 Is It a Complete AVL Tree(30 分)AVL树的建立和层序遍历以及判断是不是完全二叉树》《1123 Is It a Complete AVL Tree(30 分)AVL树的建立和层序遍历以及判断是不是完全二叉树》
《1123 Is It a Complete AVL Tree(30 分)AVL树的建立和层序遍历以及判断是不是完全二叉树》《1123 Is It a Complete AVL Tree(30 分)AVL树的建立和层序遍历以及判断是不是完全二叉树》

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

AVL树的建立和层序遍历以及判断是不是完全二叉树

建树  注意命名是根据发生节点不平衡来命名的    层序遍历队列以及判断是不是完全二叉树,层序遍历中 对于每一层 如果说下一层是的非最后一个是空的~ 但是后面确实有节点存在 那就不是完全二叉树 恩。。。。

 

代码:

#include<bits/stdc++.h>
using namespace std;
struct node
{
	int data;
	node *left;
	node *right;
};
int getHeight(struct node* tree)
{
	if(tree == NULL)
		return 0;
	int l = getHeight(tree->left);
	int r = getHeight(tree->right);
	return max(l, r) + 1;
}
struct node* rightRotate(struct node*tree)
{
	struct node*temp = tree->right;
	tree->right = temp -> left;
	temp->left = tree;
	return temp;
}
struct node* leftRotate(struct node *tree)
{
	struct node*temp = tree->left;
	tree->left = temp->right;
	temp->right = tree;
	return temp;
}
struct node* leftrightRotate(struct node* tree)
{
	tree->left = rightRotate(tree->left);
	return leftRotate(tree);
}
struct node* rightleftRotate(struct node* tree)
{
	tree->right = leftRotate(tree->right);
	return rightRotate(tree);
}
struct node* insert(struct node* tree, int val)
{
	if(tree == NULL)
	{
		tree = new node();
		tree->data = val;
		tree->left = tree->right = NULL;
		return tree;
	}
	if(val < tree->data)
	{
		tree->left = insert(tree->left,val);
		int l = getHeight(tree->left); 
		int r = getHeight(tree->right);
		if(l - r >= 2)
		{
			if (val < tree->left->data)
				tree = leftRotate(tree);//发生问题在左子树的左边
			else 
				tree = leftrightRotate(tree); //发生问题在左子树的右边
		}
	}else 
	{
		tree->right = insert(tree->right,val);
		int l = getHeight(tree->left); 
		int r = getHeight(tree->right);
		if(r - l >= 2)
		{
			if(val > tree->right->data)
				tree = rightRotate(tree);//发生问题在右子树的右边 
			else 
				tree = rightleftRotate(tree); //发生问题在右子树的左边 
		}
	}
	return tree;
}
//层序遍历好弄 最重要的是怎么弄?是否是完全二叉树怎么判定
int isComplete = 1, lack = 0; //after判断目前当前层次中是否有缺失的 如果有的话  之后还有节点的话那就说不是完全二叉树 确实
vector<int> LevelOrder(struct node *tree)
{
	queue<node*>qu;
	qu.push(tree);
	vector<int>vec;
	while(!qu.empty())
	{
		node *tmp = qu.front();
		qu.pop();
		vec.push_back(tmp->data);
		if(tmp->left != NULL)
		{
			qu.push(tmp->left);
			if (lack) 
				isComplete = 0;
		}else 
			lack = 1;	
					
		if(tmp->right != NULL)
		{
			qu.push(tmp->right);
			if (lack) 
				isComplete = 0;
		}else 
			lack = 1;			
	}	
	return vec;
}
int main()
{
	int n,x;
	scanf("%d",&n);
	struct node* tree = NULL;
	for(int i = 1; i <= n; i++)
	{
		scanf("%d",&x);
		tree = insert(tree,x);
	}
	//接下来就是层序遍历和判断是不是完全二叉树了
	vector<int> vec = LevelOrder(tree); 
	for(int i = 0 ; i <= n - 1; i ++)
		printf("%d%c",vec[i]," \n"[i == n - 1]);
	printf("%s", isComplete ? "YES" : "NO");
	return 0;	
}

 

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