平衡搜索二叉树生成java代码

package zachen2.avlTree;

public class ALVTree {
	private Node root;
	private int size;
	
	public ALVTree(){
		this.root = null;
		size = 0;
	}
	
	public int getSize(){
		return this.size;
	}
	
	public boolean isEmpty(){
		return this.size == 0;
	}
	
	//节点的高度
	private int getHight(Node node){
		if(node == null)
			return 0;
		return node.height;
	}
	
	//节点的平衡因子:左子树的高度 - 右子树的告诉
	private int getBalanceFactor(Node node){
		if(node == null){
			return 0;
		}
		
		return getHight(node.left) - getHight(node.right);
	}
	
	private void add(int element){
		this.root = add(this.root, element);
	}
	
	private Node add(Node node,int element){
		if(node == null){
			size++;
			return new Node(element);
		}
		
		if(element < node.element){
			node.left = add(node.left, element);
		}else if(element > node.element){
			node.right = add(node.right, element);
		}else{
			node.element = element;
		}
		
		//更改height
		node.height =Math.max(getHight(node.left), getHight(node.right)) + 1;
		
		//计算平衡因子
		int balanceFactor = getBalanceFactor(node);
		
		//重新维护平衡
		if(balanceFactor > 1 && getBalanceFactor(node.left) >= 0){
			return rightRotate(node);
		}
		
		if(balanceFactor > 1 && getBalanceFactor(node.left) < 0){
			node.left = leftRotate(node.left);
			return rightRotate(node);
		}
		
		if(balanceFactor < -1 && getBalanceFactor(node.right) <= 0){
			return leftRotate(node);
		}
		
		if(balanceFactor < -1 && getBalanceFactor(node.right) > 0){
			node.right = rightRotate(node.right);
			return leftRotate(node);
		}
		
		return node;
	}
	
		
	///////////////////////////////////////////////////
	// LL T1<Z<T2< X <T3<Y<T4                        //
	//        y                              x       //
	//       / \                           /   \     //
	//      x   T4     向右旋转 (y)              z     y    //
	//     / \       - - - - - - - ->    / \   / \   //
	//    z   T3                        T1 T2 T3 T4  //
	//   / \                                         //
	// T1   T2                                       //
	///////////////////////////////////////////////////
	//右旋
	private Node rightRotate(Node y){
		Node x = y.left;
		Node T3 = x.right;
		
		x.right = y;
		y.left = T3;
		
		//更新height
		x.height = Math.max(getHight(x.left), getHight(x.right)) + 1;
		y.height = Math.max(getHight(y.left), getHight(y.right)) + 1;
		
		return x;
	}
	
	////////////////////////////////////////////////
	// RR T1<Y<T2< X <T3<Z<T4                     //
	//    y                             x         //
	//  /  \                          /   \       //
	// T1   x      向左旋转 (y)          y     z      //
	//     / \   - - - - - - - ->   / \   / \     //
	//    T2  z                    T1 T2 T3 T4    //
	//       / \                                  //
	//      T3 T4                                 //
	////////////////////////////////////////////////
	//左旋
	private Node leftRotate(Node y){
		Node x = y.right;
		Node T2 = x.left;
		
		x.left = y;
		y.right = T2;
		
		//更新height
		x.height = Math.max(getHight(x.left), getHight(x.right)) + 1;
		y.height = Math.max(getHight(y.left), getHight(y.right)) + 1;
		
		return x;
	} 
	
	//判断是否是平衡二叉树
	public boolean isBalancedTree(Node node){
		if(node == null){
			return true;
		}
		
		int balanceFactor = getBalanceFactor(node);
		if(Math.abs(balanceFactor) > 1){
			return false;
		} 
		
		return isBalancedTree(node.left) && isBalancedTree(node.right);
	}
	
	
	public class Node{
		private Node left;
		private Node right;
		private int element;
		private int height;
		
		public Node(int i){
			this.element = i;
			left = null;
			right = null;
			height = 1;
		}
	}
	
	public static void main(String[] args) {
		ALVTree alv = new ALVTree();
		
		for(int i = 0; i < 10;i ++){
			alv.add(i);
		}
		
		System.out.println(alv.isBalancedTree(alv.root));
		System.out.println(alv.getHight(alv.root.left));
		System.out.println(alv.getHight(alv.root.right));
		System.out.println(alv.getBalanceFactor(alv.root));
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值