Implementing a Tree data structure using swift:
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)
}
}
My goal is to see something like this:
A1
B2
C3
G6
K0
H7
L8
L9
I know there should be some smart way to insert indents, but I also struggle with 'map'. Compiler gives me "ambiguous reference to member 'map'".