Immutable double-linked tree construction in Scala