Construction of multi-tree, conditional query, modify and delete nodes

Multi-tree construction query, add, modify, delete

1. Database table design

Generally, a tree-type node information management table only requires an id (current node id) and a parent_id (parent node id is enough). The other basic information fields are as needed.

Note that I have set a default value of -1 for the parent_id field here, which means it is not in this tree. If parent_id is 0, it means it is the root node.

For example, a management module here maintains this table. Both the addable node 2 and the addable node 3 are not currently in the tree, but they can be added to the tree later (when adding, only the parent_id is - The data of 1 means that these data can be added to the tree as nodes. The new addition is actually to pass the id and parent_id from the front end, and then update the parent_id according to the id)

2. Add entity classes corresponding to database tables

The code logic of this part of the entity class is similar. The code here refers to the code of the following blog:

Multitree Operation Blog

public class TreeNode implements Serializable {
    
      
    private int parentId;  
    private int selfId;  
    protected String nodeName;  
    protected Object obj;  
    protected TreeNode parentNode;  
    protected List<TreeNode> childList;  
  
    public TreeNode() {
    
      
        initChildList();  
    }  
  
    public TreeNode(TreeNode parentNode) {
    
      
        this.getParentNode();  
        initChildList();  
    }  
  
    public boolean isLeaf() {
    
      
        if (childList == ) {
    
      
            return true;  
        } else {
    
      
            if (childList.isEmpty()) {
    
      
                return true;  
            } else {
    
      
                return false;  
            }  
        }  
    }  
  
    /* 插入一个child节点到当前节点中 */  
    public void addChildNode(TreeNode treeNode) {
    
      
        initChildList();  
        childList.add(treeNode);  
    }  
  
    public void initChildList() {
    
      
        if (childList == )  
            childList = new ArrayList<TreeNode>();  
    }  
  
    public boolean isValidTree() {
    
      
        return true;  
    }  
  
    /* 返回当前节点的父辈节点集合 */  
    public List<TreeNode> getElders() {
    
      
        List<TreeNode> elderList = new ArrayList<TreeNode>();  
        TreeNode parentNode = this.getParentNode();  
        if (parentNode == ) {
    
      
            return elderList;  
        } else {
    
      
            elderList.add(parentNode);  
            elderList.addAll(parentNode.getElders());  
            return elderList;  
        }  
    }  
  
    /* 返回当前节点的晚辈集合 */  
    public List<TreeNode> getJuniors() {
    
      
        List<TreeNode> juniorList = new ArrayList<TreeNode>();  
        List<TreeNode> childList = this.getChildList();  
        if (childList == ) {
    
      
            return juniorList;  
        } else {
    
      
            int childNumber = childList.size();  
            for (int i = 0; i < childNumber; i++) {
    
      
                TreeNode junior = childList.get(i);  
                juniorList.add(junior);  
                juniorList.addAll(junior.getJuniors());  
            }  
            return juniorList;  
        }  
    }  
  
    /* 返回当前节点的孩子集合 */  
    public List<TreeNode> getChildList() {
    
      
        return childList;  
    }  
  
    /* 删除节点和它下面的晚辈 */  
    public void deleteNode() {
    
      
        TreeNode parentNode = this.getParentNode();  
        int id = this.getSelfId();  
  
        if (parentNode != ) {
    
      
            parentNode.deleteChildNode(id);  
        }  
    }  
  
    /* 删除当前节点的某个子节点 */  
    public void deleteChildNode(int childId) {
    
      
        List<TreeNode> childList = this.getChildList();  
        int childNumber = childList.size();  
        for (int i = 0; i < childNumber; i++) {
    
      
            TreeNode child = childList.get(i);  
            if (child.getSelfId() == childId) {
    
      
                childList.remove(i);  
                return;  
            }  
        }  
    }  
  
    /* 动态的插入一个新的节点到当前树中 */  
    public boolean insertJuniorNode(TreeNode treeNode) {
    
      
        int juniorParentId = treeNode.getParentId();  
        if (this.parentId == juniorParentId) {
    
      
            addChildNode(treeNode);  
            return true;  
        } else {
    
      
            List<TreeNode> childList = this.getChildList();  
            int childNumber = childList.size();  
            boolean insertFlag;  
  
            for (int i = 0; i < childNumber; i++) {
    
      
                TreeNode childNode = childList.get(i);  
                insertFlag = childNode.insertJuniorNode(treeNode);  
                if (insertFlag == true)  
                    return true;  
            }  
            return false;  
        }  
    }  
  
