树和二叉树总结(三)—BST二叉排序树

二叉排序树的特点:中序遍历是递增的…

下面复习几个常用的操作:

1.如何向BST中插入一个节点

思路:用递归做...
首先,判断head是否为空,为空就是需要插的地方;
然后,判断head的data是不是和新Node一样,一样就是重复Node,不加
最后,若比当前head.data小,则放到插到左节点的树你,否则就插到右节点
public static Node insertBST(Node root, Node keyNode) {
        if (root== null) { //如果遇到空的地方,就是该插入的地方...
            root= keyNode;
        } else { //如果节点不为空,则需要查找对应的位置,和二分法查找类似
            if (root.data == keyNode.data) { //找到一个同值得数,直接返回
                return root;
            } else if (keyNode.data < root.data) {
                root.left = insertBST(root.left, keyNode);
            } else {
                root.right = insertBST(root.right, keyNode);
            }
        }
        return root;
    }

2.如何创建BST

思路
  和给BST插值一样,逐步insert...
public static Node createBST(int[] arr) {
        Node root = null;
        for (int i = 0; i < arr.length; i++) {
            root = insertBST(root, new Node(arr[i]));
        }
        return root;
    }

3.判断一个树是不是二叉排序树

思路:
因为二叉排序树的中序遍历是递增有序序列,则可以利用这个特点,直接中序遍历二叉树,如果保证
前一个比后一个小,那么证明该树为一颗二叉排序树...
public static boolean judgeBst(Node root) {
        int preVal = 0;
        Node cur = root;
        Stack<Node> stack = new Stack<>();
        while (true) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            if (stack.isEmpty())
                break;
            cur = stack.pop();
            if (preVal > cur.data) {
                return false;
            } else {
                preVal = cur.data;
            }
            cur = cur.right;
        }
        return true;
    }

4.从BST中删除一个节点

思路:
  1.找到key
2.若key是叶子节点,直接删
3.若key只有左或右子树,直接隔过Node(key)接上其左或右子树...
4.若key既有左子树又有右子树,找到其左子树的最右边的节点rightest,用rightest代替key节点...然后接着从第二步开始判断如何把rightest删掉...
public static Node deleteNodeFromBST(Node root, int key) {
        if (root == null) return null;
        if (key < root.data) {
            root.left = deleteNodeFromBST(root.left, key);
        } else if (key > root.data) {
            root.right = deleteNodeFromBST(root.right, key);
        } else {
            //找到key
            if (root.left == null) {
                return root.right;
            } else if (root.right == null) {
                return root.left;
            }
            //有左右子树的情况...
            Node tmp = root;
            //找到root左子树中最右边的那个Node, 这里只能从tmp去遍历
            root = findRightest(tmp.left);
            root.left = deleteRightest(tmp.left);
            root.right = tmp.right;
        }
        return root;
    }

    // 找到左子树中最右的那个Node,因为它是root左子树的最大值...
    private static Node findRightest(Node root) {
        if (root == null) return null;
        while (root.right != null) {
            root = root.right;
        }
        return root;
    }

    // 删除刚刚从发现的那个点(root左子树中最右边的那个rightest)
    // 因为是最右面的Node,则必然没有右子树
    private static Node deleteRightest(Node root) {
        if (root.right == null)
            return root.left;
        root.right = deleteRightest(root.right);
        return null;
    }
原文地址:https://www.cnblogs.com/zhaoqi3215/p/5296979.html