ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

完整代码示例-Java实现平衡二叉树的创建,遍历以及旋转

2021-05-15 09:59:23  阅读:201  来源: 互联网

标签:null Java 示例 AVLTreeNode rightNode 二叉树 leftNode root public


结点类:

package DataStrcture.avltreedemo;

public class AVLTreeNode {
    public AVLTreeNode leftNode;
    public AVLTreeNode rightNode;
    public int value;

    //左旋转和右旋转
    public void leftRotation() {
        AVLTreeNode newNode = new AVLTreeNode(this.value);
        newNode.leftNode = this.leftNode;
        newNode.rightNode = this.rightNode.leftNode;
        this.value = this.rightNode.value;
        this.rightNode = this.rightNode.rightNode;
        this.leftNode = newNode;
    }

    public void rightRotation() {
        AVLTreeNode newNode = new AVLTreeNode(this.value);
        newNode.leftNode = this.leftNode.rightNode;
        newNode.rightNode = this.rightNode;
        this.value = this.leftNode.value;
        this.leftNode = this.leftNode.leftNode;
        this.rightNode = newNode;
    }

    //得到二叉树的高度, 左子树的高度, 右子树的高度
    public int height() {
        return Math.max(leftNode == null ? 0 : leftNode.height(),
                rightNode == null ? 0 : rightNode.height()) + 1;
    }

    public int leftHeight() {
        if (this.leftNode != null)
            return this.leftNode.height();
        return 0;
    }

    public int rightHeight() {
        if (this.rightNode != null)
            return this.rightNode.height();
        return 0;
    }

    //二叉树的创建(添加结点, 构建关系)
    public void addNode(AVLTreeNode node) {
        //左子树
        if (node.value <= this.value) {
            if (this.leftNode == null) {
                this.leftNode = node;
            } else {
                this.leftNode.addNode(node);
            }
            //右子树
        } else {
            if (this.rightNode == null) {
                this.rightNode = node;
            } else {
                this.rightNode.addNode(node);
            }
        }
    }

    //中序遍历
    public void midOrder() {
        if (this.leftNode != null) this.leftNode.midOrder();

        System.out.println(this);

        if (this.rightNode != null) this.rightNode.midOrder();
    }

    //构造器, toString
    public AVLTreeNode(int val) {
        this.value = val;
    }

    public String toString() {
        return "Node{ value=" + value + "}";
    }
}

平衡二叉树类:

package DataStrcture.avltreedemo;

public class AVLTreeDemo {
    //初始化根节点
    private AVLTreeNode root;

    public AVLTreeDemo(AVLTreeNode node) {
        this.root = node;
    }

    //测试方法
    public static void main(String[] args) {
        int arr[] = {10,9,11,6,5,7,8};
        AVLTreeDemo tree = new AVLTreeDemo(null);
        for (int x : arr) {
            tree.add(new AVLTreeNode(x));
        }
        tree.midOrder();
        System.out.println("平衡二叉树左子树的高度为: "+tree.root.leftHeight());
        System.out.println("平衡二叉树右子树的高度为: "+tree.root.rightHeight());

    }

    //遍历二叉树
    public void midOrder() {
        if (root == null) {
            System.out.println("二叉树为空, 遍历失败");
        } else {
            root.midOrder();
        }
    }

    //添加结点到二叉树
    public void add(AVLTreeNode node) {
        if (root == null) {
            root = node;
        } else {
            root.addNode(node);
            //左旋转
            if (root.leftHeight() - root.rightHeight() > 1) {
                if (root.leftNode.rightHeight() - root.leftNode.leftHeight() > 0) {
                    root.leftNode.leftRotation();
                }
                //右旋转
                root.rightRotation();
                return;
            }
            if (root.rightHeight() - root.leftHeight() > 1) {
                    if (root.rightNode.leftHeight() - root.rightNode.rightHeight() > 0) {
                        root.rightNode.rightRotation();
                    }
                    root.leftRotation();

                return;
            }
        }
    }
}

在这里插入图片描述

标签:null,Java,示例,AVLTreeNode,rightNode,二叉树,leftNode,root,public
来源: https://blog.csdn.net/nmsLLCSDN/article/details/116834229

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有