NumberTree.java 5.55 KB
package com.meishu.util.tree;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class NumberTree {

    private String id;

    private List<Node> nodeList;

    private String separator;

    private int idSeq;

    private SerialNumber number;

    public NumberTree(String separator, SerialNumber number) {
        init();
        this.separator = separator;
        this.number = number;

    }

    public void init() {
        idSeq = 1;
        this.separator = ".";
        if (this.nodeList == null) {
            nodeList = new ArrayList<Node>();
        }
    }


    /**
     * <p>功能描述:根据父节点获取所有子节点。</p>
     */
    public List<Node> getChildNodes(Node pNode) {
        String pId = pNode.getId();
        return getChildNodes(pId);
    }

    /**
     * <p>功能描述:根据父节点获取所有子节点。</p>
     */
    public List<Node> getChildNodes(String pId) {
        List<Node> childNodes = new ArrayList<Node>();
        for (Node n : nodeList) {
            if (pId.equals(n.getParentId())) {
                childNodes.add(n);
            }
        }

        return childNodes;
    }

    /**
     * <p>功能描述:获取本级值最大的节点。</p>
     */
    public Node getMaxNodeForThisLevel(Node pNode) {
        List<Node> childList = getChildNodes(pNode);
        Node root = getRoot(nodeList);
        if (childList.size() <= 0) {
            return null;
        }
        Node maxNode = root;
        for (Node node : childList) {
            if (maxNode.getNumber().compareTo(node.getNumber()) < 0) {
                maxNode = node;
            }
        }
        return maxNode;
    }

    /**
     * <p>功能描述:生成下一个子节点。</p>
     */
    public Node generateNextChildNode(Node node) {
        Node newNode = null;
        Node maxNode = getMaxNodeForThisLevel(node);
        String nextNumber = number.firstNumber();
        int level = node.getLevel();
        if (maxNode != null && !"0".equals(maxNode.getId())) {//本级存在子节点,且非根节点
            nextNumber = number.produceNext(maxNode.getNumber());
            level = maxNode.getLevel();
        }
        newNode = new Node(String.valueOf(++idSeq), nextNumber, node.getId(), level);

        generateNodeText(newNode, nextNumber);

        return newNode;
    }


    /**
     *
     * <p>功能描述:获取父节点。</p>
     */
    public Node getParentNode(Node node) {
        for (Node n : nodeList) {
            if (node.getParentId() == n.getId()) {
                return n;
            }
        }
        return node;
    }

    /**
     * <p>功能描述:生成节点路径。</p>
     */
    public void generateNodeText(Node node, String text) {

        if (node == null || "0".equals(node.getId())) {
            return;
        }

        Node pNode = getParentNode(node);

        if (!"0".equals(pNode.getId())) {
            text = pNode.getText() + separator + text;
        }

        node.setText(text);
    }

    /**
     * <p>功能描述:遍历所有树节点。</p>
     */
    public void traverseNodeList(Node node) {
        if(node==null){
            node = getRoot(nodeList);
        }
        List<Node> childNodes = getChildNodes(node);
        System.out.println(node.getText());
        if (childNodes.size() > 0) {
            for (Node n : childNodes) {
                traverseNodeList(n);
            }
        }
    }
    public static void main(String[] args) {
        SerialNumber number = new SerialNumber();
        NumberTree treeNode = new NumberTree(".", number);

        addSomeNodes(treeNode);
        treeNode.traverseNodeList(null);
    }

    /**
     * <p>功能描述:获取根节点。</p>
     */
    public Node getRoot(List<Node> nodeList) {
        Node root = null;
        if (nodeList.size() <= 0 || (root = getNodeById(nodeList, "0")) == null) {
            root = createRoot();
            nodeList.add(root);
        }
        return root;
    }

    private Node getNodeById(List<Node> nodeList, String id) {
        Node node = null;
        if(id!=null){
            for (Node n : nodeList) {
                if (id.equals(n.getId())) {
                    node = n;
                    break;
                }
            }
        }
        return node;
    }

    private Node createRoot() {
        Node root = new Node("0", number.rootNumber(), "-1", 0);
        root.setText("0");
        return root;
    }

    /**
     * <p>功能描述:测试添加节点。</p>
     */
    private static Node addSomeNodes(NumberTree tree) {
        Node root = tree.getRoot(tree.nodeList);
        Node node1 = getNextNode(tree, root);//1
        Node node2 = getNextNode(tree, root);//2
        Node node3 = getNextNode(tree, root);//3
        Node node11 = getNextNode(tree, node1);//1.1
        Node node12 = getNextNode(tree, node1);//1.2
        Node node21 = getNextNode(tree, node2);//2.1
        Node node211 = getNextNode(tree, node21);//2.1.1
        Node node212 = getNextNode(tree, node21);//2.1.2
        Node node22 = getNextNode(tree, node2);//2.2
        Node node221 = getNextNode(tree, node22);//2.2.1
        Node node31 = getNextNode(tree, node3);
        Node node32 = getNextNode(tree, node3);
        Node node311 = getNextNode(tree, node31);
        Node node3111 = getNextNode(tree, node311);
        return root;
    }

    public static Node getNextNode(NumberTree tree, Node pNode) {
        Node node = tree.generateNextChildNode(pNode);
        if (node != null) {
            tree.nodeList.add(node);
        }
        return node;
    }

}