平衡二叉树左旋和右旋的实现

思路:在增加数据时,保持此树为顺序存储二叉树,然后判断此二叉树是否为平衡二叉树,如果bf >1 就右旋 bf<-1左旋。但是还有一种情况,需要双选,暂时还没实现。
       
class AvlTree {

    Node root;

    /**
     * 增加数值
     * @param data
     */
    public void insert(Integer data) {
        if (root == null) {
            root = new Node(data);
        } else {
            insert(data, null, root, false);
        }
        //二叉树如果失去平衡
        if (this.bf() > 1) {//右旋 bnm,./
            rightRotate();
        }
        if (this.bf() < -1) {//右旋 bnm,./
            leftRotate();
        }
    }

    public void rightRotate() {
        Node tmp = root;
        root = root.left;
        Node tmp1 = root.right;
        root.right = tmp;
        root.right.left = tmp1;
    }

    public void leftRotate() {
        Node tmp = root;
        root = root.right;
        Node tmp1 = root.left;
        root.left = tmp;
        root.left.right = tmp1;
    }

    public void insert(Integer data, Node pre, Node now, boolean isLeft) {
        if (now == null) {
            if (isLeft) {
                Node node = new Node(data);
                node.setParentLeft(true);
                node.parent = pre;
                pre.setLeft(node);
            } else {
                Node node = new Node(data);
                node.setParentRright(true);
                node.parent = pre;
                pre.setRight(node);
            }
            return;
        }
        if (now.data > data) {
            insert(data, now, now.getLeft(), true);
        }

        if (now.data < data) {
            insert(data, now, now.getRight(), false);
        }
    }

    /**
     * 获取树的高度
     * @return
     */
    public int height() {
        return height(root);
    }

    private int height(Node root) {
        if (root == null) {
            return 0;
        }
        return Math.max(height(root.left), height(root.right)) + 1;
    }

    /**
     * 是否为平衡二叉树
     * @return
     */
    public boolean isBalance() {
        return isBalance(root);
    }
    public boolean isBalance(Node root) {
        if (root == null) {
            return true;
        }
        if (Math.abs(height(root.left) - height(root.right)) > 1) {
            return false;
        }
        return isBalance(root.left) && isBalance(root.right);
    }

    //>1 右旋 <-1左旋
    public int bf(Node root) {
        if (root == null) {
            return 999;
        }
        return height(root.left) - height(root.right);
    }

    //>1 右旋 <-1左旋
    public int bf() {
        return bf(root);
    }
}

class Node {
    //节点数值
    int data;
    //左节点
    Node left;
    //右节点
    Node right;
    //父节点
    Node parent;
    //位于父节点的左边
    boolean isParentLeft;
    //位于父节点的右边
    boolean isParentRright;

    public Node(int data) {
        this.data = data;
    }

    public Node getLeft() {
        return left;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public Node getRight() {
        return right;
    }

    public void setRight(Node right) {
        this.right = right;
    }

    public Node getParent() {
        return parent;
    }

    public void setParent(Node parent) {
        this.parent = parent;
    }

    public boolean isParentLeft() {
        return isParentLeft;
    }

    public void setParentLeft(boolean parentLeft) {
        isParentLeft = parentLeft;
    }

    public boolean isParentRright() {
        return isParentRright;
    }

    public void setParentRright(boolean parentRright) {
        isParentRright = parentRright;
    }

}
public class Demo1 {
    public static void main(String[] args) {
        AvlTree avlTree = new AvlTree();
        /*avlTree.insert(4);
        avlTree.insert(2);
        avlTree.insert(5);
        avlTree.insert(1);
        avlTree.insert(3);
        avlTree.insert(0);*/
        avlTree.insert(4);
        avlTree.insert(2);
        avlTree.insert(9);
        avlTree.insert(8);
        avlTree.insert(11);
        avlTree.insert(12);

        System.out.println(avlTree.bf());
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值