In-depth talk about the practical application of Java tree structure (binary sort tree)

Binary Sort Tree

1 First look at a demand

Give you a number sequence (7, 3, 10, 12, 5, 1, 9), which requires efficient completion of data query and addition

 

2 Solution analysis

 Use an array

The array is not sorted. Advantages: add directly at the end of the array, which is fast. Disadvantages: slow search speed.

Array sorting, advantages: you can use binary search, the search speed is fast, disadvantages: in order to ensure the order of the array, when adding new data, find the insertion position

After setting, the following data needs to be moved as a whole, and the speed is slow.

 Use chain storage-linked list

Regardless of whether the linked list is in order, the search speed is slow, and the speed of adding data is faster than that of the array, and there is no need to move the data as a whole.

 Use a binary sort tree

 

3 Introduction to Binary Sort Tree

  Binary Sort Tree: BST: (Binary Sort(Search) Tree), for any non-leaf node of the binary sort tree, the value of the left child node is required to be

The value of the previous node is smaller, and the value of the right child node is larger than the value of the current node.

  Special note: If there are the same values, the node can be placed in the left child node or the right child node

  For example, for the previous data (7, 3, 10, 12, 5, 1, 9), the corresponding binary sort tree is:

Java tree structure practical application (binary sort tree)

 

4 Binary sort tree creation and traversal

An array is created into a corresponding binary sort tree, and the binary sort tree is traversed using in-order, for example: the array is Array(7, 3, 10, 12, 5, 1, 9), create

The corresponding binary sort tree is built as:

Java tree structure practical application (binary sort tree)

 

5 Deletion of Binary Sort Tree

The deletion of a binary sort tree is more complicated, and there are three situations to consider

1) Delete leaf nodes (for example: 2, 5, 9, 12)

2) Delete nodes with only one subtree (for example: 1)

3) Delete nodes with two subtrees. (For example: 7, 3, 10)

4) Analysis of operation ideas

Java tree structure practical application (binary sort tree)

 

Analysis of ideas for various situations of deleting nodes:

The first situation:

Delete leaf nodes (for example: 2, 5, 9, 12)

Ideas

(1) Need to find the node targetNode to be deleted first

(2) Find the parent node parent of targetNode

(3) Determine whether targetNode is the left child node or the right child node of parent

(4) Corresponding deletion according to the previous situation

Left child node parent.left = null

Right child node parent.right = null;

The second case: delete nodes with only one subtree such as 1

Ideas

(1) Need to find the node targetNode to be deleted first

(2) Find the parent node parent of targetNode

(3) Determine whether the child node of targetNode is the left child node or the right child node

(4) Is targetNode the left child node or the right child node of parent

(5) If targetNode has a left child node

(5). 1 If targetNode is the left child node of parent

parent.left = targetNode.left;

(5).2 If targetNode is the right child node of parent

parent.right = targetNode.left;

(6) If targetNode has a right child node

(6).1 If targetNode is the left child node of parent

parent.left = targetNode.right;

(6).2 If targetNode is the right child node of parent

parent.right = targetNode.righ

Case 3: Delete the node with two subtrees. (For example: 7, 3, 10)

Ideas

(1) Need to find the node targetNode to be deleted first

(2) Find the parent node parent of targetNode

(3) Find the smallest node from the right subtree of targetNode

(4) Use a temporary variable to save the value of the smallest node temp = 11

(5) Delete the smallest node

(6) targetNode.value = temp

 

6 Code Implementation of Delete Node in Binary Sort Tree

package com.lin.binarysorttree_0314;

public class BinarySortTreeTest {

    public static void main(String[] args) {
        int[] arr = {7, 3, 10, 12, 5, 1, 9};
        BinarySortTree binarySortTree = new BinarySortTree();
        for (int i = 0; i < arr.length; i++) {
            binarySortTree.add(new SNode(arr[i]));
        }
        
        binarySortTree.add(new SNode(2));
        binarySortTree.infixOrder();
        
        // 删除
        System.out.println("***********");
        
        binarySortTree.delNode(2);
        binarySortTree.delNode(3);
        binarySortTree.delNode(5);
        binarySortTree.delNode(7);
        binarySortTree.delNode(9);
        binarySortTree.delNode(12);
        System.out.println("root:" + binarySortTree.getRoot());
        
        binarySortTree.infixOrder();
    }
}

