我有一個樹結構設置,並希望將其保存到/讀取一個字符串,用最少量的文本(所以XML序列化出)。我爲此設置了一個簡單的(或者我認爲)的結構,但是不知道如何讀取它,所以我的結構很可能不得不改變。讓我用一個例子來演示。樹形結構作爲字符串 - 如何匹配嵌套大括號?
我的樹由X的,Y在下面的示例中的座標,如:
[a,b]
|-----|
[c,d] [e,f]
|-----|-----|
[g,h] [i,j] [k,l]
當我運行我的算法把此樹爲一個字符串,我得到下面的輸出:
a,b(c,d()e,f(g,h()i,j()k,l()))
而且這裏我使用的代碼:
public string SerializeMe()
{
StringBuilder ret = new StringBuilder(this.Value.ToString())
ret.Append("(");
foreach (SimpleTreeNode<T> child in _Children)
{
ret.Append(child.SerializeMe());
}
ret.Append(")");
return ret.ToString();
}
那偉大工程,但現在我不能解析的S重新回到我的樹結構。我可以得到子字符串到第一個大括號並將其轉換爲節點的值,但我不確定如何將字符串的其餘部分拆分爲子節點。有沒有什麼方法可以輕鬆找到開口大括號,然後找到大括號?我研究過一些複雜的正則表達式,我無法正常工作,很快就完全失去了。
有沒有人有任何想法?
編輯:
這裏是我到目前爲止的代碼:
public static SimpleTreeNode<SPoint> ParsePointTree(string input)
{
//if the input string is empty, there is no node here. Return null.
if (string.IsNullOrEmpty(input)) return null;
else
{
//get the value from the first part of the string
string valString = input.Substring(0, input.IndexOf('('));
SPoint value = (SPoint)valString;
SimpleTreeNode<SPoint> node = new SimpleTreeNode<SPoint>(value);
//now we have the child nodes enclosed in brackets
string innerstring = input.Substring(input.IndexOf('('));
List<string> children = new List<string>();
// how do we split innerstring into siblings?? //
foreach (string child in children)
{
node.Children.Add(SimpleTreeNode<SPoint>.ParsePointTree(child));
}
return node;
}
}
我遇到的問題是,我會得到一個必須分成兄弟姐妹的字符串。在上面的示例中,c,d
和e,f
是兄弟姐妹,以(c,d()e,f(g,h()i,j()k,l()))
的形式表示。我需要將此字符串拆分爲c,d()
和e,f(g,h()i,j()k,l())
,這是我卡住的地方。
試過遞歸解析? –
此外,請向我們顯示您的解析代碼,而不是您的序列號代碼... –
馬克艾略特是正確的。請注意,您構建您的輸出遞歸。可能最簡單,如果你遞歸地解析它,並且每個解析級別都建立你的發射(...)對內的子樹。 –