【问题标题】:Build tree data from dictionary从字典构建树数据
【发布时间】:2013-11-20 04:41:38
【问题描述】:

我的结构如下

{
   "Start": "CDG",
   "AirportsRoutes": 
{
    "BKK" : ["SIN", "DXB", "CDG", "SFO"],
    "CDG" : ["LHR", "SIN"],
    "JFK" : ["CDG", "DXB"],
    "JNB" : ["SIN"],
    "SIN" : ["SFO", "BKK"],
    "SFO" : ["JFK", "BKK", "LHR"],
    "LHR" : ["CDG", "JFK", "DXB"],
    "YYZ" : ["SFO", "JFK"],
    "DXB" : ["JNB", "SIN", "BKK"]
}
}

我为AirportsRoutes 创建了字典为Dictionary<string, List<string>>

我想通过使用值作为下一个节点来从这个字典中构建数据树,例如如果使用“CDG”作为根节点。

如果找到与其父节点相同的键,每个叶节点将停止。

CDG->LHR->CDG
   ->LHR->JFK->CDG
   ->LHR->JFK->DXB->JNB->SIN->SFO->JFK
   ->LHR->JFK->DXB->JNB->SIN->SFO->BKK
   ->LHR->JFK->DXB->JNB->SIN->SFO->LHR
   ->LHR->JFK->DXB->JNB->SIN->BKK...
   ->LHR->JFK->DXB->SIN->SFO...
   ->LHR->JFK->DXB->SIN->BKK..
   ->LHR->JFK->DXB->BKK..
   ->LHR->DXB..

每个人都知道如何制作还是我需要将字典更改为其他内容?

【问题讨论】:

  • 这看起来很像 JSON...
  • 那么循环路线呢,(没有退出条件,树永远不会停止)?

标签: c# list dictionary tree


【解决方案1】:

这个小控制台应用程序可以满足您的需求:

public class Program
{
    public class TreeNode<T>
    {
        public List<TreeNode<T>> children = new List<TreeNode<T>>();
        public T thisItem;
        public TreeNode<T> parent;
    }

    public static void Main(string[] args)
    {
        var dict = new Dictionary<string, List<string>>
        {
            {"BKK", new List<string>{"SIN", "DXB", "CDG", "SFO"}},
            {"CDG" , new List<string>{"LHR", "SIN"}},
            {"JFK" , new List<string>{"CDG", "DXB"}},
            {"JNB" , new List<string>{"SIN"}},
            {"SIN" , new List<string>{"SFO", "BKK"}},
            {"SFO" , new List<string>{"JFK", "BKK", "LHR"}},
            {"LHR" , new List<string>{"CDG", "JFK", "DXB"}},
            {"YYZ" , new List<string>{"SFO", "JFK"}},
            {"DXB", new List<string> {"JNB", "SIN", "BKK"}}
        };

        var tree = BuildTree(dict, "CDG");
    }

    public static TreeNode<T> BuildTree<T>(Dictionary<T, List<T>> dictionary, T root)
    {
        var newTree = new TreeNode<T>
        {
            thisItem = root,
        };

        newTree.children = GetChildNodes(dictionary, newTree);
        
        return newTree;
    }

    public static List<TreeNode<T>> GetChildNodes<T>(Dictionary<T, List<T>> dictionary, TreeNode<T> parent)
    {
        var nodeList = new List<TreeNode<T>>();

        if (dictionary.ContainsKey(parent.thisItem))
        {
            foreach (var item in dictionary[parent.thisItem])
            {
                var nodeToAdd = new TreeNode<T>
                {
                    thisItem = item,
                    parent = parent,
                };

                if (!ContainedInParent(parent, item))
                {
                    nodeToAdd.children = GetChildNodes(dictionary, nodeToAdd);
                }

                // output leaf node for debugging!
                if (nodeToAdd.children.Count == 0)
                {
                    Console.WriteLine(NodeString(nodeToAdd));
                }

                nodeList.Add(nodeToAdd);
            }
        }
        
        return nodeList;
    }

    private static string NodeString<T>(TreeNode<T> node)
    {
        return (node.parent != null ? NodeString(node.parent) + "->" : string.Empty) + node.thisItem;
    }

    private static bool ContainedInParent<T>(TreeNode<T> parent, T item)
    {
        var found = false;

        if (parent != null)
        {
            if (parent.thisItem.Equals(item))
            {
                found = true;
            }
            else
            {
                found = ContainedInParent(parent.parent, item);
            }
        }

        return found;
    }
}

输出是:

CDG->LHR->CDG

CDG->LHR->肯尼迪->CDG

CDG->LHR->JFK->DXB->JNB->SIN->SFO->JFK

CDG->LHR->JFK->DXB->JNB->SIN->SFO->BKK->SIN

CDG->LHR->JFK->DXB->JNB->SIN->SFO->BKK->DXB

CDG->LHR->JFK->DXB->JNB->SIN->SFO->BKK->CDG

…………

【讨论】:

    猜你喜欢
    • 2021-11-10
    • 2015-10-18
    • 2013-12-10
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    相关资源
    最近更新 更多