用平衡二叉搜索树解决硬木种类问题

x33g5p2x  于2022-08-17 转载在 其他  
字(3.0k)|赞(0)|评价(0)|浏览(272)

一 问题描述

二 分析

使用平衡二叉搜索树,先将每个单词都插入平衡二叉树中,每出现一次,则修改该单词所在节点 cnt;最后通过中序遍历输出结果。效率高于二叉搜索树。

三 代码

package com.platform.modules.alg.alglib.poj2418;

public class Poj2418 {
    public String output = "";
    public int sum = 0;

    public String cal(String input) {
        AVLTree avlTree = new AVLTree();
        String[] line = input.split("\n");

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

        avlTree.midprint(avlTree.getRoot());
        return output;
    }

    class AVLTree {
        // 根节点
        private Node root;

        public Node getRoot() {
            return root;
        }

        /**
         * 功能描述:添加结点
         *
         * @param node 节点
         * @author cakin
         * @date 2021/3/22
         */
        public void add(Node node) {
            if (root == null) {
                node.cnt = 1;
                root = node; // 如果root为空则直接让root指向node
            } else {
                root.add(node);
            }
        }

        // 中序遍历
        public void midprint(Node root) { // 中序遍历
            if (root != null) {
                midprint(root.left);
                output += root.value;
                output += "\t";
                output += String.format("%.4f", ((double) root.cnt / (double) sum) * 100);
                output += "\n";
                midprint(root.right);
            }
        }
    }

    /**
     * @className: Node
     * @description: 节点
     * @date: 2021/3/22
     * @author: cakin
     */
    class Node {
        // 节点值
        String value;
        // 左子树根节点
        Node left;
        // 右子树根节点
        Node right;
        // 节点计数
        int cnt;

        public Node(String value) {
            this.value = value;
            this.cnt = 1;
        }

        /**
         * 功能描述:返回左子树的高度
         *
         * @author cakin
         * @date 2021/3/27
         */
        public int leftHeight() {
            if (left == null) {
                return 0;
            }
            return left.height();
        }

        /**
         * 功能描述:返回右子树的高度
         *
         * @author cakin
         * @date 2021/3/27
         */
        public int rightHeight() {
            if (right == null) {
                return 0;
            }
            return right.height();
        }

        /**
         * 功能描述:返回以该结点为根结点的树的高度
         *
         * @author cakin
         * @date 2021/3/27
         */
        public int height() {
            return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
        }

        /**
         * 功能描述:左旋转方法
         *
         * @author cakin
         * @date 2021/3/27
         */
        private void leftRotate() {
            // 创建新的结点,值为当前根结点的值
            Node newNode = new Node(value);
            // 把新的结点的左子树设置成当前结点的左子树
            newNode.left = left;
            // 把新节点的右子树设置为当前节点右子树的左子树
            newNode.right = right.left;
            // 把当前节点的值设置为右子节点的值
            value = right.value;
            // 把当前节点的右子树设置为右子树的右子树
            right = right.right;
            // 把当前节点的左子树设置为新节点
            left = newNode;
        }

        /**
         * 功能描述:右旋转
         *
         * @author cakin
         * @date 2021/3/27
         */
        private void rightRotate() {
            Node newNode = new Node(value);
            newNode.right = right;
            newNode.left = left.right;
            value = left.value;
            left = left.left;
            right = newNode;
        }

        /**
         * 功能描述:添加节点到平衡二叉树
         *
         * @param node 节点
         * @author cakin
         * @date 2021/3/22
         */
        public void add(Node node) {
            if (node == null) {
                return;
            }

            // 传入的结点的值小于当前子树的根结点的值
            if (node.value.compareTo(this.value) < 0) {
                // 当前结点左子树根结点为null
                if (this.left == null) {
                    node.cnt = 1;
                    this.left = node;
                } else {
                    // 递归的向左子树添加
                    this.left.add(node);
                }
            } else if (node.value.compareTo(this.value) > 0) { // 传入的结点的值大于当前子树的根结点的值
                if (this.right == null) {
                    node.cnt = 1;
                    this.right = node;
                } else {
                    // 递归的向右子树添加
                    this.right.add(node);
                }
            } else {
                this.cnt++;
                return;
            }

            // 当添加完一个结点后,如果(右子树的高度-左子树的高度) > 1 , 进行左旋转
            if (rightHeight() - leftHeight() > 1) {
                // 左旋转
                // leftRotate();
                // 如果它的右子树的左子树的高度大于它的右子树的右子树的高度
                if (right != null && right.leftHeight() > right.rightHeight()) {
                    // 先对右子结点进行右旋转
                    right.rightRotate();
                    // 然后再对当前结点进行左旋转
                    leftRotate();
                } else {
                    // 直接进行左旋转
                    leftRotate();
                }
                return;
            }

            // 当添加完一个结点后,如果(左子树的高度 - 右子树的高度) > 1, 进行左旋转
            if (leftHeight() - rightHeight() > 1) {
                // rightRotate();
                // 如果它的左子树的右子树高度大于它的左子树的高度
                if (left != null && left.rightHeight() > left.leftHeight()) {
                    // 先对当前结点的左子结点进行左旋转
                    left.leftRotate();
                    // 再对当前结点进行右旋转
                    rightRotate();
                } else {
                    // 直接进行右旋转
                    rightRotate();
                }
            }
        }
    }
}

四 测试

相关文章

微信公众号

最新文章

更多