World hackers leaked the actual application of Java tree structure (binary sort tree)

Binary Sort Tree
1 Look at a requirement first.
Give you a sequence (7, 3, 10, 12, 5, 1, 9), which requires efficient data query and addition

2 Solution analysis
 Use 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: binary search can be used. The search speed is fast. Disadvantages: In order to ensure the order of the array, when adding new data, after finding the insertion position
, the subsequent data needs to be moved as a whole, which is slow.
 Use chain storage-linked list
No matter whether the linked list is in order, the search speed is slow, and the speed of adding data is faster than that of arrays, and there is no need to move the data as a whole.
 Use a binary sort tree

3 binary sort tree describes
  binary sort tree: BST: (Binary Sort (Search ) Tree), for any non-leaf node binary sort tree, the required value of the left child node than when
a small value before the node, and right son The value of the node is greater than the value of the current node.
  Special note: If you have the same value, you can place the node 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 sorting tree is : Insert picture description here
4 Binary sort tree creation and traversal of
an array to create a corresponding binary sort tree, and use in-order traversal of the binary sort tree, for example: the array is Array(7, 3, 10, 12, 5, 1, 9) The
corresponding binary sorting tree is created as follows: Insert picture description here
5 Deletion of the
binary sorting tree The deletion of the binary sorting tree is more complicated. There are three situations that need to be considered.

  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 Insert picture description here
    of the idea of ​​operation Thinking analysis of various situations of deleting nodes: The
    first situation:
    deleting leaf nodes (for example: 2, 5, 9, 12)
    Ideas
    (1) Need to find the node to be deleted targetNode
    ( 2) Find the parent node parent of
    targetNode (3) Determine whether targetNode is the left child node or right child node of parent
    (4) Delete correspondingly according to the previous situation
    Left child node parent.left = null
    Right child node parent .right = null;
    The second case: delete a node with only one subtree such as 1
    idea
    (1) first need to find the node to be deleted targetNode
    (2) find the parent node of targetNode
    (3) determine the targetNode Is the child node a left child node or a right child node
    (4) Is targetNode a left child node or a right child node of parent
    (5) If targetNode has a left child node
    (5). 1 If targetNode is a left child node of parent Child node
    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)
    Idea
    (1) Need to find the node to be deleted first targetNode
    (2) Find the parent node of targetNode parent
    (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 Binary sort tree delete node code implementation
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; // Find the node to be deleted public SNode getRoot() { return root; } public SNode searchDelNode(int value) { if(root == null) { return null; } else { return root.searchDelNode (value); } } // Find the parent node of the node to be deleted public SNode searchParent(int value) { if(root == null) { return null; } else { return root.searchParent(value); } } /** * @param node the incoming node (as the root node of the binary sorting tree) * @return the value of the smallest node of the binary sorting tree with node as the root node */ public int delRightTreeMin(SNode node) { SNode target = node;


























// Find the left node in a loop, and you will find the minimum value
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;
        }
    }
}

} Insert picture description here
Any similarities are purely coincidental. Follow the editor for more information...

Guess you like

Origin blog.csdn.net/dcj19980805/article/details/114925910