当我构建嵌套集时,如何获取非叶节点的右侧值
我正在尝试使用非递归深度优先搜索方法来读取目录并构建嵌套集。我需要为给定目录的路径分配左、右和深度值(同一目录级别的文件不分先后)。 现在我已经完成了叶子节点的深度、左侧和右侧的分配。但我对如何获取非叶节点的右侧值感到困惑。
这是我希望实现的目标,也是我已经完成的:
A:给定一个目录,其中 |== 代表文件夹; |-- 代表文件。 (可以替换成自己的目录)
|==resources
|==data
|==table_design
|==mapper
|==server
|--ServerMapper.xml
|==source
|--AMapper.xml
|--bootstrap.properties
|--logback-spring.xml
B: 预期结果
0 1|||\resources|||20
1 2|||\resources\data||5
2 3|||\resources\data\table_design|||4
1 6|||\resources\mapper|||15
2 7|||\resources\mapper\server|||10
3 8|||\resources\mapper\server\ServerMapper.xml|||9
2 11|||\resources\mapper\source|||12
2 13|||\resources\mapper\AMapper.xml|||14
1 16|||\resources\bootstrap.properties|||17
1 18|||\resources\logback-spring.xml|||19
C: 我的实现
- 实体
static class MyObject {
private int left;
private int right;
private int depth;
private String path;
}
- 我的深度优先搜索逻辑
private static void dfs(File root) {
if (root == null) {
return;
}
int depth = 0;
int index = 1;
Stack<File> stack = new Stack<>();
//Indicates whether the node has been pushed onto the stack
Map<File, MyObject> map = new LinkedHashMap<>();
stack.add(root);
MyObject rootObj = new MyObject();
rootObj.setPath(root.getAbsolutePath());
rootObj.setDepth(depth);
rootObj.setLeft(index++);
map.put(root, rootObj);
while (!stack.isEmpty()) {
File cur = stack.pop();
depth--;
File[] files = cur.listFiles();
if (files != null) {
//Execute as soon as the next node is found
for (File next : files) {
//The next node is pushed if it has not been accessed
if (!map.containsKey(next)) {
depth++;
stack.push(cur);
depth++;
stack.push(next);
MyObject obj = new MyObject();
obj.setLeft(index++);
obj.setPath(next.getAbsolutePath());
obj.setDepth(depth);
if (!next.isDirectory()) {
obj.setRight(index++);
}
map.put(next, obj);
break;
}
}
}
}
System.out.println("depth" + "\t left|||path|||right");
for (MyObject value : map.values()) {
System.out.println(value.getDepth() + "\t" + value.getLeft() + "|||" + value.getPath() + "|||" + value.getRight());
}
}
- 我的输出
0 1|||\resources|||0
1 2|||\resources\bootstrap.properties|||3
1 4|||\resources\data|||0
2 5|||\resources\data\table_design|||6
1 7|||\resources\logback-spring.xml|||8
1 9|||\resources\mapper|||0
2 10|||\resources\mapper\server|||0
3 11|||\resources\mapper\server\UserMapper.xml|||12
2 13|||\resources\mapper\source|||0
2 14|||\resources\mapper\SourceJobMapper.xml|||15
I'm trying to use a non-recursive depth-first-search approach to read directories and build nested sets. I need to assign left, right, and depth values to the path of the given directory(Files at the same directory level are in no particular order).
I have now completed the assignment of depth, left and right to the Leaf node. But I'm confused about how do I get the right-hand value of a non-leaf node.
Here's what I hope to achieve, and what I've already done:
A: Given a directory, where |== stands for folder; |-- stands for file. (You can replace it with your own directory)
|==resources
|==data
|==table_design
|==mapper
|==server
|--ServerMapper.xml
|==source
|--AMapper.xml
|--bootstrap.properties
|--logback-spring.xml
B: Expected result
0 1|||\resources|||20
1 2|||\resources\data||5
2 3|||\resources\data\table_design|||4
1 6|||\resources\mapper|||15
2 7|||\resources\mapper\server|||10
3 8|||\resources\mapper\server\ServerMapper.xml|||9
2 11|||\resources\mapper\source|||12
2 13|||\resources\mapper\AMapper.xml|||14
1 16|||\resources\bootstrap.properties|||17
1 18|||\resources\logback-spring.xml|||19
C: My implementation
- entity
static class MyObject {
private int left;
private int right;
private int depth;
private String path;
}
- My depth-first-search logic
private static void dfs(File root) {
if (root == null) {
return;
}
int depth = 0;
int index = 1;
Stack<File> stack = new Stack<>();
//Indicates whether the node has been pushed onto the stack
Map<File, MyObject> map = new LinkedHashMap<>();
stack.add(root);
MyObject rootObj = new MyObject();
rootObj.setPath(root.getAbsolutePath());
rootObj.setDepth(depth);
rootObj.setLeft(index++);
map.put(root, rootObj);
while (!stack.isEmpty()) {
File cur = stack.pop();
depth--;
File[] files = cur.listFiles();
if (files != null) {
//Execute as soon as the next node is found
for (File next : files) {
//The next node is pushed if it has not been accessed
if (!map.containsKey(next)) {
depth++;
stack.push(cur);
depth++;
stack.push(next);
MyObject obj = new MyObject();
obj.setLeft(index++);
obj.setPath(next.getAbsolutePath());
obj.setDepth(depth);
if (!next.isDirectory()) {
obj.setRight(index++);
}
map.put(next, obj);
break;
}
}
}
}
System.out.println("depth" + "\t left|||path|||right");
for (MyObject value : map.values()) {
System.out.println(value.getDepth() + "\t" + value.getLeft() + "|||" + value.getPath() + "|||" + value.getRight());
}
}
- my output
0 1|||\resources|||0
1 2|||\resources\bootstrap.properties|||3
1 4|||\resources\data|||0
2 5|||\resources\data\table_design|||6
1 7|||\resources\logback-spring.xml|||8
1 9|||\resources\mapper|||0
2 10|||\resources\mapper\server|||0
3 11|||\resources\mapper\server\UserMapper.xml|||12
2 13|||\resources\mapper\source|||0
2 14|||\resources\mapper\SourceJobMapper.xml|||15
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我完成了数据的构建。
I finished the data's build.