二叉搜索树的建立,删除关键字以及寻找关键字;

public class BinarySearchTree {
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode (int val){
           this.val = val;
        }
    }
    //插入元素;
    TreeNode root =  null;
    public void put(int key){
        TreeNode node = new TreeNode(key);
        if(root == null) {
            root = node;
            return;
        }
        TreeNode partent = null;
        TreeNode cur = root;
        while (cur != null){
            if(cur.val == key){ //二叉搜索数不存在数值相同的节点,因此加入一个已经存在的元素,就会自动替换掉;
                cur.val = node.val;
                return;
            }else if (cur.val < key){
                partent = cur;
                cur = cur.right;
            }else {
                partent = cur;
                cur = cur.left;
            }
        }
        if(partent.val < key){
            partent.right = node;
        }else {
            partent.left = node;
        }
    }
    //寻找关键字key
    public TreeNode findKey(int key){
        if(root == null) return null;
        TreeNode cur = root;
        while (cur != null){
            if(cur.val == key){
                return cur;
            }else if (cur.val < key){
                cur = cur.right;
            }else {
                cur = cur.left;
            }
        }
        return null;
    }
    public void preorder(TreeNode root){
        if(root == null) return;
        System.out.print(root.val + " ");
        preorder(root.left);
        preorder(root.right);
    }
    public void inorder(TreeNode root){
        if(root == null) return;
        inorder(root.left);
        System.out.print(root.val + " ");
        inorder(root.right);
    }
    //删除关键字;
    public void remove(int key){
        TreeNode partent = null;
        TreeNode cur = root;
        while(cur != null){
            if(cur.val == key){
                break;
            }else if(cur.val < key){
                partent = cur;
                cur = cur.right;
            }else {
                partent = cur;
                cur = cur.left;
            }
        }
        if(cur == null) return;
        removeNode(partent,cur);
    }
    public void removeNode(TreeNode partent,TreeNode cur) {
        if(cur.left == null){
            if (cur == root){
                root = cur.right;
            }else if(partent.left == cur){
                partent.left = cur.right;
            }else {
                partent.right = cur.right;
            }
        }else if(cur.right == null){
            if (cur == root){
                root = cur.left;
            }else if(partent.right == cur){
                partent.left = cur.left;
            }else {
                partent.right = cur.left;
            }
        }else {
            TreeNode targetPartent = cur;
            TreeNode target = targetPartent.right;
            while (target.left != null){
                targetPartent = target;
                target = target.left;
            }
            cur.val = target.val;
            if(target == targetPartent.left){
                targetPartent.left = target.right;
            }else {
                targetPartent.right = target.right;
            }
        }
    }
    public static void main(String[] args) {
        BinarySearchTree tree = new BinarySearchTree();
        int[] array = {23, 54, 12, 45, 64, 67, 76, 25, 65};
        for (int i = 0; i < array.length; i++) {
            tree.put(array[i]);
        }
        tree.preorder(tree.root);
        System.out.println();
        tree.inorder(tree.root);
        tree.put(52);
        System.out.println();
        tree.inorder(tree.root);
        tree.put(23);
        System.out.println();
        tree.inorder(tree.root);
        System.out.println();
        TreeNode ret = tree.findKey(52);
        try {
            System.out.println(ret.val);
        }catch (NullPointerException e) {
            e.printStackTrace();
            System.out.println("找不到这个节点");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值