你好,假設你有普通的unix路徑樹作爲輸入(如字符串)。樹形結構的路徑樹
root 0
root/file1.txt 1
root/file2.txt 2
root/folder1 3
root/folder1/file3.txt 4
root/folder1/file4.txt 5
e.t.c.
將此字符串轉換爲樹型數據結構的更好方法是什麼?
你好,假設你有普通的unix路徑樹作爲輸入(如字符串)。樹形結構的路徑樹
root 0
root/file1.txt 1
root/file2.txt 2
root/folder1 3
root/folder1/file3.txt 4
root/folder1/file4.txt 5
e.t.c.
將此字符串轉換爲樹型數據結構的更好方法是什麼?
現在我用簡單的樹表示,通過創建自己根節點
template<typename T>
class TreeNode
{
public:
TreeNode() {};
TreeNode(T)
{
value = T;
}
TreeNode(const T& value)
: Value(value)
{
}
T Value;
vector<TreeNode<T>*> Children;
};
我真的不明白吉爾的算法(上面貼出)。但我想解決方案必須如下:
1. Get a root node, set depth_level = 0
3. set support_node = root_node
4. for each path line
5. determine the quantity of slashes "/", key and file(folder) name
so for example in string root/folder1/file4.txt 5, num of slashes = 2 filename = file4.txt, key = 5
create current_node
6. if num_of_slashes == level + 2
7. set support_node = current_node
8. if num_of_slashes == level + 1
9. add children to support_node
10. And after that we must remember all ancestors, going down to leaves. Cuz we can return to any ancestor.
對我來說這個算法似乎是非常複雜的。我不明白算法,上面發佈,也許有可能澄清這個問題?也許我用來存儲樹的結構不是最好的?
是這樣的:
創造 '\'
node=root;
token=getNextToken(inputString);
while (token){
if (!node.childExist(token)) node.createChild(token)
node=node.Child(token)
token=getNextToken(inputString);
}
是的 - 這絕對是一個很好的解析字符串來構建樹的算法。接下來的問題是「我用什麼來實現樹?」這就是我上面引用的鏈接:http://stackoverflow.com/questions/205945/why-does-the-c-stl-not-provide-any-tree-containers。正如jahhaj所說,沒有人可以隨時隨地使用內置的「最佳方式」。 – paulsm4 2012-08-10 23:35:42
Gir:不允許使用任何第三方庫。代碼中的節點是什麼?我 – 2012-08-11 09:12:29
paulsm4:我看到這個鏈接,但我不能使用增強圖庫來解決這個問題 – 2012-08-11 09:14:11
問:你是什麼意思?另外:看看這裏:http://stackoverflow.com/questions/205945/why-does-the-c-stl-not-provide-any-tree-containers – paulsm4 2012-08-10 22:28:02
我有路徑樹作爲字符串。我需要製作樹數據結構(C++類)並反映適當的層次結構。 – 2012-08-10 22:33:33
什麼樣的樹? – AlexLordThorsen 2012-08-10 22:36:27