JAVA树形数据结构的实现

ryan
  • 19

已知数据结构

[
    "测试/测试/测试2",
    "工单/问题反馈309",
    "工单/问题反馈565",
    "工单/问题反馈54",
    "工单/问题反馈310",
    "测试专用/123456/test/123"
]

想要转成树形结构,输出如下

测试
  测试
    测试2
工单
  问题反馈309
  问题反馈565
  问题反馈54
  问题反馈310
测试专用
  123456
    test
      123
回复
阅读 2.7k
4 个回答

首先,抛开编程语言。
你用文字/伪代码的方式,描述一下你的思路。

你是不会算法,还是不会编程语言?

你可以参考下jdk 中XML 的API。

package testng;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;

public class Test {
    
    
    /**
     * 添加节点
     * @param nodeMap
     * @param parentNodeName 父节点名称
     * @param nodeName
     */
    public static void addNode(Map<String,Node> nodeMap,String parentNodeName,String nodeName){
        Node parentNode = null != parentNodeName ? nodeMap.get(parentNodeName) : null;
        Node node = nodeMap.get(nodeName);
        if(null != node){
            return;
        }
        node = new Node();
        node.setName(nodeName);
        nodeMap.put(nodeName, node);
        if(null != parentNode){
            parentNode.addChild(node);
            node.setParent(parentNode);
        }
    }
    public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
        
        String [] arrays = new String[]{
                     "测试/测试1/测试2",
                    "工单/问题反馈309",
                    "工单/问题反馈565",
                    "工单/问题反馈54",
                    "工单/问题反馈310",
                    "测试专用/123456/test/123"
        };
        Map<String,Node> resultMap = new HashMap<String,Node>();
        for(String line : arrays){
            String nodeNameArray [] = line.split("/"); 
            String preNodeName = null;
            for(String curNodeName : nodeNameArray){
                addNode(resultMap,preNodeName,curNodeName);
                preNodeName = curNodeName;
            }
        }
        for(Map.Entry<String,Node> entry : resultMap.entrySet()){
            Node node = entry.getValue();
            if(null == node.getParent()){
                print(node,"");
            }
        }
        
    }
    
    /**
     * 递归打印node
     * @param node
     * @param blank
     */
    public static void print(Node node,String blank){
        System.out.println(blank + node.getName());
        if(node.getChilds() == null || node.getChilds().isEmpty()){
            return;
        }
        for(Node cur : node.getChilds()){
            print(cur,blank + "   ");
        }
    }
    static class Node{
        private String name;
        private int index;
        private List<Node> childs;
        private Node parent;
        
        public List<Node> getChilds() {
            return childs;
        }
        public void addChild(Node child) {
             if(null == childs){
                 childs = new ArrayList<Node>();
             }
             childs.add(child);
        }
        public Node getParent() {
            return parent;
        }
        public void setParent(Node parent) {
            this.parent = parent;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getIndex() {
            return index;
        }
        public void setIndex(int index) {
            this.index = index;
        }
    }
}
打印结果:
测试
   测试1
      测试2
测试专用
   123456
      test
         123
工单
   问题反馈309
   问题反馈565
   问题反馈54
   问题反馈310

实现代码

import com.alibaba.fastjson.JSON;

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

/**
 * @author ryan
 * @version $Id Test.java, v 0.1 2019-10-14 10:21 ryan Exp $
 */
public class Test {
    public static void main(String[] args) {
        String[] arrays = new String[] { "测试/测试1/测试2", "工单/问题反馈309", "工单/问题反馈565", "工单/问题反馈54",
                                         "工单/问题反馈310", "测试专用/123456/test/123" };

        Node node = new Node("root");

        for (String array : arrays) {
            List<Node> eachList = null;
            for (String str : array.split("/")) {
                if (eachList == null) {
                    eachList = node.getSubNodeList();
                }
                eachList = buildSubNode(str, eachList);
            }
        }

        System.out.println(JSON.toJSONString(node));

    }

    /**
     * 构建子节点
     *
     * @param nodeName 节点名称
     * @param subNodeList 子节点集合
     * @return 子节点
     */
    private static List<Node> buildSubNode(String nodeName, List<Node> subNodeList) {
        // 如果已经存在
        for (Node node : subNodeList) {
            if (node.getName().equals(nodeName)) {
                return node.getSubNodeList();
            }
        }

        Node node = new Node(nodeName);
        subNodeList.add(node);
        return node.getSubNodeList();
    }

    static class Node {
        private String name;

        List<Node>     subNodeList = new ArrayList<Node>();

        Node(String name) {
            this.name = name;
        }

        /**
         * Getter method for property <tt>name</tt>.
         *
         * @return property value of name
         */
        public String getName() {
            return name;
        }

        /**
         * Setter method for property <tt>name</tt>.
         *
         * @param name value to be assigned to property name
         */
        public void setName(String name) {
            this.name = name;
        }

        /**
         * Getter method for property <tt>subNodeList</tt>.
         *
         * @return property value of subNodeList
         */
        public List<Node> getSubNodeList() {
            return subNodeList;
        }

        /**
         * Setter method for property <tt>subNodeList</tt>.
         *
         * @param subNodeList value to be assigned to property subNodeList
         */
        public void setSubNodeList(List<Node> subNodeList) {
            this.subNodeList = subNodeList;
        }
    }
}

打印结果

{"name":"root","subNodeList":[{"name":"测试","subNodeList":[{"name":"测试1","subNodeList":[{"name":"测试2","subNodeList":[]}]}]},{"name":"工单","subNodeList":[{"name":"问题反馈309","subNodeList":[]},{"name":"问题反馈565","subNodeList":[]},{"name":"问题反馈54","subNodeList":[]},{"name":"问题反馈310","subNodeList":[]}]},{"name":"测试专用","subNodeList":[{"name":"123456","subNodeList":[{"name":"test","subNodeList":[{"name":"123","subNodeList":[]}]}]}]}]}
宣传栏