【数据结构】——搜索树

1.1 概念

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

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

1.2 二叉搜索树的实现

二叉树定义:

static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val){
            this.val = val;
        }
    }

    public TreeNode root;

查找元素:

    /**
     * 查找key
     * @param key
     * @return 找到返回地址,否则返回null
     */
    public TreeNode 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 cur;
            }
        }
        return null;
    }

插入元素:

    /**
     * 插入元素
     * @param key
     * @return
     */
    public boolean insert(int key){
        TreeNode node = new TreeNode(key);
        if (root == null){
            root = node;
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null){
            if (cur.val < key){
                parent = cur;
                cur = cur.right;
            }else if (cur.val > key){
                parent = cur;
                cur = cur.left;
            }else {
                //存在相同的元素,不能插入成功
                return false;
            }
        }
        //
        if (parent.val < key){
            parent.right = node;
        }else {
            parent.left = node;
        }
        return true;
    }

删除关键字为key的元素:

    /**
     * 插入元素
     * @param key
     * @return
     */
    public boolean insert(int key){
        TreeNode node = new TreeNode(key);
        if (root == null){
            root = node;
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null){
            if (cur.val < key){
                parent = cur;
                cur = cur.right;
            }else if (cur.val > key){
                parent = cur;
                cur = cur.left;
            }else {
                //存在相同的元素,不能插入成功
                return false;
            }
        }
        //
        if (parent.val < key){
            parent.right = node;
        }else {
            parent.left = node;
        }
        return true;
    }
    /**
     * 进行删除
     * @param cur 当前需要删除的节点
     * @param parent 当前需要删除节点的父亲节点
     */
    private void removeNode(TreeNode cur, TreeNode parent) {
        if (cur.left == null){
            if (cur == root){
                root = root.right;
            }else if (cur == parent.left){
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if (cur.right == null){
            if (cur == root){
                cur = cur.left;
            }else if (cur == parent.left){
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            TreeNode targetParent = cur;
            TreeNode target = cur.right;
            while (target.left != null){
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            //分两种情况
            if (targetParent.left == target){
                targetParent.left = target.right;
            }else {
                targetParent.right = target.right;
            }
        }
    }

中序遍历:

public void inorder(TreeNode root){
        if (root == null){
            return;
        }
        inorder(root.left);
        System.out.print(root.val+" ");
        inorder(root.right);
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值