我在JavaScript擡頭這一基本格式樹結構的自定義樹數據結構:我需要使用JavaScript創建
function Tree(parent, child, data) {
this.parent = parent;
this.children = child || [];
this.data = data;
this.addNode ...
this.addChild ...
}
我正在一棵樹就是「長」這個問題。我使用的數據是在步道的街道名單,這幾乎是一條直線路徑,但也有幾個小劈叉的蹤跡,該數據會看起來像:
A ->
B ->
C ->
D -> E,F
E ->
G ->
H
F -> I
I -> J
J -> K,L
K ->
M ->
N
L -> O
O -> P
我會想避免代碼看起來像:
tree.children[0].children[0].children[0].addNode("E");
tree.children[0].children[0].children[0].push("F");
所以我的問題之一是如何遍歷樹,簡單地說?
node = tree;
while(node.children != null)
node = node.children[0];
,如果你能幫助我,我會很感激,感謝,
mathacka
你試過你的代碼? – Mathletics