u012833537
2019-07-02 22:15
采纳率: 100%
浏览 381

求:把字符串变成树节点的算法 C# 或者JAVA

求:把字符串变成树节点的代码
数码家居>电子类>汽车用品>车用收纳箱
数码家居>电子类>汽车用品>车载香水/香薰
数码家居>电子类>生活电器>其他
数码家居>电子类>生活电器>净水设备
数码家居>电子类>生活电器>吸尘器
数码家居>电子类>生活电器>扫地机器人
数码家居>电子类>生活电器>空气净化器
服饰户外>内衣袜品>内裤
服饰户外>内衣袜品>塑身衣/裤
服饰户外>内衣袜品>文胸
服饰户外>内衣袜品>睡衣/家居服
服饰户外>内衣袜品>袜子
服饰户外>女装>T恤
服饰户外>女装>休闲裤

比如  数码家居>电子类>汽车用品>车用收纳箱

数码家居 1 电子类 2 汽车用品3 车用收纳箱4

数码家居1 电子类 2 汽车用品3 车载香水/香薰5

服饰户外>内衣袜品>内裤
服饰户外 9 内衣袜品 10 内裤 11


  class CNode
{
    private int _NodeID = 0;

    public int NodeID
    {
        get { return _NodeID; }
        set { _NodeID = value; }
    }
    private string _NodeName = "";

    public string NodeName
    {
        get { return _NodeName; }
        set { _NodeName = value; }
    }
    private int _   Parentid = 0;

    public int  Parentid
    {
        get { return _  parentid; }
        set { _ parentid = value; }
    }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • blownewbee 2019-07-03 10:12
    已采纳

    完整代码+exe采纳我的回答以后留下email给你

