你好假设你有普通的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.
将此字符串转换为树数据结构的更好方法是什么?
答案 0 :(得分:1)
类似的东西:
为'\'
创建根节点node=root;
token=getNextToken(inputString);
while (token){
if (!node.childExist(token)) node.createChild(token)
node=node.Child(token)
token=getNextToken(inputString);
}
答案 1 :(得分:1)
现在我使用由我自己创建的简单树表示
template<typename T>
class TreeNode
{
public:
TreeNode() {};
TreeNode(T)
{
value = T;
}
TreeNode(const T& value)
: Value(value)
{
}
T Value;
vector<TreeNode<T>*> Children;
};
我真的不明白Gir的算法(上面发布)。但我认为解决方案必须如下:
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.
对我来说,这个算法似乎非常复杂。我不明白上面发布的算法,也许有可能澄清这个问题?也许我用来存储树的结构不是最好的?