class BinarySortTree{
    private SNode root;
    // 查找要删除的节点
    public SNode getRoot() {
        return root;
    }
    public SNode searchDelNode(int value) {
        if(root == null) {
            return null;
        } else {
            return root.searchDelNode(value);
        }
    }
    // 查找要删除节点的父节点
    public SNode searchParent(int value) {
        if(root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }
    /**
     * @param node 传入的节点(当作二叉排序树的根节点)
     * @return 返回的以node为根节点的二叉排序树的最小节点的值
     */
    public int delRightTreeMin(SNode node) {
        SNode target = node;
        //    循环地查找左节点,就会找到最小值
        while(target.left != null) {
            target = target.left;
        }
        delNode(target.value);// !!!!
        return target.value;//   !!!!!
    }
    
    // 删除节点
    public void delNode(int value) {
        if(root == null) {
            return;
        } else {
            //     找删除节点
            SNode targetNode = searchDelNode(value);
            //     没有找到
            if(targetNode == null) {
                return;
            }
            //    如果发现当前这棵二叉树只有一个节点
            if(root.left == null && root.right == null) {
                root = null;
                return;
            }
            //     去找到targetNode的父节点
            SNode parent = searchParent(value);
            //     如果删除的节点是叶子节点
            if(targetNode.left == null && targetNode.right == null) {
                //    判断targetNode是父节点的左子节点还是右子节点
                if(parent.left != null && parent.left.value == value) {
                    parent.left = null;
                } else if(parent.right != null && parent.right.value == value) {
                    parent.right = null;
                }
            } else if(targetNode.left != null && targetNode.right != null) { //    有左右子节点
                int delRightTreeMin = delRightTreeMin(targetNode.right);
                targetNode.value = delRightTreeMin;
            } else {//    只有一个子节点
                //     要删除的节点只有左节点
                if(targetNode.left !=  null) {
                    if(parent != null) {
                        //     如果targetNode是parent的左子节点
                        if(parent.left.value == value) {
                            parent.left = targetNode.left;
                        } else {
                            parent.right = targetNode.left;
                        }
                    } else {
                        root = targetNode.left;
                    }
                } else {//    要删除的节点有右子节点
                    if(parent != null) {
                        if(parent.left.value == value) {
                            parent.left = targetNode.right;
                        } else {
                            parent.right = targetNode.right;
                        }
                    } else {
                        root = targetNode.right;
                    }
                }
            }
            
            
        }
    }
    // 中序遍历
    public void infixOrder() {
        if(root == null) {
            System.out.println("空树!");
        } else {
            root.infixOrder();
        }
    }
    // 添加
    public void add(SNode node) {
        if(root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }
}

class SNode{
    protected int value;
    protected SNode left;
    protected SNode right;
    
    public SNode(int value) {
        // TODO Auto-generated constructor stub
        this.value = value;
    }
    
    
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "Node = [value = " + value + "]";
    }
    
    // 添加节点
    public void add(SNode node) {
        if(node == null) {
            return;
        }
        if(node.value < this.value) {
            if(this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }
        } else {
            if(this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
    }
    // 中序遍历
    public void infixOrder() {
        if(this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right != null) {
            this.right.infixOrder();
        }
    }
    // 查找要删除的节点
    public SNode searchDelNode(int value) {
        if(this.value == value) {
            return this;
        } else if(this.value > value) {
            // 如果左子节点为空
            if(this.left == null) {
                return null;
            }
            return this.left.searchDelNode(value);
        } else {
            if(this.right == null) {
                return null;
            }
            return this.right.searchDelNode(value);
        }
    }
    // 查找要删除节点的父节点, 如果没有则返回null
    public SNode searchParent(int value) {
        if(( this.left != null && this.left.value == value) 
                || ( this.right != null && this.right.value == value )) {
            return this;
        } else {
             // 如果查找的值小于当前节点的值,并且当前节点的左子节点不为空
            if(value < this.value && this.left != null) {
                return this.left.searchParent(value);
            } else if(value >= this.value && this.right != null) {
                return this.right.searchParent(value);
            } else {
                return null;
            }
        }
    }
    
}

 

Java tree structure practical application (binary sort tree)

Guess you like

Origin blog.csdn.net/a159357445566/article/details/115210835