【Java数据结构】二叉搜索树

目录

先定义树结点

查找

插入数据

前序遍历

中序遍历

⭐⭐移除一个元素⭐⭐

测试


BinarySearchTree.java文件

先定义树结点

package binsearchtree;
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode (int val) {
            this.val = val;
        }
    }

查找

    public TreeNode root;
    //查找
    public TreeNode search(TreeNode root, int key) {
        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 inSertTree(int val) {
        if (root == null) {
            root = new TreeNode(val);
            return;
        }
        TreeNode cur = root;
        TreeNode p = null;
        TreeNode tmp = new TreeNode(val);
        while (cur != null) {
            p = cur;
            if (cur.val > val) {
                cur = cur.left;
            } else {
                cur = cur.right;
            }
        }
        if (val > p.val) {
            p.right = tmp;
        } else {
            p.left = tmp;
        }
    }

前序遍历

    //前序遍历
    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);
    }

⭐⭐移除一个元素⭐⭐

首先找到cur.val == key时的结点cur

考虑多种情况:(代码在后文)

1. cur.left == null

①. cur是root,则root=cur.right

② cur不是root,cur是parent.left,则parent.left = cur.right

③. cur不是root,cur是parent.right,则parent.right = cur.right

2. cur.right == null

①. cur是root,则root=cur.left

② cur不是root,cur是 parent.left,则 parent.left = cur.left

③. cur不是root,cur是 parent.right,则 parent.right = cur.left

 3. cur.left != null && cur.right != null (替罪羊删除法)

思路:在其左树找最大的数据 / 右树最小的数据

    //移除元素
    public void remove (int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else if (cur.val == key){
                removeNode(parent,cur);
            } else {
                parent = cur;
                cur = cur.left;
            }
        }
    }
    //移除元素子方法
    public void removeNode(TreeNode parent, TreeNode cur) {
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root = 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;
            }
        }
    }

测试

Test.java

package binsearchtree;
public class Test {
    public static void main(String[] args) {
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        binarySearchTree.inSertTree(10);
        binarySearchTree.inSertTree(41);
        binarySearchTree.inSertTree(19);
        binarySearchTree.inSertTree(5);
        binarySearchTree.inSertTree(18);
        binarySearchTree.inOrder(binarySearchTree.root);
        System.out.println();
        binarySearchTree.preOrder(binarySearchTree.root);
        System.out.println();

        //删除元素10
        binarySearchTree.remove(10);
        binarySearchTree.inOrder(binarySearchTree.root);
        System.out.println();
        binarySearchTree.preOrder(binarySearchTree.root);
        System.out.println();
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值