BST(二叉搜索树)的基本操作
BST(二叉搜索树)
- 首先,我们定义树的数据结构如下:
public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode(int val) {
        this.val = val;
        this.left = null;
        this.right = null;
    }
}
一、判断BST的合法性
二叉搜索树的左子树节点都比父节点要小、右子树节点都比父节点要大;每一个子树都是BST。
我们遍历的时候如果只比较父节点和他的子节点大小的话,会有bug:
public boolean isValidBST(TreeNode root) {
    if (root == null) {
        return true;
    }
    if (root.left != null && root.val <= root.left.val) {
        return false;
    }
    if (root.right != null && root.val >= root.right.val) {
        return false;
    }
    return isValidBST(root.left) && isValidBST(root.right);
}

对于这样子的树,确实满足左子树小于父节点,右子树大于父节点,但是却不是一个BST,所以我们使用两个辅助参数来解决这个问题:
public boolean isValidBST(TreeNode root) {
    return isValidBST(root, null, null);
}
public boolean isValidBST(TreeNode root, TreeNode min, TreeNode max) {
    if (root == null) {
        return true;
    }
    if (min != null && root.val <= min.val) {
        return false;
    }
    if (max != null && root.val >= max.val) {
        return false;
    }
    // 左子树不得大于max边界,右子树不得小于min边界,就能得到正确结果了
    return isValidBST(root.left, min, root) && isValidBST(root.right, root, max);
}
二、在BST中查找目标值
直接暴力全部搜索:
boolean isInBST(TreeNode root, int target) {
    if (root == null) {
        return false;
    }
    if (root.val == target) {
        return true;
    }
    return isInBST(root.left, target) || isInBST(root.right, target);
}
但是BST有左小右大的这个特性,于是可以运用类似二分的思想,只需要将target和当前结点值比较,如果小于,就搜索左边,右边就可以不用搜索,反之:
boolean isInBST(TreeNode root, int target) {
    if (root == null) {
        return false;
    }
    if (root.val == target) {
        return true;
    }
    if (root.val < target) {
        return isInBST(root.right, target);
    } else {
        return isInBST(root.left, target);
    }
}
三、在BST中插入一个值
TreeNode insertIntoBST(TreeNode root, int val) {
    if (root == null) {
        return new TreeNode(val);
    }
    if (root.val == val) {
        return root;
    }
    // 比节点的值大
    if (root.val < val) {
        root.right = insertIntoBST(root.right, val);
    }
    // 比节点的值小
    if (root.val > val) {
        root.left = insertIntoBST(root.left, val);
    }
    return root;
}
四、在BST中删除一个值
- 分三种情况:
- 没有左右子节点:
- 可以直接删除
 
- 只有左子树或者只有右子树
- 只需将上一个父节点的next指向他的唯一孩子即可
 
- 既有左子树又有右子树
- 找到待删除节点的右子树的最小的那个值,与待删除的节点进行交换,然后把待删除的节点删除(这里只是进行值得交换,实际中一个节点可能包含多个域,应该要修改指针得指向,而不是简单得交换数据)
 
 
- 没有左右子节点:
public TreeNode deleteNode(TreeNode root, int key) {
    if (root == null) {
        return null;
    }
    if (root.val == key) {
        // 删除
        // 如果只有一个或者没有子树得情况
        if (root.left == null) {
            return root.right;
        }
        if (root.right == null) {
            return root.left;
        }
        // 当既有左子树又有右子树时
        // 这里只是进行值交换,应该进行指针修改
        TreeNode minNode = getMin(root);
        root.val = minNode.val;
        // 删除交换后得节点,相当于此时待删除节点又跑到末尾去了
        root.right = deleteNode(root.right, key);
    } else if (root.val > key) {
        // 去左子树找
        root.left = deleteNode(root.left, key);
    } else if (root.val < key) {
        // 去右子树找
        root.right = deleteNode(root.right, key);
    }
    return root;
}
public TreeNode getMin(TreeNode node) {
    while (node.left != null) {
        node = node.left;
    }
    return node;
}
    我走得很慢,但我从不后退!

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号