沒有需要遞歸。只是簡單的循環遍歷對象。對於每個對象,如果其parent_id爲0,則將其複製到答案數組中。否則通過它在數組中的位置來訪問父對象,並將當前對象附加到它的子對象列表中。
這是如何解決你的陣列。請仔細注意更新對象一次更新該對象的所有副本這一事實的結果。
0: Start
answer = []
objects = [
{id : 1, parent_id : 0},
{id : 2, parent_id : 1},
{id : 3, parent_id : 1},
{id : 4, parent_id : 2},
{id : 5, parent_id : 4},
{id : 6, parent_id : 0}
]
1: Append object 1 to answer
answer = [
{id : 1, parent_id : 0}
]
objects = [
{id : 1, parent_id : 0},
{id : 2, parent_id : 1},
{id : 3, parent_id : 1},
{id : 4, parent_id : 2},
{id : 5, parent_id : 4},
{id : 6, parent_id : 0}
]
2: Append object 2 to children of object 1
answer = [
{id : 1, parent_id : 0, children : [
{id : 2, parent_id : 1}
]}
]
objects = [
{id : 1, parent_id : 0, children : [
{id : 2, parent_id : 1}
]},
{id : 2, parent_id : 1},
{id : 3, parent_id : 1},
{id : 4, parent_id : 2},
{id : 5, parent_id : 4},
{id : 6, parent_id : 0}
]
3: Append object 3 to children of object 1
answer = [
{id : 1, parent_id : 0, children : [
{id : 2, parent_id : 1},
{id : 3, parent_id : 1}
]}
]
objects = [
{id : 1, parent_id : 0, children : [
{id : 2, parent_id : 1},
{id : 3, parent_id : 1}
]},
{id : 2, parent_id : 1},
{id : 3, parent_id : 1},
{id : 4, parent_id : 2},
{id : 5, parent_id : 4},
{id : 6, parent_id : 0}
]
4: Append object 4 to children of object 2
answer = [
{id : 1, parent_id : 0, children : [
{id : 2, parent_id : 1},
{id : 3, parent_id : 1, children : [
{id : 4, parent_id : 3}
]}
]}
]
objects = [
{id : 1, parent_id : 0, children : [
{id : 2, parent_id : 1},
{id : 3, parent_id : 1, children : [
{id : 4, parent_id : 3}
]}
]},
{id : 2, parent_id : 1},
{id : 3, parent_id : 1, children : [
{id : 4, parent_id : 3}
]},
{id : 4, parent_id : 2},
{id : 5, parent_id : 4},
{id : 6, parent_id : 0}
]
5: Append object 5 to children of object 4
answer = [
{id : 1, parent_id : 0, children : [
{id : 2, parent_id : 1},
{id : 3, parent_id : 1, children : [
{id : 4, parent_id : 3, children : [
{id : 5, parent_id : 4}
]}
]}
]}
]
objects = [
{id : 1, parent_id : 0, children : [
{id : 2, parent_id : 1},
{id : 3, parent_id : 1, children : [
{id : 4, parent_id : 3, children : [
{id : 5, parent_id : 4}
]}
]}
]},
{id : 2, parent_id : 1},
{id : 3, parent_id : 1, children : [
{id : 4, parent_id : 3, children : [
{id : 5, parent_id : 4}
]}
]},
{id : 4, parent_id : 3, children : [
{id : 5, parent_id : 4}
]}
{id : 5, parent_id : 4},
{id : 6, parent_id : 0}
]
6: Append object 6 to answer
answer = [
{id : 1, parent_id : 0, children : [
{id : 2, parent_id : 1},
{id : 3, parent_id : 1, children : [
{id : 4, parent_id : 3, children : [
{id : 5, parent_id : 4}
]}
]}
]},
{id : 6, parent_id : 0}
]
objects = [
{id : 1, parent_id : 0, children : [
{id : 2, parent_id : 1},
{id : 3, parent_id : 1, children : [
{id : 4, parent_id : 3, children : [
{id : 5, parent_id : 4}
]}
]}
]},
{id : 2, parent_id : 1},
{id : 3, parent_id : 1, children : [
{id : 4, parent_id : 3, children : [
{id : 5, parent_id : 4}
]}
]},
{id : 4, parent_id : 3, children : [
{id : 5, parent_id : 4}
]}
{id : 5, parent_id : 4},
{id : 6, parent_id : 0}
]
什麼是「節點」在這一行是指? 「節點[item.parent] .Children.Add(節點);」 – rzetterberg 2011-05-05 18:04:29
@Ander:這是我的錯誤。查看更正的代碼。 – 2011-05-05 20:50:20