    /* 找到一颗树中某个节点 */  
    public TreeNode findTreeNodeById(int id) {
    
      
        if (this.selfId == id)  
            return this;  
        if (childList.isEmpty() || childList == ) {
    
      
            return ;  
        } else {
    
      
            int childNumber = childList.size();  
            for (int i = 0; i < childNumber; i++) {
    
      
                TreeNode child = childList.get(i);  
                TreeNode resultNode = child.findTreeNodeById(id);  
                if (resultNode != ) {
    
      
                    return resultNode;  
                }  
            }  
            return ;  
        }  
    }  
  
    /* 遍历一棵树,层次遍历 */  
    public void traverse() {
    
      
        if (selfId < 0)  
            return;  
        print(this.selfId);  
        if (childList ==  || childList.isEmpty())  
            return;  
        int childNumber = childList.size();  
        for (int i = 0; i < childNumber; i++) {
    
      
            TreeNode child = childList.get(i);  
            child.traverse();  
        }  
    }  
  
    public void print(String content) {
    
      
        System.out.println(content);  
    }  
  
    public void print(int content) {
    
      
        System.out.println(String.valueOf(content));  
    }  
  
    public void setChildList(List<TreeNode> childList) {
    
      
        this.childList = childList;  
    }  
  
    public int getParentId() {
    
      
        return parentId;  
    }  
  
    public void setParentId(int parentId) {
    
      
        this.parentId = parentId;  
    }  
  
    public int getSelfId() {
    
      
        return selfId;  
    }  
  
    public void setSelfId(int selfId) {
    
      
        this.selfId = selfId;  
    }  
  
    public TreeNode getParentNode() {
    
      
        return parentNode;  
    }  
  
    public void setParentNode(TreeNode parentNode) {
    
      
        this.parentNode = parentNode;  
    }  
  
    public String getNodeName() {
    
      
        return nodeName;  
    }  
  
    public void setNodeName(String nodeName) {
    
      
        this.nodeName = nodeName;  
    }  
  
    public Object getObj() {
    
      
        return obj;  
    }  
  
    public void setObj(Object obj) {
    
      
        this.obj = obj;  
    }  
}  

3. Create a new multi-tree tree tool class

Through the previous three steps, a multi-tree has been constructed, as well as some basic operations of the multi-tree.

For example, the TreeNode root node here is actually the tree that has been built and exists in this attribute.

public class TreeHelper {
    
      
  
    private TreeNode root;  
    private List<TreeNode> tempNodeList;  
    private boolean isValidTree = true;  
  
    public TreeHelper() {
    
      
    }  
  
    public TreeHelper(List<TreeNode> treeNodeList) {
    
      
        tempNodeList = treeNodeList;  
        generateTree();  
    }  
  
    public static TreeNode getTreeNodeById(TreeNode tree, int id) {
    
      
        if (tree == )  
            return ;  
        TreeNode treeNode = tree.findTreeNodeById(id);  
        return treeNode;  
    }  
  
    /** generate a tree from the given treeNode or entity list */  
    public void generateTree() {
    
      
        HashMap nodeMap = putNodesIntoMap();  
        putChildIntoParent(nodeMap);  
    }  
  
    /** 
     * put all the treeNodes into a hash table by its id as the key 
     *  
     * @return hashmap that contains the treenodes 
     */  
    protected HashMap putNodesIntoMap() {
    
      
        int maxId = Integer.MAX_VALUE;  
        HashMap nodeMap = new HashMap<String, TreeNode>();  
        Iterator it = tempNodeList.iterator();  
        while (it.hasNext()) {
    
      
            TreeNode treeNode = (TreeNode) it.next();  
            int id = treeNode.getSelfId();  
            if (id < maxId) {
    
      
                maxId = id;  
                this.root = treeNode;  
            }  
            String keyId = String.valueOf(id);  
  
            nodeMap.put(keyId, treeNode);  
            // System.out.println("keyId: " +keyId);  
        }  
        return nodeMap;  
    }  
  
