有序二叉排序树

1.实现方式


/**
 * @Author QingHao
 * @Date: 2022/05/30/ 22:36
 * @Describe 有序二叉排序树
 */
public class BinarySearchTree<E extends Comparable<E>> {

    public Node root;

    /**
     * 添加节点
     *
     * @param e
     * @return
     */
    public boolean add(E e) {
        if (root == null) {
            this.root = new Node(e);
            return true;
        }
        return root.append(e);
    }

    /**
     * 输出所有数据
     *
     * @return
     */
    public String toString() {
        if (root == null) {
            return "[]";
        }
        StringBuilder stringBuilder = new StringBuilder("[");
        return root.midOrder(stringBuilder).replace(stringBuilder.length() - 1, stringBuilder.length() - 1, "]").toString();
    }

    /**
     * 搜索数据
     *
     * @param e
     * @return
     */
    public Node get(E e) {
        if (root == null) {
            return null;
        }
        return root.isDestNode(e);
    }

    public class Node {
        private E ele;
        private Node left;
        private Node right;

        @Override
        public String toString() {
            return "Node{" +
                    "ele=" + ele +
                    ", left=" + left +
                    ", right=" + right +
                    '}';
        }

        /**
         * 添加元素,添加时进行判断小于父节点的话就放左边放,大于的话放右边
         *
         * @param e
         * @return
         */
        public boolean append(E e) {
            if (e.compareTo(ele) == 0) {
                return false;
            } else if (e.compareTo(ele) < 0) {
                if (left == null) {
                    left = new Node(e);
                    return true;
                }
                return left.append(e);
            } else {
                if (right == null) {
                    right = new Node(e);
                    return true;
                }
                return right.append(e);
            }
        }

        public Node(E e) {
            ele = e;
        }

        /**
         * 进行中序遍历,接值进行拼接返回
         *
         * @param str
         * @return
         */
        public StringBuilder midOrder(StringBuilder str) {
            if (left != null) {
                left.midOrder(str);
            }
            str.append(ele).append(",");

            if (right != null) {
                right.midOrder(str);
            }

            return str;
        }

        /**
         * 判断当前节点是否为目标节点
         *
         * @param e
         * @return
         */
        public Node isDestNode(E e) {

            if (e.compareTo(ele) == 0) {
                return this;
            } else if (e.compareTo(ele) < 0) {
                if (left == null)
                    return null;
                return left.isDestNode(e);
            } else {
                if (right == null)
                    return null;
                return right.isDestNode(e);
            }

        }
    }

}

2.添加数据测试


/**
 * @Author QingHao
 * @Date: 2022/05/30/ 23:02
 * @Describe
 */
public class BinarySearchTreeRun {
    public static void main(String[] args) {

        BinarySearchTree<String> binarySearchTree = new BinarySearchTree<>();
        //添加数据
        binarySearchTree.add("t");
        binarySearchTree.add("b");
        binarySearchTree.add("d");
        binarySearchTree.add("a");
        binarySearchTree.add("e");
        binarySearchTree.add("c");

        //打印输出数据
        System.out.println(binarySearchTree.toString());

        //搜索数据
        System.out.println(binarySearchTree.get("b"));
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