亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Go 中遍歷孩子的譜系

在 Go 中遍歷孩子的譜系

Go
HUH函數 2022-05-18 15:42:41
我有一個龐大的人口譜系 ( map[int][]int),其中關鍵是動物,而價值切片中的父母 (兩個)。沒有父母的動物將有父母的負整數。其次,我有一個動物列表,我想建立它們的特定譜系并寫入文件。我列表譜系中的所有動物都應該寫入同一個文件。pedigree := map[int][]int{    1:  []int{2, 3},    2:  []int{-1, 5},    3:  []int{6, 7},    4:  []int{8, 9},    5:  []int{-1, -2},    6:  []int{8, -2},    7:  []int{-1, -2},    8:  []int{-1, -2},    9:  []int{10, -2},    10: []int{-1, 4}}list := []int{1, 4}以及我對 io.Writer 編寫的文件的期望:  1   2   3  2  -1   5  3   6   7  5  -1  -2  6   8  -2  7  -1  -2  8  -1  -2  4   8   9  9  10  -2 10  -1   4所以我需要一個遞歸函數來遍歷從基礎動物開始的譜系,然后在所有路徑上繼續,直到達到負數的父母。更重要的是,我列表中的動物被確定為導致譜系周期的動物(動物 4 成為其自身的偉大父母)。我已經使用以下代碼進行了嘗試:type tree struct {    sire   *tree    dam    *tree    animal int    base   int    path   []int}func newTree(a, s, d, b int) tree {    return tree{        animal: a,        sire:   &tree{animal: s, base: b},        dam:    &tree{animal: d, base: b},        base:   b,    }}for _, animal := range list {    myTree = newTree(animal, pedigree[animal][0], pedigree[animal][1], 0)    walk(myTree, written, fw) // written is a map of integers and fw is io.Writer}func walk(t tree, written map[int]int, writer io.Writer) tree {    style := "%12d%12d%12d\n"    if t.animal == t.base {        return t    }    if t.base == 0 {  // for the first iteration        t.base = t.animal    }    if _, ok := written[t.animal]; !ok {        sire := t.sire.animal        dam := t.dam.animal        if sire == 0 {            sire = t.base        }        if dam == 0 {            dam = t.base        }        written[t.animal] = 0        io.WriteString(writer, fmt.Sprintf(style, t.animal, sire, dam))    }    // Shift forward.    if t.sire.animal > 0 {        myTree := newTree(t.sire.animal, pedigree[t.sire.animal][0], pedigree[t.dam.animal][1], t.base)        walk(myTree, written, writer)    }我的譜系由大約 1300 萬只動物組成,但我無法讓功能在正確的點停止并在stack overflow完成幾只動物后感到恐慌。我相信我的一些問題是:參與循環的動物是基礎動物,所以我應該檢查是否animal == base (1->2->3->1)參與循環的動物不是基礎動物(1->2->3->4->5->6->3)任何幫助將不勝感激。
查看完整描述

1 回答

?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

由于您的系譜圖中可以有循環,因此您可能會陷入這樣的循環。這意味著您必須檢測循環并停止在那里構建。你可以通過稍微改變你的樹來做到這一點。


首先,您tree按值傳遞對象,但附加指向它的指針。將指針傳遞給周圍的樹:


func walk(t *tree, written map[int]int, writer io.Writer) *tree {

}

更好的方法可能是:


func (t *tree) walk(written map[int]int, writer io.Writer) {...}

您還應該將 a 添加*parent到樹中,以便檢測循環:


type tree struct {

    parent *tree

    sire   *tree

    dam    *tree

    animal int

    base   int

    path   []int

}

每次創建新關卡時,適當設置父級:


myTree := newTree(t.sire.animal, pedigree[t.sire.animal][0], pedigree[t.dam.animal][1], t.base)

myTree.parent=t

myTree.walk(written, writer)

然后添加一個函數來測試你是否進入循環:


func (t *tree) loop(animal int) bool {

   for t!=nil {

       if t.animal==animal {

          return true

       }

       t=t.parent

   }

   return false

}

并檢查您是否正在進入循環:


if !myTree.loop(myTree.animal) {

   myTree.walk(written, writer)

}


查看完整回答
反對 回復 2022-05-18
  • 1 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號