📜  门| GATE-CS-2007 |问题 4(1)

📅  最后修改于: 2023-12-03 15:42:16.687000             🧑  作者: Mango

GATE-CS-2007 Question 4

This question is about understanding the implementation of a data structure called "binary search tree". The following code block represents the implementation of some functions for the binary search tree:

class Node {
    int value;
    Node left;
    Node right;
}

class BST {
    Node root;

    public void insert(int v) {
        if (root == null) {
            root = new Node();
            root.value = v;
        } else {
            insertHelper(root, v);
        }
    }

    private void insertHelper(Node n, int v) {
        if (v < n.value) {
            if (n.left == null) {
                n.left = new Node();
                n.left.value = v;
            } else {
                insertHelper(n.left, v);
            }
        } else {
            if (n.right == null) {
                n.right = new Node();
                n.right.value = v;
            } else {
                insertHelper(n.right, v);
            }
        }
    }

    public boolean search(int v) {
        return searchHelper(root, v);
    }

    private boolean searchHelper(Node n, int v) {
        if (n == null) {
            return false;
        }
        if (n.value == v) {
            return true;
        }
        if (v < n.value) {
            return searchHelper(n.left, v);
        } else {
            return searchHelper(n.right, v);
        }
    }

    public void delete(int v) {
        root = deleteHelper(root, v);
    }

    private Node deleteHelper(Node n, int v) {
        if (n == null) {
            return null;
        }
        if (v < n.value) {
            n.left = deleteHelper(n.left, v);
        } else if (v > n.value) {
            n.right = deleteHelper(n.right, v);
        } else {
            if (n.left == null) {
                return n.right;
            } else if (n.right == null) {
                return n.left;
            } else {
                n.value = getSuccessor(n.right);
                n.right = deleteHelper(n.right, n.value);
            }
        }
        return n;
    }

    private int getSuccessor(Node n) {
        int successor = n.value;
        while (n.left != null) {
            successor = n.left.value;
            n = n.left;
        }
        return successor;
    }
}

The Node class represents a single node in the binary search tree. Each of these nodes have a value, left and right pointer which point to the left and right child nodes respectively.

The BST class implements the binary search tree functionalities using the Node class. It has an instance variable called root which points to the root node of the binary search tree.

The insert(int v) function takes an integer v and inserts it into the binary search tree. If the binary search tree is empty, it creates a new node with the given value and sets it as the root node. If the binary search tree is not empty, the insertHelper function is called with the root node and the given value.

The search(int v) function takes an integer v and searches for it in the binary search tree. It returns true if the given value is found in the binary search tree, and false otherwise.

The delete(int v) function takes an integer v and deletes it from the binary search tree. It first calls the deleteHelper function with the root node and the given value. The deleteHelper function recursively traverses the binary search tree and deletes the node with the given value. If the node to be deleted has a left child or a right child, it simply replaces the node with its child. If the node to be deleted has both left and right children, it gets the successor of the node (the smallest node in its right subtree) and replaces the node with the successor.

Overall, the binary search tree implementation is very intuitive and simple to understand. The insert, search, and delete operations all have a time complexity of O(log n) in the best case when the tree is balanced, and O(n) in the worst case when the tree is completely unbalanced.