IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

手写数据结构-二分搜索树

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

1、二分搜索树基础

1、为什么要有树结构?

将数据使用树结构存储后,出奇的高效。

常见的树结构:二分搜索树/平衡二叉树(AVL 红黑树)/堆/优先队列..等

2、二叉树特点

  • 二叉树具有唯一根节点
  • 二叉树每个节点最多有两个孩子,每个节点最多有一个父亲
  • 二叉树具有天然的递归结构
  • 二叉树不一定是满的
  • 一个节点也是二叉树 NULL也是一颗二叉树

3、二分搜索树 (Binary Search Tree)

  • 二分搜索树是二叉树
  • 每个节点的值大于左节点的值,小于右节点的值
  • 每颗子树也是一个二分搜索树
  • 存储的元素必须要有可比性

2、手动实现二分搜索树及复杂度分析

package com.tc.javabase.datastructure.tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Classname BST
 * @Description  二分搜索树
 * @Date 2020/7/19 18:37
 * @Created by zhangtianci
 */
public class BST<E extends Comparable> {
    private class Node{
        public E e;
        public Node left,right;

        public Node(E e, Node left, Node right) {
            this.e = e;
            this.left = left;
            this.right = right;
        }

        public Node(E e) {
            this(e,null,null);
        }

        public Node(){
            this(null,null,null);
        }
    }

    private Node root;  //根节点
    private int size;

    public BST(){
        root = null;
        size = 0;
    }

    public int getSize(){
        return size;
    }

    public boolean isEmpty(){
        return size == 0 ? true : false;
    }

    /**
     * 增加
     * 1.新增一个节点
     */

    /**
     * 新增一个节点
     * @param e
     */
    public void add(E e){
        add(root,e);
    }

    // 向以node为根的二分搜索树中插入元素e,递归算法
    // 返回插入新节点后二分搜索树的根
    private Node add(Node node,E e){
        if (node == null){
            size++;
            return new Node(e);
        }

        if (e.compareTo(node.e) > 0){
            node.right = add(node.right,e);
        }else if (e.compareTo(node.e) < 0){
            node.left = add(node.left,e);
        }

        return node;
    }


    /**
     * 查询
     * 1.查询最小节点
     * 2.查询最大节点
     * 3.是否包含指定元素节点
     */

    /**
     * 查询最小节点
     * @return
     */
    public E getMin(){
        if(size == 0){
            throw new IllegalArgumentException("二分搜索树为空!");
        }

        return getMin(root).e;
    }

    //返回以node为根的二分搜索树的最小值所在的节点
    private Node getMin(Node node){
        if (node.left == null){
            return node;
        }
        return getMin(node.left);
    }
    /**
     * 查询最大节点
     * @return
     */
    public E getMax(){
        if(size == 0){
            throw new IllegalArgumentException("二分搜索树为空!");
        }

        return getMax(root).e;
    }

    //返回以node为根的二分搜索树的最大值所在的节点
    private Node getMax(Node node){
        if (node.right == null){
            return node;
        }
        return getMin(node.right);
    }

    /**
     * 是否包含指定元素节点
     * @param e
     * @return
     */
    public boolean contains(E e){
        return contains(root,e);
    }

    // 看以node为根的二分搜索树中是否包含元素e, 递归算法
    private boolean contains(Node node,E e){
        if (node == null){
            return false;
        }

        if (node.e.compareTo(e) == 0){
            return true;
        }

        if (e.compareTo(node.e) < 0){
             return contains(node.left,e);
        }else {
            return contains(node.right,e);
        }
    }

    /**
     * 删除
     * 1.删除最大节点
     * 2.删除最小节点
     * 3.删除指定元素节点
     */

    /**
     * 删除最大节点
     * @return
     */
    public E removeMax(){
        E ret = removeMax();
        root = removeMax(root);
        return ret;
    }

    // 删除掉以node为根的二分搜索树中的最大节点
    // 返回删除节点后新的二分搜索树的根
    private Node removeMax(Node node){
        if(node.right == null){
            Node leftNode = node.left;
            node.left = null;
            size --;
            return leftNode;
        }

        node.right = removeMax(node.right);
        return node;
    }

    /**
     * 删除最小节点
     * @return
     */
    public E removeMin(){
        if (isEmpty()){
            throw new IllegalArgumentException("BST 为空!");
        }
        E retn = getMin();
        root = removeMin(root);
        return retn;
    }

    //删除以node为根节点的最小节点
    private Node removeMin(Node node){
        if(node.left == null){
            Node rightNode = node.right;
            node.right = null;
            size --;
            return rightNode;
        }

        node.left = removeMin(node.left);
        return node;
    }

    /**
     * 删除指定元素节点
     * @param e
     */
    public void remove(E e){
        root = remove(root, e);
    }

    // 删除掉以node为根的二分搜索树中值为e的节点, 递归算法
    // 返回删除节点后新的二分搜索树的根
    private Node remove(Node node, E e){

        if( node == null )
            return null;

        if( e.compareTo(node.e) < 0 ){
            node.left = remove(node.left , e);
            return node;
        }
        else if(e.compareTo(node.e) > 0 ){
            node.right = remove(node.right, e);
            return node;
        }
        else{   // e.compareTo(node.e) == 0

            // 待删除节点左子树为空的情况
            if(node.left == null){
                Node rightNode = node.right;
                node.right = null;
                size --;
                return rightNode;
            }

            // 待删除节点右子树为空的情况
            if(node.right == null){
                Node leftNode = node.left;
                node.left = null;
                size --;
                return leftNode;
            }

            // 待删除节点左右子树均不为空的情况

            // 找到比待删除节点大的最小节点, 即待删除节点右子树的最小节点
            // 用这个节点顶替待删除节点的位置
            Node successor = getMin(node.right);
            successor.right = removeMin(node.right);
            successor.left = node.left;

            node.left = node.right = null;

            return successor;
        }
    }



    /**
     * 前序遍历
     * 中序遍历(从小到大)
     * 后续遍历(先处理叶子节点再处理根节点,适用场景 回收内存时 先回收叶子节点的内存再回收根节点的内存)
     * 层序遍历
     */


    /**
     * 前序遍历
     */
    public void preOrder(){
        preOrder(root);
    }

    // 前序遍历以node为根的二分搜索树, 递归算法
    private void preOrder(Node node){

        if(node == null)
            return;

        System.out.println(node.e);
        preOrder(node.left);
        preOrder(node.right);
    }

    /**
     * 中序遍历(从小到大)
     */
    public void inOrder(){
        inOrder(root);
    }

    // 中序遍历以node为根的二分搜索树, 递归算法
    private void inOrder(Node node){

        if(node == null)
            return;

        inOrder(node.left);
        System.out.println(node.e);
        inOrder(node.right);
    }

    /**
     * 后续遍历
     */
    public void postOrder(){
        postOrder(root);
    }

    // 后序遍历以node为根的二分搜索树, 递归算法
    private void postOrder(Node node){

        if(node == null)
            return;

        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.e);
    }

    /**
     * 层序遍历
     */
    public void levelOrder(){
        Queue<Node> q = new LinkedList<>();
        q.add(root);
        while(!q.isEmpty()){
            Node cur = q.remove();
            System.out.println(cur.e);

            if(cur.left != null)
                q.add(cur.left);
            if(cur.right != null)
                q.add(cur.right);
        }
    }

}

文章永久链接:https://tech.souyunku.com/?p=38385


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(62) 打赏



未经允许不得转载:搜云库技术团队 » 手写数据结构-二分搜索树

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367