我有一个像这样的复杂数组(树):
[0] code = "pc1"
children
[0] code = "gc1"
children
[0] code = "cc1"
children = nil
[1] code = "cc2"
children = nil
[1] code = "gc2"
children = nil
[1] code = "pc2"
children = nil
我想要的是搜索没有孩子的任何节点。如果找到任何儿童笔记,我想保持层次结构。例如,搜索“2”将返回:
[0] code = "pc1"
children
[0] code = "gc1"
children
[0] code = "cc2"
children = nil
[1] code = "gc2"
children = nil
[1] code = "pc2"
children = nil
搜索“p”将返回:
[0] code = "pc1"
children
[1] code = "pc2"
children = nil
我试了好久。但未能找到解决方案。任何的想法?感谢。
于2017年8月27日编辑:
这是我的树类:
class Node {
var parentCode: String = ""
weak var parent: Node?
lazy var children = [Node]()
init() {}
func add(node: Node) {
children.append(code)
code.parent = self
}
如果我想搜索 o 字母,那将是结果。