35 lines
849 B
Go
35 lines
849 B
Go
package cut_the_tree
|
||
|
||
import (
|
||
"encoding/json"
|
||
"maps"
|
||
)
|
||
|
||
func (root Node) WeighingTreeWithRecursion() map[string]uint16 {
|
||
nodeBytes, _ := json.Marshal(Node{
|
||
ID: root.ID,
|
||
Name: root.Name,
|
||
})
|
||
weight := uint16(len(nodeBytes))
|
||
var res = make(map[string]uint16)
|
||
for _, v := range root.Children {
|
||
maps.Copy(res, v.WeighingTreeWithRecursion()) // Т.е. в go нет оптимизации хвостовой рекурсии, это породит неимоверное кол-во аллокаций.
|
||
weight = weight + res[v.ID]
|
||
}
|
||
res[root.ID] = weight
|
||
return res
|
||
}
|
||
|
||
func (root Node) WeighingTreeIdiomaticDFS() map[string]uint16 {
|
||
return nil
|
||
}
|
||
|
||
func (root Node) WeighingTreeIdiomaticBFS() map[string]uint16 {
|
||
return nil
|
||
}
|
||
|
||
func (root Node) DecomposeTree(weights map[string]uint16, limit int) ([]*Node, error) {
|
||
|
||
return nil, nil
|
||
}
|