二叉树应用 —— 平衡二叉树实现

二叉树,可用于快速搜索。
平衡二叉树,可以有效预防出现持续单边的二叉树,可有效避免因此原因而造成的搜索效率低下(单边的二叉树则相当于线性查找)

实现平衡二叉树的构建,以及二叉树的显示。

代码如下:

/** 
 * @author 作者 : Cactus
 * @version 创建时间:2018-3-9 下午04:20:48 
 */
class AVLTree{
    private int v;
    private AVLTree l;
    private AVLTree r;
    private int balance = 0;
    public AVLTree(int v){
        this.v = v;
    }
    public int getHeight(){
        int h = 2;
        int hl = l == null ? 0 : l.getHeight();
        int hr = r == null ? 0 : r.getHeight();
        return h + Math.max(hl, hr);
    }
    public int getWidth(){
        int w = ("" + v).length();
        if(l != null){
            w += l.getWidth();
        }
        if(r != null){
            w += r.getWidth();
        }
        return w;
    }
    private void caculateBalance(){
        int lh = l == null ? 0 : l.getHeight();
        int rh = r == null ? 0 : r.getHeight();
        balance = lh - rh;
    }
    public int getBalance(){
        return balance;
    }
    private AVLTree adjustLL(){
        AVLTree root = l;
        l = root.r;
        root.r = this;
        return root;
    }
    private AVLTree adjustRR(){
        AVLTree root = r;
        r = root.l;
        root.l = this;
        return root;
    }
    private AVLTree adjustLR(){
        l = l.adjustRR();
        return adjustLL();
    }
    private AVLTree adjustRL(){
        r = r.adjustLL();
        return adjustRR();
    }

    public AVLTree add(AVLTree the){
        AVLTree root = this;
        if(the.v < v){
            if(l == null){
                l =the;
            }else{
                l = l.add(the);
            }
        }
        if(the.v > v){
            if( r == null){
                r = the;
            }else{
                r = r.add(the);
            }
        }
        caculateBalance();
        if(balance > 2){
            if(l.getBalance() >0){
                root = adjustLL();
            }else{
                root = adjustLR();
            }
        }
        if(balance < -2){
            if(r.getBalance() < 0){
                root = adjustRR();
            }else{
                root = adjustRL();
            }
        }
        caculateBalance();
        return root;
    }
    private int getRootPos(int x){
        return l == null ? x : x + l.getWidth();
    }
    private void printInBuf(char[][] buf, int x, int y){
        String sv = ""+v;
        int p1 = l == null ? x : l.getRootPos(x);
        int p2 = getRootPos(x);
        int p3 = r == null ? p2 : r.getRootPos(p2 + sv.length());
        buf[y][p2] = '|';
        for(int i = p1; i <= p3; i++){
            buf[y + 1][i] = '-';
        }
        for(int i = 0; i < sv.length(); i++){
            buf[y + 1][p2 + i] = sv.charAt(i);
        }
        if(p1 < p2){
            buf[y + 1][p1] = '/';
        }
        if(p3 > p2){
            buf[y + 1][p3] = '\\';
        }
        if(l != null){
            l.printInBuf(buf, x, y + 2);
        }
        if(r != null){
            r.printInBuf(buf, p2 + sv.length(), y + 2);
        }
    }
    private void showBuf(char[][] buf){
        for(int i = 0; i < buf.length; i++){
            for(int j = 0; j < buf[i].length; j++){
                System.out.print(buf[i][j] == 0 ? ' ' : buf[i][j]);
            }
            System.out.println();
        }
    }
    public void show(){
        char[][] buf = new char[getHeight()][getWidth()];
        printInBuf(buf,0,0);
        showBuf(buf);
    }

}
public class Main {
    public static void main(String[] args){
        AVLTree root = new AVLTree(80);
        root = root.add(new AVLTree(70));
        root = root.add(new AVLTree(60));
        root = root.add(new AVLTree(50));
        root = root.add(new AVLTree(40));
        root = root.add(new AVLTree(30));
        root = root.add(new AVLTree(20));
        root = root.add(new AVLTree(10));
        root = root.add(new AVLTree(8));
        root = root.add(new AVLTree(7));
        root.show();
    }

}

运行结果如下:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喜鹊先生Richard

随缘~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值