0
A
回答
1
數據結構
簡單的關聯數組,將做的工作
array(2) {
["data"]=> int
["children"]=> array()
}
PHP
<?php
function makeTree($data, $children = array()) {
$tree = array();
$tree['data'] = $data;
$tree['children'] = $children;
return $tree;
}
function traverse($tree) {
echo $tree['data'] . "\n";
foreach($tree['children'] as $child) {
traverse($child);
}
}
$root = makeTree(1, array(
makeTree(2, array(makeTree(5), makeTree(6))),
makeTree(3, array(makeTree(7), makeTree(8))),
makeTree(4, array(makeTree(9), makeTree(10), makeTree(11)))
));
traverse($root);
結果
1
2
5
6
3
7
8
4
9
10
11
相關問題
- 1. 遍歷樹的結構和作用
- 2. 遍歷數據結構
- 3. 樹遍歷還是什麼?
- 4. Clojure的數據結構遍歷/搜索
- 5. 樹的樹?樹木表?我創建了什麼樣的數據結構?
- 6. 什麼樣的數據結構可以實現並行處理
- 7. 什麼數據結構用於查找2棵樹的交集
- 8. 顯示在java中執行BFS遍歷的圖的樹結構
- 9. 「學習樹」是什麼樣的數據結構?
- 10. 遍歷JSON樹並操作其數據/結構
- 11. 遞歸查詢鄰接列表以預先遍歷SQL中的樹遍歷?
- 12. 給定預構造遍歷的構造樹
- 13. 遍歷樹遍歷
- 14. 在樹中遍歷
- 15. 這是什麼樣的數據結構?
- 16. 樹的遍歷在Java中
- 17. 什麼樣的數據結構用於複合索引?
- 18. 遍歷/修改Groovy數據結構
- 19. 樹的遍歷
- 20. 返回樹中的遍歷結果
- 21. 如何修改預購樹的遍歷
- 22. 什麼遍歷樹會給我正確的結果
- 23. 遍歷Rails應用程序中的複雜數據結構
- 24. 什麼haskell數據結構來存儲可變的樹
- 25. 遍歷XML結構
- 26. 什麼是遍歷嵌套數組中的樹枝
- 27. 遍歷C#中的任意字典樹結構
- 28. 遍歷數組樹?
- 29. sql表結構 - 通過向後遍歷樹構建數據對象
- 30. 什麼是在Python中遍歷樹的最有效方法?
感謝您的回覆。答案是真的,但是如何將項目存儲在樹中,如果樹層級和子節點是動態的。 – 2014-12-04 06:55:00