表示树状结构的最有效方法 [英] most efficient way to represent tree like structure

查看:86
本文介绍了表示树状结构的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析一个具有树状结构的字符串

I am parsing a string that has a tree like structure

例如| 123 ^ M | test ^ test〜test ^ t& y | test ^ test ^ test〜test ^ test |

Ex. |123^M|test^test~test^t&y|test^test^test~test^test|

基本节点是整个字符串

个子节点是通过'|'拆分而来的字符串

child nodes of that are strings obtained from split by '|'

子节点每个都用'〜'分隔.

child nodes of those strings are each string split by '~'

每个子节点都是用'^'分隔的字符串

child nodes of those are each string split by '^'

每个子节点都是用'&'

child nodes of those are each string split by '&'

这是树中最大级别的数量.

That's the maximum number of levels there are in the tree.

我正在尝试创建一个高效且快速的程序来处理字符串,例如屏蔽某些字符串,修改或按某个值进行搜索.

I'm trying to create an efficient and fast program to do things with the strings, like mask certain strings, or modify, or search by some value.

我创建了基本上是字符串Lists of Lists的类.另一个选择是使用XML字符串并使用XML函数直接指定要访问的节点.

I created classes that is basically string Lists of Lists.  Another option was to use an XML string and use XML functions to directly specify which node you want to access. 

另一个问题是,我有一个带有树状视图的GUI界面,用户可以在其中选中例如要屏蔽的节点.但是我也可以为此使用数据网格.

Another problem is that I have a GUI interface with a treeview where the user can checkbox which nodes they want to mask for example.  But I can also use a datagrid for that. 

明智地处理此问题的最佳方法是什么?我可能正在处理大型字符串,其中很多.

Performance wise, what is the best way to handle this?  I could potentially be processing large strings, and many of them.

推荐答案

如果您正在寻找快速且节省内存的成本,那么我就不会使用Visaul Studio类的Structures.这些类的目的是使编程变得容易并且能够重用代码,而不是快速高效地执行.我的首选是 使用70年代为Unix编程开发的算法(例如YACC和LEX)进行有效编程.

If you are looking for fast and memory effiecient cost then I wouldn't use visaul Studio class Structures.  The classes are designed to make it easy to program and to be able to reuse code, not to execute fast and effieciently.  My prefernce is to use algorithms that were developed for Unix Programming in the 70's like YACC and LEX for effiecient programming.

您可以使用两个字符串方法SPLIT和INDEXOF来完成您所要求的一切.参见下面的代码

You can do everything you are asking using two string methods SPLIT and INDEXOF.  See the code below

          char []分隔符= {'|','〜','^','&'};
         字符串mystr ="| 123 ^ M | test ^ test〜test ^ t& y | test ^ test ^ test〜test ^ test |";
          foreach(分隔符中的char分隔符)
          {
            if(mystr.IndexOf(seperator)!= -1)
                                       {
                                    string [] stringarray = mystr.Split(seperator);
                                    foreach(stringarray中的字符串子字符串)
                                    {
                //在这里添加您的代码
                                    }

           char[] seperators = {'|','~','^','&'};
           string mystr = "|123^M|test^test~test^t&y|test^test^test~test^test|";
           foreach(char seperator in seperators)
           {
              if(mystr.IndexOf(seperator) != -1)
             {
                 string[] stringarray = mystr.Split(seperator);
                 foreach(string substring in stringarray)
                 {
                   //add your code here
                 }

            }
         }

             }
          }


这篇关于表示树状结构的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