二叉树获取树的高度

public class AVLTreeDemo {

    public static void main(String[] args) {
        int[] arr = {4, 3, 6, 5, 7, 8};
        /*
                    4
                   / \
                  3   6
                     / \
                    5   7
                         \
                          8
         */
        AVLTree tree = new AVLTree();
        int len = arr.length;
        for (int i = 0; i < len; i++) {
            tree.add(new Node(arr[i]));
        }
        tree.infixOrder();
        System.out.println("\n树的高度:"+tree.getRoot().height());
        System.out.println("树的左子树高度:"+tree.getRoot().left.height());
        System.out.println("树的右子树高度:"+tree.getRoot().right.height());
        System.out.println("树的左右子树高度差的绝对值:" + tree.getRoot().differ());
    }

}

class AVLTree {
    private Node root;

    public Node getRoot() {
        return root;
    }

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

    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("二叉树为空,无法遍历~");
        }
    }

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


}

class Node {
    public int num;
    public Node left;
    public Node right;

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

    @Override
    public String toString() {
        return num + "";
    }

    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.print("=>" + num);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    /**
     * 返回以该节点为根节点的树的高度
     */
    public int height() {
        return Math.max(this.left == null ? 0 : this.left.height(),
                this.right == null ? 0 : this.right.height()) + 1;
    }

    /**
     * 返回左子树的高度
     */
    public int leftHeight() {
        if (this.left == null) {
            return 0;
        } else {
            return this.left.height();
        }
    }

    /**
     * 返回右子树的高度
     */
    public int rightHeight() {
        if (this.right == null) {
            return 0;
        } else {
            return this.right.height();
        }
    }

    /**
     * 返回左右子树高度的差
     */
    public int differ() {
        return Math.abs(leftHeight() - rightHeight());
    }

    /**
     * 添加新的节点【二叉排序树】
     */
    public void add(Node node) {
        if (node == null) {
            return;
        }
        if (node.num < this.num) {
            if (this.left != null) {
                this.left.add(node);
            } else {
                this.left = node;
            }
        } else {
            if (this.right != null) {
                this.right.add(node);
            } else {
                this.right = node;
            }
        }
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值