LeetCode-95-不同的二叉搜索树II

二叉搜索数的插入、查找、删除

 

public class TestUtil {
    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode();
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        for (Integer value : list) {
            insert(treeNode, value);
        }

    }

    private static void insert(TreeNode root, Integer value) {
        if (root == null) {
            TreeNode treeNode = new TreeNode();
            treeNode.value = value;
            treeNode.lChild = null;
            treeNode.rChild = null;

            root = treeNode;
        }

        if (root.value > value) {
            insert(root.lChild, value);
        }

        if (root.value < value) {
            insert(root.rChild, value);
        }
    }

    private static TreeNode find(TreeNode root, Integer value) {
        if (root == null || root.value.equals(value)) {
            return root;
        }

        if (root.value > value) {
            return find(root.lChild, value);
        }

        if (root.value < value) {
            return find(root.rChild, value);
        }

        return null;
    }

    private static TreeNode delete(TreeNode root, Integer value) {
        if (root == null) {
            return root;
        }

        TreeNode father = null;
        TreeNode delete = root;
        while(delete != null) {
            if (delete.value.equals(value)) {
                break;
            } if (delete.value > value) {
                father = delete;
                delete = delete.lChild;
            } else {
                father = delete;
                delete = delete.rChild;
            }
        }

        if (delete.lChild != null) {
            if (delete.lChild.rChild == null) {
                delete.value = delete.lChild.value;
                delete.lChild = delete.lChild.lChild;
            } else {
                TreeNode preNode = delete.lChild;
                TreeNode tempNode = delete.lChild.rChild;
                while (tempNode.rChild != null) {
                    preNode = tempNode;
                    tempNode = tempNode.rChild;
                }

                delete.value = tempNode.value;
                preNode.rChild = tempNode.lChild;
            }
            return root;
        }

        if (delete.rChild != null) {
delete.value = delete.rChild.value;
delete.lChild = delete.rChild.lChild;
delete.rChild = delete.rChild.rChild; } if (father == null) { root = null; } else if (father.lChild.value == delete.value) { father.lChild = null; } else { father.rChild = null; } return root; } } class TreeNode { Integer value; TreeNode lChild; TreeNode rChild; }

  

二叉树搜索树(即:二叉排序树,二叉查找树)
二叉排序树或者是一颗空树,或者是具有下列性质的二叉树:
(1)若它的左子树不空,则左子树上的所有结点的值均小于它的根结点的值。
(2)若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值。
(3)它的左、右子树叶分别是二叉排序树。


二叉排序树是递归定义的。由定义可得:中叙遍历二叉排序树是可以得到一个结点值递增的序列。

 

参考: https://blog.csdn.net/weixin_41423494/article/details/89145139

posted on 2020-01-02 20:58  wangsong412  阅读(131)  评论(0编辑  收藏  举报