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

如何使用Java实现二叉搜索树数据结构

发布时间:2023-10-09 04:33:52

Java语言可以使用类来实现二叉搜索树数据结构。下面是一个简单的示例代码:

首先,我们需要定义一个Node类,表示二叉搜索树的节点:

class Node {
    int key;
    Node left, right;

    public Node(int item) {
        key = item;
        left = right = null;
    }
}

然后,我们定义一个BinarySearchTree类,来实现二叉搜索树的操作:

class BinarySearchTree {
    Node root;

    BinarySearchTree() {
        root = null;
    }

    void insert(int key) {
        root = insertRec(root, key);
    }

    Node insertRec(Node root, int key) {
        if (root == null) {
            root = new Node(key);
            return root;
        }

        if (key < root.key) {
            root.left = insertRec(root.left, key);
        } else if (key > root.key) {
            root.right = insertRec(root.right, key);
        }

        return root;
    }

    void inOrder() {
        inOrderRec(root);
    }

    void inOrderRec(Node root) {
        if (root != null) {
            inOrderRec(root.left);
            System.out.print(root.key + " ");
            inOrderRec(root.right);
        }
    }

    boolean search(int key) {
        return searchRec(root, key);
    }

    boolean searchRec(Node root, int key) {
        if (root == null || root.key == key) {
            return root != null;
        }

        if (key < root.key) {
            return searchRec(root.left, key);
        }

        return searchRec(root.right, key);
    }

    void delete(int key) {
        root = deleteRec(root, key);
    }

    Node deleteRec(Node root, int key) {
        if (root == null) {
            return root;
        }

        if (key < root.key) {
            root.left = deleteRec(root.left, key);
        } else if (key > root.key) {
            root.right = deleteRec(root.right, key);
        } else {
            if (root.left == null) {
                return root.right;
            } else if (root.right == null) {
                return root.left;
            }

            root.key = minValue(root.right);

            root.right = deleteRec(root.right, root.key);
        }

        return root;
    }

    int minValue(Node root) {
        int minValue = root.key;
        while (root.left != null) {
            minValue = root.left.key;
            root = root.left;
        }
        return minValue;
    }
}

接下来,我们可以使用BinarySearchTree类来创建二叉搜索树并进行相关操作:

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 binary search tree:");
        bst.inOrder();

        int key = 60;
        if (bst.search(key)) {
            System.out.println("
" + key + " found");
        } else {
            System.out.println("
" + key + " not found");
        }

        bst.delete(60);

        System.out.println("
Inorder traversal after deletion of " + key + ":");
        bst.inOrder();
    }
}

运行以上代码,将得到以下输出:

Inorder traversal of the binary search tree:
20 30 40 50 60 70 80 
60 found
Inorder traversal after deletion of 60:
20 30 40 50 70 80 

这样,我们就实现了一个简单的二叉搜索树数据结构。