tests/cutTheTree/main.go

27 lines
691 B
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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) DecomposeTree(weights map[string]uint16, limit int) ([]*Node, error) {
return nil, nil
}