algorithm - 我想从字符串创建通用列表

标签 algorithm generics

我想从字符串创建通用列表

输入字符串是(a(b,c,u),d,e(f),g(),h,i(j(k,l,m(n))),r)

我的类(class)是

public class Node
{
    public string Name; // method name
  //  public decimal Time; // time spent in method
    public List<Node> Children;
}

子节点用()表示。

例子:a为父节点,b,c u为子节点;将保存在List<Node>与它的父级相同,它有 j 作为子级,k、l 和 m 作为子级 j。

它很像树

<.>
 |---<a>
 |    |--- b
 |    |--- c
 |    |--- u
 |--- d
 |---<e>
 |    |--- f
 |---<g>
 |--- h
 |---<i>
 |    |---<j>
 |    |    |--- k
 |    |    |--- l
 |    |    |---<m>
 |    |    |    |--- n
 |--- r

最佳答案

您的最终结果 Node 数据结构最终看起来类似于一棵树。要实现您想要的效果,您可以使用递归函数。

这是一个这样的函数的例子(带有注释):

    //Recursive Function that creates a list of Node objects and their    
    //children from an input string
    //Returns: List of Nodes
    //Param string input: The input string used to generate the List
    //Param int index: The index to start looping through the string
    public List<Node> CreateNodeList(string input, int index)
    {
        List<Node> nodeList = new List<Node>();
        Node currentNode = new Node();
        StringBuilder nameBuilder = new StringBuilder();

        //Start looping through the characters in the string at the 
        //specified index
        for(int i = index; i < array.size; i++)
        {
            switch(input[i])
            {
                //If we see an open bracket we need to use the 
                //proceeding list to create the current nodes children
                //We do this by recursively calling this function 
                //(passing the input string and the next index as 
                //parameters) and setting the children property to b 
                //the return value
                case ‘(‘:
                    currentNode.Children = CreateNodeList(input, i+1);
                    i = input.IndexOf(‘)’, i);
                    break;
                //If we see a closed bracket we create a new Node based 
                //of the existing string, add it to the list, and then 
                //return the list
                case ‘)’:
                    currentNode.Name = nameBuilder.ToString();
                    nodeList.Add(currentNode);
                    nameBuilder.Clear();
                    return nodeList;
                //if we see a comma we create a new node object based 
                //off the current string and add it to the list
                case ‘,’:
                    currentNode.Name = nameBuilder.ToString();
                    nodeList.Add(currentNode);
                    nameBuilder.Clear();
                    currentNode = new Node();
                    break;
                //Any other character we see must be for the name   
                //of a node, so we will append it to our string 
                //builder and continue looping
                default:
                    nameBuilder.Append(input[i]);
            }
        }

        //We will probably never reach here since your input string 
        //usually ends in  ‘)’ but just in case we finish by returning 
        //the list
        return nodeList;
    }

    //An example of how to use this recursive function
    public static void main()
    {
        //Your input string
        string input = “(a(b,c,u),d,e(f),g(),h,i(j(k,l,m(n))),r)”;
        //Call our function with the input string and 1 as arguments
        //We use 1 to skip the first ‘(‘ which is a index 0
        List<Node> nodeList = CreateNodeList(input, 1);
        //Do stuff with list here 
    }

此函数跟踪节点名称的字符,创建新节点并将它们添加到列表中,每次它看到 ',' 或 ')'(看到 ')' 时返回列表))并且它通过递归调用函数并使用其返回值,当它看到“(”字符时也会填充节点子节点。一个主要缺点是您必须跟踪您所在的索引。

此函数是徒手编写的,但它与 C# 非常相似(您没有指定语言,所以我希望这对您有所帮助。)

我希望这对您有所帮助,并且正是您正在寻找的:)

关于algorithm - 我想从字符串创建通用列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34119121/

相关文章:

c++ - 从输入中创建最大可能的数字 - cout 的实现问题

algorithm - 如何同步信号接收器时钟?

java - 如何在比较器的映射中使用泛型以避免警告

scala - 如何在 Scala 中实例化由类型参数表示的类型实例

scala - 如何通过scala中的类方法传递类型参数?

javascript - 具有半径(模糊?)的颜色选择器算法

performance - 快速排序:枢轴元素的选择、输入和最坏情况下的性能有何关系?

java - 如何使用两个泛型获取参数化类型的类实例

java - 泛型映射 - 避免大量强制转换

javascript - 如何正确实现递归算法?