Data structure (tree)

Keywords: less Java

1, Binary sort tree

Definition: the value of the left subtree is less than the value of the parent node, and the value of the right subtree is greater than the value of the parent node. All nodes meet this definition.

Delete: in three cases
① If the deleted node is a leaf node, you can delete it directly. It doesn't affect the whole tree
② There are only left subtrees or right subtrees in the deleted nodes. It's better to move the subtree up
③ The deleted nodes include both left and right subtrees. If the deleted node is p, then according to the properties of binary tree (the result of middle order traversal is from small to large), it is necessary to find the precursor (the largest node smaller than P) or the successor (the smallest node larger than p) of P node to replace P node. That is, the right subtree of the right subtree of the left subtree of P.. Or P's right subtree's left subtree's left subtree....
It can be understood as follows: if we want to not move the right subtree of p and find a node to replace the p node in its left subtree, then in order to maintain the properties of the binary tree, we need to find the one with the largest node value in the left subtree of p, that is, the right subtree of the right subtree of the left subtree... , which is the rightmost node in the left subtree. If the rightmost node is q, then there must be no right subtree for this node, because if there is still a right subtree, then there are still nodes much larger than q.
So give the value of q node to p node, then give the left subtree of q to the parent node of q, and then delete q node.
java code:

public class BinaryTree {

    private Node root;

    public class Node{
        int val;
        Node leftChild,rightChild;
        Node(int val){
            this.val = val;
            leftChild=null;
            rightChild = null;
        }
    }

    BinaryTree(){
        root = null;
    }

    public void insert(int data) {
        Node newNode = new Node(data);
        if(root == null) root = newNode;
        else {
            Node cur = root;
            while (true) {
                if (data < cur.val) {
                    if (cur.leftChild == null) {
                        cur.leftChild = newNode;
                        break;
                    }
                    cur = cur.leftChild;
                } else {
                    if (cur.rightChild == null) {
                        cur.rightChild = newNode;
                        break;
                    }
                    cur = cur.rightChild;
                }
            }
        }
    }

    public Node find(int data) {
        Node cur = root;
        while (cur != null) {
            if (cur.val == data) return cur;
            if (data < cur.val) cur = cur.leftChild;
            else cur = cur.rightChild;
        }
        return null;
    }

    public Node dele(int data){
        return dele(root,data);
    }
    //delete
    private Node dele(Node p, int data) {
        if (p == null) return null;
        if (data < p.val) p.leftChild = dele(p.leftChild, data);
        else if (data > p.val) p.rightChild = dele(p.rightChild, data);
        else if(p.leftChild==null){
            if( root == p)
                root = p.rightChild;
            return  p.rightChild;
        }
        else if(p.rightChild==null){
            if(root == p)
                root = p.leftChild;
            return p.leftChild;
        }
        else{
            Node cur = p.leftChild,pre = p;
            while(cur.rightChild != null){
                pre = cur;
                cur = cur.rightChild;
            }
            p.val = cur.val;
            if(pre==p)p.leftChild = cur.leftChild;
            else pre.rightChild = cur.leftChild;
            cur = null;
        }
        return p;
    }

    //Print node value middle order traversal
    public void printVal(){
        printVal(root);
    }
    private void printVal(Node p) {
        if (p == null) return;
        printVal(p.leftChild);
        System.out.print(p.val + " ");
        printVal(p.rightChild);
    }
}

Published 70 original articles, won praise 5, visited 7144
Private letter follow

Posted by CBaZ on Fri, 14 Feb 2020 00:36:57 -0800