悲战!蓝桥杯(第一天)-哈希表-

目录

1. 搜索树

2. 搜索

3. Map

4. Set

5. 哈希表


1. 搜索树

1. 概念

二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树。

  • 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
  • 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
  • 它的左右子树也分别为二叉搜索树

总结:二叉搜索树的左子树节点 < 父亲节点 < 右子树节点

2.操作

2.1 插入

public  void checkInsertLegal() throws insertException{
        throw new insertException("待插入的值在树中已存在");
    }
    public void insert(int val){
        TreeNode node = new TreeNode(val);
        if (root == null) {
            root = node;
            return;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        //寻找插入位置
        while (cur != null) {
            if (cur.val > val) {
                parent = cur;
                cur = cur.left;
            }else if (cur.val < val){
                parent = cur;
                cur = cur.right;
            }else {
                try {
                    checkInsertLegal();
                }
                catch (insertException e){
                    e.printStackTrace();
                }
                return;
            }
        }
        //插入
        if (parent.val > val) {
            parent.left = node;
        }
        else{
            parent.right = node;
        }
    }

2.2 查找

public boolean search(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val < key){
                cur = cur.right;
            }else if (cur.val > key){
                cur = cur.left;
            }else {
                return true;
            }
        }

2,3 删除(难)

 public void remove(int key) {
        TreeNode parent = null;
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val < key) {
                cur = cur.right;
            } else if (cur.val > key) {
                cur = cur.left;
            } else {
                //删除(三种情况)
                //removeNode()
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值