欢迎访问宙启技术站
智能推送

实现Java中的二叉搜索树数据结构

发布时间:2023-07-04 16:58:40

二叉搜索树(Binary Search Tree,简称BST)是一种数据结构,它是一棵二叉树,满足以下条件:

1. 左子树上所有结点的值都小于根结点的值。

2. 右子树上所有结点的值都大于根结点的值。

3. 左右子树也都是二叉搜索树。

实现Java中的二叉搜索树,我们可以定义一个类,包含以下几个重要的方法:

1. 插入操作(insert):向二叉搜索树中插入一个新的结点。

2. 删除操作(delete):从二叉搜索树中删除指定结点。

3. 查找操作(search):在二叉搜索树中查找指定值的结点。

4. 中序遍历(in-order traversal):按照结点值的大小顺序输出所有结点的值。

首先,我们需要定义一个结点类,表示二叉搜索树的结点,它包含三个成员变量:值(value)、左子结点(left)和右子结点(right)。

class Node {
    int value;
    Node left;
    Node right;
    
    public Node(int value) {
        this.value = value;
        left = null;
        right = null;
    }
}

接下来,我们定义一个二叉搜索树类,包含插入、删除、查找和中序遍历等方法。

class BinarySearchTree {
    private Node root;
    
    public BinarySearchTree() {
        root = null;
    }
    
    public void insert(int value) {
        root = insertRec(root, value);
    }
    
    private Node insertRec(Node root, int value) {
        if (root == null) {
            root = new Node(value);
            return root;
        }
        
        if (value < root.value) {
            root.left = insertRec(root.left, value);
        } else if (value > root.value) {
            root.right = insertRec(root.right, value);
        }
        
        return root;
    }
    
    public void delete(int value) {
        root = deleteRec(root, value);
    }
    
    private Node deleteRec(Node root, int value) {
        if (root == null) {
            return root;
        }
        
        if (value < root.value) {
            root.left = deleteRec(root.left, value);
        } else if (value > root.value) {
            root.right = deleteRec(root.right, value);
        } else {
            if (root.left == null) {
                return root.right;
            } else if (root.right == null) {
                return root.left;
            }
            
            root.value = minValue(root.right);
            root.right = deleteRec(root.right, root.value);
        }
        
        return root;
    }
    
    private int minValue(Node root) {
        int minv = root.value;
        while (root.left != null) {
            minv = root.left.value;
            root = root.left;
        }
        return minv;
    }
    
    public Node search(int value) {
        return searchRec(root, value);
    }
    
    private Node searchRec(Node root, int value) {
        if (root == null || root.value == value) {
            return root;
        }
        
        if (value < root.value) {
            return searchRec(root.left, value);
        }
        
        return searchRec(root.right, value);
    }
    
    public void inorder() {
        inorderRec(root);
    }
    
    private void inorderRec(Node root) {
        if (root != null) {
            inorderRec(root.left);
            System.out.print(root.value + " ");
            inorderRec(root.right);
        }
    }
}

使用二叉搜索树类,我们可以进行插入、删除、查找和中序遍历等操作。

public class Main {
    public static void main(String[] args) {
        BinarySearchTree bst = new BinarySearchTree();
        
        bst.insert(50);
        bst.insert(30);
        bst.insert(20);
        bst.insert(40);
        bst.insert(70);
        bst.insert(60);
        bst.insert(80);
        
        System.out.println("Inorder traversal of the BST:");
        bst.inorder();
        
        Node result = bst.search(60);
        if (result != null) {
            System.out.println("
Node with value 60 found.");
        } else {
            System.out.println("
Node with value 60 not found.");
        }
        
        bst.delete(80);
        System.out.println("
Inorder traversal after deleting 80:");
        bst.inorder();
    }
}

上述代码中,首先创建一个二叉搜索树对象bst,并依次插入一些结点。然后使用中序遍历方法输出树中所有结点的值。接下来,使用搜索方法查找值为60的结点,并输出查找结果。最后,使用删除方法删除值为80的结点,并再次进行中序遍历。

以上就是Java中实现二叉搜索树数据结构的简要介绍,可以通过插入、删除、查找和中序遍历等操作来操作二叉搜索树。通过合理的使用,二叉搜索树可以在很多场景中发挥重要作用。