Java大数据平台开发 学习笔记(30)—— 平衡二叉查找树(AVL)的构建与(前序、中序、后序)遍历

一、数据结构与算法:


Step 1) 创建 平衡二叉排序树节点:

class Node {
    int value;
    Node left;
    Node right;

    public Node(int value){
        this.value = value;
    }
    
    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
}

Step 2) 创建 获取平衡二叉排序树的 (左子树、右子树)高度方法:

    public int liftHeight() {
        if (left == null) {
            return 0;
        }
        return left.height();
    }

    public int rightHeight() {
        if (right == null) {
            return 0;
        }
        return right.height();
    }

    public int height() {
        return Math.max((left == null ? 0 : left.height()), (right == null ? 0 : right.height())) + 1;
    }

Step 3) 创建 获取平衡二叉排序树的 (左旋转、右旋转)方法:

 	public void leftRotate(){
        Node newNode = new Node(value);
        newNode.left = left;
        newNode.right = right.left;
        value = right.value;
        right = right.right;
        left = newNode;
    }

    public void rightRotate(){
        Node newNode = new Node(value);
        newNode.right = right;
        newNode.left = left.right;
        value = left.value;
        left = left.left;
        right = newNode;
    }

Step 4) 创建 获取平衡二叉排序树的 (根节点、父节点)方法:

	public Node search(int value){
        if(value == this.value){
            return this;
        }else if(value < this.value){
            if(this.left == null) {
                return null;
            }
            return this.left.search(value);
        }else {
            if(this.right == null){
                return null;
            }
            return this.right.search(value);
        }
    }

    public Node seachParent(int value){
        if((this.left != null && this.left.value == value) ||
                (this.right != null && this.right.value == value)){
            return this;
        }else{
            if((value < this.value) && (this.left != null)){
                return this.left.seachParent(value);
            }else if((value >= this.value) && (this.right != null)){
                return this.right.seachParent(value);
            }else{
                return null;
            }
        }
    }

Step 5) 创建 二叉排序树 添加元素方法:

public void add(Node node){
        if(node == null){
            return;
        }
        if(node.value < this.value){
            if(this.left == null){
                this.left = node;
            }else {
                this.left.add(node);
            }
        }else {
            if(this.right == null){
                this.right = node;
            }else {
                this.right.add(node);
            }
        }

        if(rightHeight() - liftHeight() > 1){
            if((right != null) && (right.liftHeight() < right.rightHeight())){
                right.rightRotate();
            }
            leftRotate();
        }
        if(liftHeight() - rightHeight() > 1){
            if((left != null) && (left.rightHeight() > left.liftHeight())){
                left.leftRotate();
            }
            rightRotate();
        }
    }

Step 6) 提供 二叉树节点遍历方式:

	//前序遍历
    public void preNode(){
        System.out.println(this);
        if(this.left != null){
            this.left.preNode();
        }
        if(this.right != null){
            this.right.preNode();
        }
    }

    //中序遍历
    public void infixNode(){
        if(this.left != null){
            this.left.infixNode();
        }
        System.out.println(this);
        if(this.right != null){
            this.right.infixNode();
        }
    }

    //后序遍历
    public void postNode(){
        if(this.left != null){
            this.left.postNode();
        }
        if(this.right != null){
            this.right.postNode();
        }
        System.out.println(this);
    }

Step 7) 构建 根节点:

class AVLTree {
    private Node root;

    public Node getRoot() {
        return root;
    }

    public void setRoot(Node root) {
        this.root = root;
    }

    public Node seach(int value){
        if(root == null){
            return null;
        }else {
            return root.search(value);
        }
    }

    public Node seachParent(int value){
        if(root == null){
            return null;
        }else{
            return root.seachParent(value);
        }
    }

    public int delRightTreeMin(Node node){
        Node target = node;
        while (target.left != null){
            target = target.left;
        }
        delNode(target.value);
        return target.value;
    }

    public void delNode(int value){
        if(root == null){
            return;
        }else {
            Node targetNode = seach(value);
            if(targetNode == null){
                return;
            }
            if((root.left == null) && (root.right == null)){
                root = null;
                return;
            }
            Node parent = seachParent(value);
            if((targetNode.left == null) && (targetNode.right == null)){
                if((parent.left != null) && (parent.left.value == value)){
                    parent.left = null;
                }else if((parent.right != null) && (parent.right.value == value)) {
                    parent.right = null;
                }
            }else if((targetNode.left != null) && (targetNode.right != null)){
                int minValue = delRightTreeMin(targetNode.right);
                targetNode.value = minValue;
            }else{
                if(targetNode.left != null){
                    if(parent != null) {
                        if (parent.left.value == value) {
                            parent.left = targetNode.left;
                        } else {
                            parent.right = targetNode.left;
                        }
                    }else{
                        root = targetNode.left;
                    }
                }else{
                    if(parent != null) {
                        if(parent.left.value == value){
                            parent.left = targetNode.right;
                        }else{
                            parent.right = targetNode.right;
                        }
                    }else{
                        root = targetNode.right;
                    }
                }
            }
        }
    }

    public void add(Node node){
        if (root == null){
            root = node;
        }else {
            root.add(node);
        }
    }

    public void preOrder(){
        if(this.root != null){
            this.root.preNode();
        }else{
            System.out.println("找不到根节点,无法遍历 !!");
        }
    }

    public void infixOrder(){
        if(this.root != null){
            this.root.infixNode();
        }else{
            System.out.println("找不到根节点,无法遍历 !!");
        }
    }

    public void postOrder(){
        if(this.root != null){
            this.root.postNode();
        }else{
            System.out.println("找不到根节点,无法遍历 !!");
        }
    }
}

Step 8) main 方法:

public static void main(String[] args) {

     int[] arr = {10, 11, 7, 6, 8, 9};

     AVLTree avlTree = new AVLTree();

     for (int i=0; i < arr.length; i++){
         avlTree.add(new Node(arr[i]));
     }

     System.out.println("树的高度:"+avlTree.getRoot().height());
     System.out.println("左子树的高度:"+avlTree.getRoot().liftHeight());
     System.out.println("右子树的高度:"+avlTree.getRoot().rightHeight());
     System.out.println("当前根节点:"+avlTree.getRoot() + "\n");

     System.out.println("前序遍历");
     avlTree.preOrder();

     System.out.println("中序遍历");
     avlTree.infixOrder();

     System.out.println("后序遍历");
     avlTree.postOrder();
}


• 由 ChiKong_Tam 写于 2020 年 9 月 23 日

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值