打印带缩进的树。迅速

时间:2017-09-22 18:56:29

标签: swift data-structures tree

使用swift实现Tree数据结构:

class Node {

    var value: String
    var children: [Node] = []
    weak var parent: Node?

    init(_ value: String) {
        self.value = value
    }

    func add(_ child: Node){
        children.append(child)
        child.parent = self
    }

    func printTree() {
        var text = self.value
        if !self.children.isEmpty {
            text += "\n  " + self.children.map{$0.printTree()}.joined(separator: ", ")
        }
        print(text)
    }

}

我的目标是看到这样的事情:

A1
    B2
    C3
        G6
    K0
        H7
            L8
        L9

我知道应该有一些聪明的方法来插入缩进,但我也很难用'map'。编译器给我“模糊的参考成员'地图'”。

3 个答案:

答案 0 :(得分:3)

我认为vacawama的答案非常智能,但是如果你想练习包括map的递归调用,你可以这样写:

class Node {
    //...

    func treeLines() -> [String] {
        return [self.value] + self.children.flatMap{$0.treeLines()}.map{"    "+$0}
    }
    func printTree() {
        let text = treeLines().joined(separator: "\n")
        print(text)
    }
}

测试:

let a1 = Node("A1")
let b2 = Node("B2")
let c3 = Node("C3")
let g6 = Node("G6")
let k0 = Node("K0")
let h7 = Node("H7")
let l8 = Node("L8")
let l9 = Node("L9")
a1.add(b2)
a1.add(c3)
a1.add(k0)
c3.add(g6)
k0.add(h7)
k0.add(l9)
h7.add(l8)
a1.printTree()

输出:

A1
    B2
    C3
        G6
    K0
        H7
            L8
        L9

有些观点:

  • 您的printTree()是一个void函数,因此在map中使用它会生成一个空元组数组,不能是joined

    (在Swift中,void函数可以被认为是返回空元组。当无法找到适当的重载时,Swift会生成对成员的模糊引用。)

    您可能需要一个中间函数,其结果可以表示部分树结构。

  • 需要在每一行添加缩进,因此中间结果应该很容易分成几行。

答案 1 :(得分:2)

这样的事情应该有效:

func printTree(_ indent: String = "") {

    print(indent + self.value)

    for child in children {
        child.printTree(indent + "    ")
    }
}

答案 2 :(得分:2)

如果你想要漂亮,你可以这样做:

extension Node
{
   func treeLines(_ nodeIndent:String="", _ childIndent:String="") -> [String]
   {
      return [ nodeIndent + value ]
           + children.enumerated().map{ ($0 < children.count-1, $1) }
             .flatMap{ $0 ? $1.treeLines("┣╸","┃ ") : $1.treeLines("┗╸","  ") }
             .map{ childIndent + $0 } 
   }

   func printTree()
   { print(treeLines().joined(separator:"\n")) }
}

a1.printTree()

// A1
// ┣╸B2
// ┣╸C3
// ┃ ┗╸G6
// ┗╸K0
//   ┣╸H7
//   ┃ ┗╸L8
//   ┗╸L9

您还可以将其概括为任何树结构的打印功能,以便您为每个节点选择要打印的内容:

func printTree<T>(_ node:T, _ nodeInfo:@escaping (T)->(String,[T]) ) 
{
  func lines(_ aNode:T, _ nodeIndent:String="", _ childIndent:String="") -> [String]
  {
    let (label,children) = nodeInfo(aNode)
    return [ nodeIndent + label]
         + children.enumerated().map{ ($0 < children.count-1, $1) }
           .flatMap{ $0 ? lines($1,"┣╸","┃ ") :lines($1,"┗╸","  ") }
           .map{ childIndent + $0 }
  }
  print( lines(node).joined(separator:"\n") )
}

// print a root node providing a capture to obtain the node's label
// and its array of children

printTree(a1){ ($0.value,$0.children) }

// works for any tree structure.  for example, views :

printTree(view){( "\(type(of:$0)) \($0.frame)", $0.subviews )}