【PAT】1066. Root of AVL Tree (25)

题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1066

题目描述:

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树的构建,可以参考:http://www.cppblog.com/cxiaojia/archive/2013/07/22/187776.html

参考代码:

#include<iostream>
#include<cmath>
using namespace std;

struct TreeNode
{
	int value;
	TreeNode *left;
	TreeNode *right;
	int height;
	TreeNode(int v):value(v),left(NULL),right(NULL),height(0){}
	TreeNode():left(NULL),right(NULL){}
};

int getHeight(TreeNode *t)
{
	if(t == NULL) return -1;
	else return t->height;
}

int max(int a,int b) {return a>b? a:b;}

//左左
TreeNode *SingleRotateLeft(TreeNode *t2)
{
	TreeNode *t1;
	t1 = t2->left;
    t2->left = t1->right;
	t1->right = t2;

	t2->height = max(getHeight(t2->left),getHeight(t2->right)) + 1;
	t1->height = max(getHeight(t1->left),getHeight(t1->right)) + 1;
	return t1;
}

//右右
 TreeNode *SingleRotateRight(TreeNode *t2)
 {
	 TreeNode *t1;
	 t1 = t2->right;
	 t2->right = t1->left;
	 t1->left = t2;

	 t2->height = max(getHeight(t2->left),getHeight(t2->right)) + 1;
	 t1->height = max(getHeight(t1->left),getHeight(t1->right)) + 1;
	 return t1;
 }

 //左右
 TreeNode * DoubleRotateLR(TreeNode *t3)
 {
	 t3->left = SingleRotateRight(t3->left);
	 return SingleRotateLeft(t3);
 }

 //右左
 TreeNode * DoubleRotateRL(TreeNode *t3)
 {
	 t3->right = SingleRotateLeft(t3->right);
	 return SingleRotateRight(t3);
 }
 
 bool isBalanced(TreeNode *left,TreeNode *right)
 {
	 return abs(getHeight(left) - getHeight(right)) < 2;
 }

 TreeNode* insert(int v, TreeNode *root)
 {
	if(root == NULL)
	{
		root = new TreeNode(v);
		return root;
	}
	if(v > root->value) //节点插入在右子树中
	{
		root->right = insert(v,root->right);
		if(!isBalanced(root->left,root->right)){
			if(v > root->right->value)
				root = SingleRotateRight(root);
			else
				root = DoubleRotateRL(root);
		}
	}else{
		root->left = insert(v,root->left);
		if(!isBalanced(root->left,root->right)){
			if(v < root->left->value)
				root = SingleRotateLeft(root);
			else
				root = DoubleRotateLR(root);
		}
	}
	root->height = max(getHeight(root->left),getHeight(root->right)) + 1;
	return root;
 }

 int main()
 {
	 int n;
	 while(cin>>n)
	 {
		 int t;
		 TreeNode *root = NULL;
		 for(int i=0; i<n; i++)
		 {	
			 cin>>t;
			 root = insert(t,root);			 
		 }
		 cout<<root->value<<endl;
	 }
	 return 0;
 }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值