2分木
2分木らしきものを作った。あっているかはさておき。
import Foundation class N { static var count:Int = 0 var identifier:Int = 0 var v:Int = 0 var small:N? = nil var big:N? = nil func add(_ n:N) { if self.v > n.v { if let small = self.small { small.add(n) } else { self.small = n } } else { if let big = self.big { big.add(n) } else { self.big = n } } } init(_ v:Int) { self.identifier = N.count N.count = N.count + 1 self.v = v } func selfdesc() -> String { return "\(self.identifier):\(self.v)" } func desc() -> String { let small = self.small?.desc() ?? "" let big = self.big?.desc() ?? "" return ("(\(self.v) \(small) \(big))") } func idesc() -> String { let small = self.small?.idesc() ?? "" let big = self.big?.idesc() ?? "" return ("(\(self.selfdesc()) \(small) \(big))") } }