    其他人如果需要:https://download.csdn.net/download/caozhy/11271091

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Q767996
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            class CNode
            {
                private int _NodeID = 0;
    
                public int NodeID
                {
                    get { return _NodeID; }
                    set { _NodeID = value; }
                }
                private string _NodeName = "";
    
                public string NodeName
                {
                    get { return _NodeName; }
                    set { _NodeName = value; }
                }
                private int _parentid = 0;
    
                public int Parentid
                {
                    get { return _parentid; }
                    set { _parentid = value; }
                }
                public override string ToString()
                {
                    return NodeName;
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var nodes = new List<CNode>();
                int id = 0;
                foreach (string s in textBox1.Lines)
                {
                    string[] paths = s.Split('>');
                    CNode pnode = null;
                    for (int i = 0; i < paths.Length; i++)
                    {
                        var pnode1 = nodes.FirstOrDefault(x => x.NodeName == paths[i]);
                        if (pnode1 == null)
                        {
                            pnode1 = new CNode() { NodeID = id++, NodeName = paths[i], Parentid = pnode == null ? -1 : pnode.NodeID };
                            nodes.Add(pnode1);
                        }
                        pnode = pnode1;
                    }            
                }
                loadroot(nodes);
            }
    
            private void loadroot(List<CNode> list)
            {
                foreach (var item in list.Where(x => x.Parentid == -1))
                {
                    var tn = treeView1.Nodes.Add(item.NodeName);
                    loadchild(list, tn, item);
                }
            }
    
            private void loadchild(List<CNode> list, TreeNode tn, CNode node)
            {
                foreach (var item in list.Where(x => x.Parentid == node.NodeID))
                {
                    var tn1 = tn.Nodes.Add(item.NodeName);
                    loadchild(list, tn1, item);
                }
            }
        }
    }
    
    

    图片说明

    完整代码+exe采纳我的回答以后留下email给你

    点赞 打赏 评论
  • 路过乌云 2019-07-03 09:52

    能不能具体描述下 想要什么样的效果
    是不是这样
    {
    "T恤": {
    "nodeid": "46",
    "parentid": "45"
    },
    "休闲裤": {
    "nodeid": "49",
    "parentid": "45"
    },
    "其他": {
    "nodeid": "12",
    "parentid": "11"
    },
    "内衣袜品": {
    "nodeid": "30",
    "parentid": "29"
    },
    "内裤": {
    "nodeid": "31",
    "parentid": "30"
    },
    "净水设备": {
    "nodeid": "16",
    "parentid": "11"
    },
    "吸尘器": {
    "nodeid": "20",
    "parentid": "11"
    },
    "塑身衣/裤": {
    "nodeid": "34",
    "parentid": "30"
    },
    "女装": {
    "nodeid": "45",
    "parentid": "29"
    },
    "扫地机器人": {
    "nodeid": "24",
    "parentid": "11"
    },
    "数码家居": {
    "nodeid": "1",
    "parentid": "0"
    },
    "文胸": {
    "nodeid": "37",
    "parentid": "30"
    },
    "服饰户外": {
    "nodeid": "29",
    "parentid": "0"
    },
    "汽车用品": {
    "nodeid": "3",
    "parentid": "2"
    },
    "生活电器": {
    "nodeid": "11",
    "parentid": "2"
    },
    "电子类": {
    "nodeid": "2",
    "parentid": "1"
    },
    "睡衣/家居服": {
    "nodeid": "40",
    "parentid": "30"
    },
    "空气净化器": {
    "nodeid": "28",
    "parentid": "11"
    },
    "袜子": {
    "nodeid": "43",
    "parentid": "30"
    },
    "车用收纳箱": {
    "nodeid": "4",
    "parentid": "3"
    },
    "车载香水/香薰": {
    "nodeid": "8",
    "parentid": "3"
    }
    }

    public class TreeStringParse {
        public static void main(String[] args) {
            String ss="数码家居>电子类>汽车用品>车用收纳箱\n" +
                    "数码家居>电子类>汽车用品>车载香水/香薰\n" +
                    "数码家居>电子类>生活电器>其他\n" +
                    "数码家居>电子类>生活电器>净水设备\n" +
                    "数码家居>电子类>生活电器>吸尘器\n" +
                    "数码家居>电子类>生活电器>扫地机器人\n" +
                    "数码家居>电子类>生活电器>空气净化器\n" +
                    "服饰户外>内衣袜品>内裤\n" +
                    "服饰户外>内衣袜品>塑身衣/裤\n" +
                    "服饰户外>内衣袜品>文胸\n" +
                    "服饰户外>内衣袜品>睡衣/家居服\n" +
                    "服饰户外>内衣袜品>袜子\n" +
                    "服饰户外>女装>T恤\n" +
                    "服饰户外>女装>休闲裤";
            String[] split = ss.split("\n");
            Map<String,Map<String,String>> map=new HashMap<>();
            int id=1;
            for(String s:split){
                String[] split1 = s.split(">");
                for(int j=0;j<split1.length;j++){
                    String name = split1[j];
                    Map<String, String> node = map.get(name);
                    if(node==null){
                        node=new HashMap<>();
                        node.put("nodeid",id+"");
                        if(j==0){
                            //最初节点
                            node.put("parentid","0");
                        }else{
    //                        找到上级
                            String parentName = split1[j - 1];
                            Map<String, String> parentNode = map.get(parentName);
                            String parentNodeid = parentNode.get("nodeid");
                            node.put("parentid",parentNodeid);
                        }
                        map.put(name,node);
                    }
                    id++;
                }
            }
            String s = JSON.toJSONString(map);
            System.out.println(s);
        }
    }
    
    点赞 打赏 评论
  • 路过乌云 2019-07-03 14:34
    package wyj;
    
    import com.alibaba.fastjson.JSON;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class TreeStringParse {
        public static void main(String[] args) {
            String ss="数码家居>电子类>汽车用品>车用收纳箱\n" +
                    "数码家居>电子类>汽车用品>车载香水/香薰\n" +
                    "数码家居>电子类>生活电器>其他\n" +
                    "数码家居>电子类>生活电器>净水设备\n" +
                    "数码家居>电子类>生活电器>吸尘器\n" +
                    "数码家居>电子类>生活电器>扫地机器人\n" +
                    "数码家居>电子类>生活电器>空气净化器\n" +
                    "服饰户外>内衣袜品>内裤\n" +
                    "服饰户外>内衣袜品>塑身衣/裤\n" +
                    "服饰户外>内衣袜品>文胸\n" +
                    "服饰户外>内衣袜品>睡衣/家居服\n" +
                    "服饰户外>内衣袜品>袜子\n" +
                    "服饰户外>女装>T恤\n" +
                    "服饰户外>女装>休闲裤";
            String[] split = ss.split("\n");
            Map<String,Map<String,String>> map=new HashMap<>();
            int id=1;
            for(String s:split){
                String[] split1 = s.split(">");
                for(int j=0;j<split1.length;j++){
                    String name = split1[j];
                    Map<String, String> node = map.get(name);
                    if(node==null){
                        node=new HashMap<>();
                        node.put("nodeid",id+"");
                        if(j==0){
                            //最初节点
                            node.put("parentid","0");
                        }else{
    //                        找到上级
                            String parentName = split1[j - 1];
                            Map<String, String> parentNode = map.get(parentName);
                            String parentNodeid = parentNode.get("nodeid");
                            node.put("parentid",parentNodeid);
                        }
                        map.put(name,node);
                    }
                    id++;
                }
            }
            String s = JSON.toJSONString(map);
            System.out.println(s);
        }
    }
    输出
    {"电子类":{"nodeid":"2","parentid":"1"},"袜子":{"nodeid":"43","parentid":"30"},"服饰户外":{"nodeid":"29","parentid":"0"},"生活电器":{"nodeid":"11","parentid":"2"},"数码家居":{"nodeid":"1","parentid":"0"},"其他":{"nodeid":"12","parentid":"11"},"车载香水/香薰":{"nodeid":"8","parentid":"3"},"净水设备":{"nodeid":"16","parentid":"11"},"吸尘器":{"nodeid":"20","parentid":"11"},"T恤":{"nodeid":"46","parentid":"45"},"空气净化器":{"nodeid":"28","parentid":"11"},"睡衣/家居服":{"nodeid":"40","parentid":"30"},"扫地机器人":{"nodeid":"24","parentid":"11"},"内裤":{"nodeid":"31","parentid":"30"},"休闲裤":{"nodeid":"49","parentid":"45"},"车用收纳箱":{"nodeid":"4","parentid":"3"},"女装":{"nodeid":"45","parentid":"29"},"塑身衣/裤":{"nodeid":"34","parentid":"30"},"汽车用品":{"nodeid":"3","parentid":"2"},"内衣袜品":{"nodeid":"30","parentid":"29"},"文胸":{"nodeid":"37","parentid":"30"}}
    
    
    点赞 打赏 评论

相关推荐 更多相似问题