【发布时间】:2018-02-10 21:42:51
【问题描述】:
我需要在不改变其 O(n) 复杂度的情况下为 Trie 数据结构创建四个自定义函数:
- makeDir("pathToDir")
- make("pathToFile")
- delete("pathToDir")
- forceDelete("pathToDirOrFile")
makeDir("pathToDir"):仅当路径是有效路径时才将路径添加到特里树
make("pathToFile"): 仅当路径是有效路径时才添加路径(文件节点不能调用 makeDir())
delete("pathToDir"):仅当路径没有子目录时才从 trie 中删除路径
forceDelete("pathToDirOrFile"):删除路径及其子目录
例如,命令列表是:
makeDir("\dir");
makeDir("\dir\foo")
makeDir("\dir\foo\lol\ok"); /* incorrect path */
make("\dir\file");
makeDir("\dir\file\abc"); /* file can't have sub dirs */
delete("\dir"); /* has childs, incorrect */
delete("\dir\file");
forceDelete("\dir");
有人知道如何识别节点指示文件的路径吗?实现这些功能的最佳方式是什么?
【问题讨论】:
-
“有效路径”究竟是什么意思?
-
您可以在示例中理解它,正如您在第三行看到的那样,lol 文件夹不存在,因此这是一个无效路径。
标签: algorithm time-complexity trie