1123 Is It a Complete AVL Tree (30分) AVL 平衡二叉搜索树 层序遍历

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.jpgF2.jpg
F3.jpgF4.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

 吾:其实只是层序输出方式不同,没有用queue。因为看柳之前的代码,可以通过index的大小输出层序遍历,觉得很好用,所以这里用了这种方式,也算是小小的学以致用吧。 

树的旋转很重要!!!

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int maxindex = -1;


struct node
{
	node* left, * right;
	int data, level,index;
};

bool cmp(node *a, node *b)
{
	return a->index < b->index;
}

node * rotateleft(node *root)
{
	node *t = root->right;
	root->right = t->left;
	t->left = root;
	return t;
}

node * rotateright(node *root)
{
	node *t = root->left;
	root->left = t->right;
	t->right = root;
	return t;
}

node * leftToright(node *root)
{
	root->left=rotateleft(root->left);
	return rotateright(root);
}

node * rightToleft(node *root)
{
	root->right=rotateright(root->right);
	return rotateleft(root);
}

int getheight(node *root)
{
	if (root==NULL)
	{
		return 0;
	}
	return max(getheight(root->left), getheight(root->right)) + 1;
}

void dfsindex(node *root)
{
	if (root!=NULL&&root->index>maxindex)
	{
		maxindex = root->index;
	}
	if (root->left!=NULL)
	{
		root->left->index = root->index * 2;
		dfsindex(root->left);
	}
	if (root->right!=NULL)
	{
		root->right->index = root->index * 2 + 1;
		dfsindex(root->right);
	}
}

node* insert(node* root, int value)
{
	if (root==NULL)
	{
		root = new node();
		root->data = value;
		root->left = NULL;
		root->right = NULL;
		
	}
	else if(value<root->data)
	{
		root->left=insert(root->left, value);
		
		if (getheight(root->left) - getheight(root->right) == 2)
		{
			root=value < root->left->data ? rotateright(root) : leftToright(root);
		}
	}
	else
	{
		root->right = insert(root->right, value);
		
		if (getheight(root->right)-getheight(root->left)==2)
		{
			root = value > root->right->data ? rotateleft(root) : rightToleft(root);
		}
	}
	return root;
}

vector<node*> v;
void getnode(node* root)
{
	if (root!=NULL)
	{
		v.push_back(root);
	}
	if (root->left!=NULL)
	{		
		getnode(root->left);
	}
	if (root->right!=NULL)
	{
		getnode(root->right);
	}
}

int main()
{//其实也只是层序输出方式不同而已,前面的调整数要会
	//freopen("in.txt", "r", stdin);
	int n,t;
	cin >> n;
	node *root = NULL;
	for (int i = 0; i < n; i++)
	{
		cin >> t;
		root=insert(root, t);		
	}
	root->index = 1;
	dfsindex(root);//从上到下,把每个点的index纠正好
	getnode(root);//遍历树种所有的点,压入v中
	sort(v.begin(), v.end(), cmp);//把v按照index排序,用于层序输出
	cout << v[0]->data;
	for (int i = 1; i < v.size(); i++)
	{
		cout << " " << v[i]->data;
	}
	cout << endl;
	cout << ((maxindex == n) ? "YES" : "NO");
	cout<< endl;
	return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值