    /** 
     * set the parent nodes point to the child nodes 
     *  
     * @param nodeMap 
     *            a hashmap that contains all the treenodes by its id as the key 
     */  
    protected void putChildIntoParent(HashMap nodeMap) {
    
      
        Iterator it = nodeMap.values().iterator();  
        while (it.hasNext()) {
    
      
            TreeNode treeNode = (TreeNode) it.next();  
            int parentId = treeNode.getParentId();  
            String parentKeyId = String.valueOf(parentId);  
            if (nodeMap.containsKey(parentKeyId)) {
    
      
                TreeNode parentNode = (TreeNode) nodeMap.get(parentKeyId);  
                if (parentNode == ) {
    
      
                    this.isValidTree = false;  
                    return;  
                } else {
    
      
                    parentNode.addChildNode(treeNode);  
                    // System.out.println("childId: " +treeNode.getSelfId()+" parentId: "+parentNode.getSelfId());  
                }  
            }  
        }  
    }  
  
    /** initialize the tempNodeList property */  
    protected void initTempNodeList() {
    
      
        if (this.tempNodeList == ) {
    
      
            this.tempNodeList = new ArrayList<TreeNode>();  
        }  
    }  
  
    /** add a tree node to the tempNodeList */  
    public void addTreeNode(TreeNode treeNode) {
    
      
        initTempNodeList();  
        this.tempNodeList.add(treeNode);  
    }  
  
    /** 
     * insert a tree node to the tree generated already 
     *  
     * @return show the insert operation is ok or not 
     */  
    public boolean insertTreeNode(TreeNode treeNode) {
    
      
        boolean insertFlag = root.insertJuniorNode(treeNode);  
        return insertFlag;  
    }  
  
    /** 
     * adapt the entities to the corresponding treeNode 
     *  
     * @param entityList 
     *            list that contains the entities 
     *@return the list containg the corresponding treeNodes of the entities 
     */  
    public static List<TreeNode> changeEnititiesToTreeNodes(List entityList) {
    
      
        OrganizationEntity orgEntity = new OrganizationEntity();  
        List<TreeNode> tempNodeList = new ArrayList<TreeNode>();  
        TreeNode treeNode;  
  
        Iterator it = entityList.iterator();  
        while (it.hasNext()) {
    
      
            orgEntity = (OrganizationEntity) it.next();  
            treeNode = new TreeNode();  
            treeNode.setObj(orgEntity);  
            treeNode.setParentId(orgEntity.getParentId());  
            treeNode.setSelfId(orgEntity.getOrgId());  
            treeNode.setNodeName(orgEntity.getOrgName());  
            tempNodeList.add(treeNode);  
        }  
        return tempNodeList;  
    }  
  
    public boolean isValidTree() {
    
      
        return this.isValidTree;  
    }  
  
    public TreeNode getRoot() {
    
      
        return root;  
    }  
  
    public void setRoot(TreeNode root) {
    
      
        this.root = root;  
    }  
  
    public List<TreeNode> getTempNodeList() {
    
      
        return tempNodeList;  
    }  
  
    public void setTempNodeList(List<TreeNode> tempNodeList) {
    
      
        this.tempNodeList = tempNodeList;  
    }  
  
} 

4. Conditional query of multi-tree tree

Here is a reference to the code I rewrote myself. Since it is in the cloud desktop, I have included screenshots. The logic can be connected with the above code.

For example, this column builds a shareholder tree and queries the tree based on the shareholder name. First, add a new isRead field in the entity class with a default value of 0. After the query is found, it will be set to 1, and finally all child nodes will be deleted. The node whose isRead is 0 is returned to the front end.

5. New nodes of multi-tree

  • First, based on the condition that parent_id is -1, find out all the node data that are not in the tree, and then add the nodes into the tree.

  • According to the node id, change the parent_id to the parent node id passed by the front end and the addition is successful.

6. Node deletion in multi-tree

What is done here is relatively simple. The front end passes the current node id. When deleting, check whether there is a node whose parent_id is the current node id. If there is, the current node will not be deleted and the user will be reminded to delete the lower node first.

7. Multi-tree modification

It is currently relatively simple here and only allows him to modify the basic information of the node. The front-end only needs to pass the node ID and then modify it.

Guess you like

Origin blog.csdn.net/qq_45925197/article/details/131085098