【数据结构】——搜索树

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);
    }

你可能感兴趣的:(数据结构,数据结构,算法)