Binary sort tree (implemented in java code) constructs a binary sort tree, traverses the binary sort tree, finds and deletes nodes

Posted by kawai84 on Thu, 06 Jan 2022 10:34:42 +0100

Define tree nodes

Define the attributes of tree nodes and the middle order traversal method

class Node {
    int value;
    Node left;
    Node right;
    public Node(int value) {
        this.value = value;
    }
    //Medium order traversal
    //The result of a binary tree traversed in middle order is ordered
    public void infixOrder() {
        if(this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right != null) {
            this.right.infixOrder();
        }
    }
}

Define the method of constructing binary sort tree by tree node (add tree node)

public void add(Node node) {
        if(node == null) {
            return;
        }
        //Judge the value of the incoming node and the value relationship with the root node of the current subtree
        if(node.value < this.value) {
            //If the left child node of the current node is null
            if(this.left == null) {
                this.left = node;
            } else {
                //Recursively add to the left subtree
                this.left.add(node);
            }
        } else { //The value of the added node is greater than the value of the current node
            if(this.right == null) {
                this.right = node;
            } else {
                //Recursive right subtree addition
                this.right.add(node);
            }
        }
    }

Defines the lookup method for tree nodes

/**
 *
 * @param value The value of the node you want to delete
 * @return If the node is found, null is returned
 */
public Node search(int value) {
    if(value == this.value) { //This node is found
        return this;
    } else if(value < this.value) {//If the search value is less than the current node, the left subtree will be searched recursively
        //If the left child node is empty, the search fails
        if(this.left  == null) {
            return null;
        }
        return this.left.search(value);
    } else { //If the search value is not less than the current node, recursively search the right subtree
         //If the right child node is empty, the search fails
        if(this.right == null) {
            return null;
        }
        return this.right.search(value);
    }

}

Define the method to find the parent node of the node to be deleted (for deletion operation)

/**
 *
 * @param value The value of the node to find
 * @return The parent node of the node to be deleted is returned. If not, null is returned
 */
public Node searchParent(int value) {
    //If the current node is the parent node of the node to be deleted, return
    if((this.left != null && this.left.value == value) ||
            (this.right != null && this.right.value == value)) {
        return this;
    } else {
        //If the searched value is less than the value of the current node, and the left child node of the current node is not empty
        if(value < this.value && this.left != null) {
            return this.left.searchParent(value); //Left subtree recursive lookup
        } else if (value >= this.value && this.right != null) {
            return this.right.searchParent(value); //Recursive search of right subtree
        } else {
            return null; // Parent node not found
        }
    }

}

Define binary sort tree

Define the root node and the middle order traversal method

class BinarySortTree {
    private Node root;
    
    public Node getRoot() {
        return root;
    }
    //Medium order traversal
    public void infixOrder() {
        if(root != null) {
            root.infixOrder();
        } else {
            System.out.println("Binary sort tree is empty and cannot be traversed");
        }
    }
}

Define the construction method of binary sort tree

public void add(Node node) {
    if(root == null) {
        root = node;//If root is empty, direct root to node
    } else {
        root.add(node);
    }
}

Define the method of finding nodes in binary sort tree

public Node search(int value) {
    if(root == null) {
        return null;
    } else {
        return root.search(value);
    }
}

Define a binary sort tree method to find the parent node of the node to be deleted (for deletion)

public Node searchParent(int value) {
    if(root == null) {
        return null;
    } else {
        return root.searchParent(value);
    }
}

Defines a method to find a binary sort tree, find the smallest node and delete it (for deletion)

//1. The returned information of the smallest node of the binary sort tree with node as the root node
//2. Delete the minimum node of the binary sort tree with node as the root node
/**
 *
 * @param node Incoming node (as the root node of binary sort tree)
 * @return Returns the information of the smallest node of the binary sort tree with node as the root node
 */
public int delRightTreeMin(Node node) {
    Node target = node;
    //Loop to find the left child node, and the minimum value will be found
    //Because the leftmost node is the minimum
    while(target.left != null) {
        target = target.left;
    }
    //At this point, the target points to the smallest node
    //Delete minimum node
    delNode(target.value);
    return target.value;
}

Define the method of deleting nodes in binary sort tree

There are three ways to delete a node

  • The node to be deleted is a leaf node
  1. You need to find the targetNode to delete first

  2. Find the parent node of targetNode

  3. Determines whether the targetNode is the left child node or the right child node of the parent

  4. Delete according to the previous situation

    Left child node parent left = null

    Right child node parent right = null;

  • Delete nodes with only one subtree
  1. You need to find the targetNode to delete first

  2. Find the parent node of targetNode

  3. Determines whether the targetNode has a left child node or a right child node

  4. Determines whether the targetNode is the left child node or the right child node of the parent

    • If the targetNode has a left child node

    ① if the targetNode is the left child node of the parent

    ​ parent.left = targetNode.left;

    ② if the targetNode is the right child node of the parent

    ​ parent.right = targetNode.left;

    • If the targetNode has a right child node

    ① if the targetNode is the left child node of the parent

    ​ parent.left = targetNode.right;

    ② if the targetNode is the right child node of the parent

    ​ parent.right = targetNode.right

  • Delete a node with two subtrees

    1. You need to find the targetNode to delete first

    2. Find the parent node of targetNode

    3. Find the smallest node from the left subtree of targetNode

    4. Use a temporary variable temp to save the information of the smallest node

    5. Delete the minimum node

    6. Assign the information of the smallest node to the node to be deleted

      targetNode.value = temp

      The results show that the node to be deleted is not deleted, but the smallest node is deleted

public void delNode(int value) {
    if(root == null) {
        return;
    }else {
        //1. First find the targetNode to be deleted
        Node targetNode = search(value);
        //If the node to be deleted is not found
        if(targetNode == null) {
            return;
        }
        //If we find that the current binary sort tree has only one node
        if(root.left == null && root.right == null) {
            root = null;
            return;
        }

        //To find the parent node of targetNode
        Node parent = searchParent(value);
        //If the node to be deleted is a leaf node
        if(targetNode.left == null && targetNode.right == null) {
            //Judge whether the targetNode is the left child node or the right child node of the parent node
            if(parent.left != null && parent.left.value == value) { //Is the left child node
                parent.left = null;
            } else if (parent.right != null && parent.right.value == value) {//Is a child node
                parent.right = null;
            }
        } else if (targetNode.left != null && targetNode.right != null) { //Delete a node with two subtrees
            int minVal = delRightTreeMin(targetNode.right);
            targetNode.value = minVal;


        } else { // Delete nodes with only one subtree
            //If the node to be deleted has a left child node
            if(targetNode.left != null) {
                if(parent != null) {
                    //If the targetNode is the left child node of the parent
                    if(parent.left.value == value) {
                        parent.left = targetNode.left;
                    } else { //  targetNode is the right child node of the parent
                        parent.right = targetNode.left;
                    }
                } else {
                    root = targetNode.left;
                }
            } else { //If the node to be deleted has a right child node
                if(parent != null) {
                    //If the targetNode is the left child node of the parent
                    if(parent.left.value == value) {
                        parent.left = targetNode.right;
                    } else { //If the targetNode is the right child node of the parent
                        parent.right = targetNode.right;
                    }
                } else {
                    root = targetNode.right;
                }
            }

        }

    }
}

Topics: Java Algorithm data structure Binary tree