Binary search tree -- Java implementation

Posted by ricmetal on Tue, 10 Mar 2020 05:13:18 +0100

Characteristic

  1. If its left subtree is not empty, the values of all nodes in the left subtree are smaller than the values of its root nodes;
  2. If its right subtree is not empty, the values of all nodes in the right subtree are smaller than the values of its root nodes;
  3. Its left and right subtrees are also binary search numbers.

Node definition in Java

    //Node definition
    private class Node{
        int data;
        Node left;
        Node right;
    }
    
    //Root node
    private Node root;

Search node

  1. If the search value is larger than the current value, the right subtree is searched;
  2. If the search value is smaller than the current value, the left subtree will be searched;
  3. Equal to stop.
 //Search node
    public Node find(int key){
        if (root == null){
            throw new ArithmeticException("root is null");
        }
        Node currentNode = root;
        while(currentNode!=null && currentNode.data!=key){
            if(key>currentNode.data){
                currentNode = currentNode.right;
            }else{
                currentNode = currentNode.left;
            }
        }

        if(currentNode==null){
            throw new RuntimeException("the node is not found");
        }

        return currentNode;
    }

Insertion node

Similar to the find operation, until the location of the empty node is found and found

public void insert(int key){
        //Create node to insert
        Node newNode = new Node();
        newNode.data = key;
        if(root==null){
            root=newNode;
        }else{
            Node currentNode = new Node();  //Create current node
            Node nextNode = root;
            while (nextNode!=null){
                currentNode = nextNode;
                if(key>currentNode.data){
                    nextNode = currentNode.right;
                }else{
                    nextNode = currentNode.left;
                }
            }

            if(key>currentNode.data){
                currentNode.right=newNode;
            }else{
                currentNode.left=newNode;
            }
        }
    }

Sequential traversal

Left subtree – > root – > right subtree

//Middle order traversal left root right
    public void inOrder(Node root){
        if(root!=null){
            inOrder(root.left);
            System.out.println(root.data);
            inOrder(root.right);
        }
    }

Find Max and min

Maximum value: starting from the root node, find the right child node of the parent node until there is no right child node, then the value of this node is the maximum value.

Minimum value: starting from the root node, find the left child node of the parent node until there is no left child node, then the value of this node is the minimum value.

Delete node

There are three situations

1. This node is a leaf node

Just change the reference of its parent node to null

2. This node has a child node

Just change the reference of its parent node to the reference of its child node

3. This node has two child nodes

Find the successor node of the node (that is, the smallest node larger than the node) and replace the node

private Node getSuccessor(Node delNode)    //Find the middle order successor node to delete
    {
        Node successorParent=delNode;
        Node successor=delNode;
        Node current=delNode.right;

        //Used to find subsequent nodes
        while(current!=null)
        {
            successorParent=successor;
            successor=current;
            current=current.left;
        }

        //If the successor node is the left child of the right subtree of the node to be deleted, you need to adjust the right subtree of the node to be deleted in advance
        if(successor!=delNode.right)
        {
            successorParent.left=successor.right;
            successor.right=delNode.right;
        }
        return successor;
    }

    public boolean delete(int key) // Delete Vertex
    {
        Node current = root;
        Node parent = new Node();
        boolean isRightChild = true;
        while (current.data != key)
        {
            parent = current;
            if (key > current.data)
            {
                current = current.right;
                isRightChild = true;
            }
            else
            {
                current = current.left;
                isRightChild = false;
            }
            if (current == null) {
                return false; // No nodes found to delete
            }
        }
        // At this time, current is the node to be deleted, and parent is its parent node
        // To delete a node as a leaf node
        if (current.right == null && current.left == null)
        {
            if (current == root)
            {
                root = null; // Empty the whole tree
            }
            else
            {
                if (isRightChild) {
                    parent.right = null;
                } else {
                    parent.left = null;
                }
            }
            return true;
        }
        //The node to be deleted has a child node
        else if(current.left==null)
        {
            if(current==root) {
                root=current.right;
            } else if(isRightChild) {
                parent.right=current.right;
            } else {
                parent.left=current.right;
            }
            return true;
        }
        else if(current.right==null)
        {
            if(current==root) {
                root=current.left;
            } else if(isRightChild) {
                parent.right=current.left;
            } else {
                parent.left=current.left;
            }
            return true;
        }
        //The node to be deleted has two child nodes
        else
        {
            Node successor=getSuccessor(current);    //Find the successor node to delete

            if(current==root) {
                root=successor;
            } else if(isRightChild) {
                parent.right=successor;
            } else {
                parent.left=successor;
            }

            successor.left=current.left;
            return true;
        }
    }
Published 9 original articles, won praise 8, and visited 20000+
Private letter follow

Topics: Java