Tree Swift语言中的树

Tree Swift语言中的树,tree,swift,Tree,Swift,我试着用手指的方式用雨燕做一棵树 这是我的密码: //this code works struct testArbre { var racine: Int? } let a0 = testArbre(racine:nil) //this code stuck on error struct arbre { var racine: Int? var feuilleGauche: arbre? var feuilleDroite : arbre

我试着用手指的方式用雨燕做一棵树

这是我的密码:

//this code works
struct testArbre {
    var racine: Int?
}

let a0 = testArbre(racine:nil)



//this code stuck on error
struct arbre {
    var racine: Int?
        var feuilleGauche: arbre?
        var feuilleDroite : arbre?
}

let a1 = arbre(racine: 10, feuilleGauche: nil, feuilleDroite: nil)

let a2 = arbre(racine:5, feuilleGauche:nil, feuilleDroite:nil)

let a3 = arbre(racine:1, feuilleGauche: a1, feuilleDroite: a2);
下面是运行此代码时出现的错误:

<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: swift frontend command failed due to signal (use -v to see invocation)
Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254
:0:错误:无法执行命令:分段错误:11
:0:错误:swift前端命令因信号而失败(使用-v查看调用)
命令/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/xcodefault.xctoolchain/usr/bin/swift失败,退出代码254

结构是值类型。想象一下结构是如何初始化的:您有一个struct arbre,因此系统将为其成员分配内存,这些成员是Int和struct arbre,因此系统将为其成员分配内存(Int和struct arbre),因此系统将为其成员分配内存(Int和struct arbre)…->撞车

你需要让它成为一门课

如果您想要一个结构,其中包含对同一类型的引用,这在C中很容易用指针完成