平衡二叉树 记录

JAVA实现,以及JFrame 结果展示

package com.example.fndemo.tree;

import java.util.Stack;


/**
 * 平衡二叉树,旋转
 * @author zq
 * @date 2020-09-08  18:20
 */
public class AVLTree {
   
	Node<Integer> rootNode;
	public static void main(String[] args) {
   
		Integer[] datas = {
   2, 1, 0, 3, 4, 5, 6, 9, 8, 7};
		AVLTree avlTree = new AVLTree();
		for (Integer data : datas) {
   
			avlTree.insert(data);
		}
		avlTree.midTraversing();
		System.out.println();
		// 窗口展示
		DrawTree drawTree = new DrawTree(avlTree.rootNode);
	}
	public void insert(Integer data) {
   
		if (null == rootNode) {
   
			rootNode = new Node<Integer>(data);
			return;
		}
		Node insert = new Node<Integer>(data).insert(rootNode);
		this.adjust(insert);
		
		System.out.println("是否插入成功:" + (null != insert.getParent()) + " 当前key:" + insert.getKey() + " 父亲key:" + (null == insert.getParent() ? null : insert.getParent().getKey()));
	}
	
	public Node<Integer> getRootNode() {
   
		return rootNode;
	}
	
	//平衡检查以及调整
	public  void adjust(Node leafNode){
   
		//回溯四层,判断是否发生失衡
		Node current = leafNode;
		int index=1;
		while (current!=null&&index<5){
   
			if (current.getBalance() > 1) {
   
				if (current.getLeftChild().getBalance() < 0) {
   
					rotateLeft(current.getLeftChild());
				}
				rotateRight(current);
				return;
			}
			if (current.getBalance() < -1) {
   
				if (current.getRightChild().getBalance() > 0) {
   
					rotateRight(current.getRightChild());
				}
				rotateLeft(current);
				return;
			}
			current=current.getParent();
			index++;
		}
		
	}
	
	//当前节点左旋
	public void rotateLeft(Node  currentNode){
   
		Node parent = currentNode.getParent();
		Node rightChild = currentNode.getRightChild();
		Node rightChildLeftChild = rightChild.getLeftChild();
		
		currentNode.setParent(rightChild);
		currentNode.setRightChild(rightChildLeftChild);
		
		rightChild.setParent(parent);
		rightChild.setLeftChild(currentNode);
		
		if (parent != null) {
   
			if (parent.getRightChild().equals(currentNode)) {
   
				parent.setRightChild(rightChild);
			}
			if (parent.getLeftChild().equals(currentNode)) {
   
				parent.setRightChild(rightChild);
			}
		}else{
   
			rootNode=rightChild;
		}
		if (null != rightChildLeftChild) {
   
			rightChildLeftChild.setParent(currentNode);
		}
		
		
	}
	
	//当前节点右旋
	public void rotateRight(Node  currentNode){
   
		Node parent = currentNode.getParent();
		Node leftChild = currentNode.getLeftChild();
		Node leftChildRightChild = leftChild.getRightChild();
		leftChild.setRightChild(currentNode);
		leftChild.setParent(parent);
		currentNode.setParent(leftChild);
		currentNode.setLeftChild(leftChildRightChild);
		
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值