PAT1066:Root of AVL Tree Java语言实现

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),输出根结点元素
解题思路:判断插入结点对现有结点的平衡因子的影响,进而进行LL,LR,RL,RR旋转
假设三个结点连接关系为A->B->C,C为新插入结点并使得A的平衡因子==2
若C在A的左孩子的左子树上,则对A与B进行LL旋转
若C在A的左孩子的右子树上,则对A,B,C进行LR旋转,可分解为首先对B与C进行RR旋转,再对A与C进行LL旋转
若C在A的右孩子的右子树上,则对A与B进行RR旋转
若C在A的右孩子的左子树上,则对A,B,C进行RL旋转,可分解为首先对B与C进行LL旋转,再对A与C进行RR旋转



import java.util.Scanner;

class TreeNode{
	int value;
	TreeNode left;
	TreeNode right;
	int height;
	TreeNode(int value){
		this.value=value;
		left=null;
		right=null;
		height=0;
	}
}

public class RootOfAVLTree {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		TreeNode root=null;
		for(int i=0;i<N;i++){
			root=insert(sc.nextInt(),root);			
		}
		System.out.println(root.value);
		sc.close();
	}
	
	public static void print(TreeNode root){
		System.out.println(root.value);
		if(root.left!=null){
			print(root.left);
		}
		if(root.right!=null){
			print(root.right);
		}
	}
	
	public static TreeNode insert(int value,TreeNode root){
		if(root==null){
			return new TreeNode(value);
		}
		if(value<root.value){
			root.left=insert(value,root.left);
		}
		else if(value>root.value)
		{
			root.right=insert(value,root.right);
		}
		else
			;
		
		return balance(root);
	}
	public static TreeNode balance(TreeNode root){
		if(root==null)
			return root;
		if(height(root.left)-height(root.right)>1){
			if(height(root.left.left)>=height(root.left.right))
				root=rotateWithLeftChild(root);
			else
				root=doubleWithLeftChild(root);
		}
		else 
			if(height(root.right)-height(root.left)>1){
				if(height(root.right.right)>=height(root.right.left))
					root=rotateWithRightChild(root);
				else
					root=doubleWithRightChild(root);
			}
		root.height=Math.max(height(root.left),height(root.right))+1;
		return root;
	}
	
	public static TreeNode rotateWithLeftChild(TreeNode root){
		TreeNode k1=root.left;
		root.left=k1.right;
		k1.right=root;
		root.height=Math.max(height(root.left), height(root.right))+1;
		k1.height=Math.max(height(k1.left), root.height)+1;
		return k1;
	}
	public static TreeNode rotateWithRightChild(TreeNode root){
		TreeNode k1=root.right;
		root.right=k1.left;
		k1.left=root;
		root.height=Math.max(height(root.left), height(root.right))+1;
		k1.height=Math.max(root.height, height(k1.right));
		return k1;
	}
	public static TreeNode doubleWithLeftChild(TreeNode root){
		root.left=rotateWithRightChild(root.left);
//		不需要在计算高度,此步骤已经包含在rotateWithRightChild(),rotateWithLeftChild()方法中了
//		root.height=Math.max(height(root.left), height(root.right))+1;
		return rotateWithLeftChild(root);
	}
	public static TreeNode doubleWithRightChild(TreeNode root){
		TreeNode k1=root.right;
//		下面这个式子是错误的,不能把rotateWithLeftChild的结果赋给k1,应该直接赋值给root.right
//		k1=rotateWithLeftChild(k1);
		root.right=rotateWithLeftChild(k1);
		return rotateWithRightChild(root);
	}
	
	private static int height(TreeNode root){
		return root==null?-1:root.height;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值