3.BST

核心逻辑

二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。二叉搜索树作为一种经典的数据结构,它既有链表的快速插入与删除操作的特点,又有数组快速查找的优势;所以应用十分广泛,例如在文件系统和数据库系统一般会采用这种数据结构进行高效率的排序与检索操作.

适用范围

  1. 插入一个数值
  2. 查询是否包含某个数值
  3. 删除某个数值

算法复杂度

和树深度强相关 O(log n)

package com.zby.calc;

import com.zby.calc.A_BinaryHeap.Student;
import com.zby.calc.B_LRUCache.Node;

/**
 * 
 * 二叉搜索树
 * 
 * @author zby
 * @time 2021/6/10 16:24
 * ****************************************
 */
public class C_BST {
    // 根节点
    private TreeNode<Integer, Student> root;
    /**
     *
     * @desc 删除
     ******************************************
     */
    public void remove(int iq) {
        if(root==null)
            return;
        TreeNode<Integer, Student> choseNode = root;
        TreeNode<Integer, Student> choseNodeParent = null;
        while (choseNode!=null) {
            if(choseNode.getKey()==iq)
                break;
            else if(choseNode.getKey()>iq) {
                if(choseNode.getLeft()==null)
                    return;
                choseNodeParent = choseNode;
                choseNode = choseNode.getLeft();
            } else {
                if(choseNode.getRight()==null)
                    return;
                choseNodeParent = choseNode;
                choseNode = choseNode.getRight();
            }
        }
        if(choseNode.getKey()!=iq||choseNodeParent==null)
            return;
        TreeNode<Integer, Student> replaceNode = getReplaceNode(choseNode);
        if(replaceNode==null) {
            if(choseNodeParent.getRight()==choseNode)
                choseNodeParent.setRight(null);
            if(choseNodeParent.getLeft()==choseNode)
                choseNodeParent.setLeft(null);
            return;
        } else {
            if(choseNodeParent.getRight()==choseNode)
                choseNodeParent.setRight(replaceNode);
            if(choseNodeParent.getLeft()==choseNode)
                choseNodeParent.setLeft(replaceNode);
        }
    }
    /**
     *
     * @desc 获取替换节点
     * 1.叶子节点  不用找了 直接替换
     * 2.左右节点只有一个 直接替换
     * 3.左右节点都有 找左子节点最大值
     ******************************************
     */
    private TreeNode<Integer, Student> getReplaceNode(TreeNode<Integer,Student> node) {
        if(node==null)
            return null;
        if(node.getLeft()==null&&node.getRight()==null)
            return null;
        if(node.getLeft()==null)
            return node.getRight();
        if(node.getRight()==null)
            return node.getLeft();
        node = node.getLeft();
        while(node!=null) {
            if(node.getRight()==null)
                return node;
            node = node.getRight();
        }
        return null;
    }
    /**
     *
     * @desc 插入数据
     ******************************************
     */
    public void add(Student v) {
        if (root==null) {
            root = new TreeNode<>(v.getIq(),v);
            return;
        }
        TreeNode<Integer, Student> choseNode = root;
        while (choseNode!=null) {
            if(choseNode.getValue().getIq()==v.getIq())
                return;
            else if(choseNode.getValue().getIq()>v.getIq()) {
                if(choseNode.getRight()==null) {
                    choseNode.setRight(new TreeNode<>(v.getIq(),v));
                    return;
                }
                choseNode = choseNode.getRight();
            } else {
                if(choseNode.getLeft()==null) {
                    choseNode.setLeft(new TreeNode<>(v.getIq(),v));
                    return;
                }
                choseNode = choseNode.getLeft();
            }
        }


    }
    /**
     *
     * @desc 获取节点
     ******************************************
     */
    public Student get(int iq) {
        TreeNode<Integer,Student> choseNode = root;
        while (choseNode!=null) {
            if(choseNode.getKey()==iq)
                return choseNode.getValue();
            else if(iq>choseNode.getKey()) {
                if(choseNode.right==null)
                    return null;
                choseNode = choseNode.right;
                continue;
            } else {
                if(choseNode.left==null)
                    return null;
                choseNode = choseNode.left;
                continue;
            }
        }
        return null;
    }


    public static void main(String[] args) {

    }
    /**
     * 
     * 树节点
     * 
     * @author zby
     * @time 2021/6/10 16:26
     * ****************************************
     */
    class TreeNode<K,V> {
        private TreeNode<K,V> left;
        private TreeNode<K,V> right;
        private K key;
        private V value;

        public void setLeft(TreeNode<K, V> left) {
            this.left = left;
        }

        public void setRight(TreeNode<K, V> right) {
            this.right = right;
        }
        public TreeNode<K, V> getLeft() {
            return left;
        }

        public TreeNode<K, V> getRight() {
            return right;
        }

        public K getKey() {
            return key;
        }

        public V getValue() {
            return value;
        }

        public TreeNode(K k, V v) {
            this.key = k;
            this.value = v;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
翻译 This is Elsevier's new document class for typeset journal articles, elsarticle.cls. It is now accepted for submitted articles, both in Elsevier's electronic submission system and elsewhere. Elsevier's previous document class for typeset articles, elsart.cls, is now over 10 years old. It has been replaced with this newly written document class elsarticle.cls, which has been developed for Elsevier by the leading TeX developer STM Document Engineering Pvt Ltd. elsarticle.cls is based upon the standard LaTeX document class article.cls. It uses natbib.sty for bibliographical references. Bugs and problems with elsarticle.cls may be reported to the developers of the class via elsarticle@stmdocs.in. The file manifest.txt provides a list of the files in the elsarticle bundle. The following are the main files available: - elsarticle.dtx, the dtx file - elsdoc.pdf, the user documentation - elsarticle-template-num.tex, template file for numerical citations - elsarticle-template-harv.tex, template file for name-year citations - elsarticle-template-num-names.tex, template file for numerical citations + new natbib option. Eg. Jones et al. [21] - elsarticle-num.bst, bibliographic style for numerical references - elsarticle-harv.bst, bibliographic style for name-year references - elsarticle-num-names.bst, bibliographic style for numerical referencces + new natbib option for citations. To extract elsarticle.cls from *.dtx: latex elsarticle.ins The documentation file is elsdoc.tex in the contrib directory. To compile it: 1. pdflatex elsdoc 2. pdflatex elsdoc 3. pdflatex elsdoc
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值