【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

【Java数据结构】二叉搜索树_第1张图片

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

【Java数据结构】二叉搜索树_第2张图片

 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();
    }
}

 

你可能感兴趣的:(java,数据结构,二叉树,二叉搜索树)