所以我一直在学习决策树和东西,我一直在谷歌上搜索一种方法来计算树的终端节点。
让我解释一下
我需要找到一种方法,也许使用矢量或我不知道,我有这棵树:
|-----6-------
4|
|---2-------|------7------
|
|
1 |
| |------11------
| |-----8----10|------12------
|-----3---5 |
|------9-------
这棵树可以是任何大小,我需要找到每个节点的每个值..即
node 4 = 6+7
node 5 = 8+9
node 10 = 11+12
这有什么算法吗?
答案 0 :(得分:1)
Populate a list of nodes with root
While there are nodes left to process
Take next node (call it `N`) to process from list
For each immediade child node (call it `n`) of `N`
Add `n` to end of node list
Add the value associated with `n` to a running total value for `N`
Record total for `N`
Mark `N` as processed
答案 1 :(得分:1)
您尝试做的事情称为tree traversal。如果树太深,应该考虑Breadth-first search。