Fix recursion algo
continuous-integration/drone/push Build is failing Details

main
Андрей Иванов 2024-09-26 15:33:19 +03:00
parent e46e7c4fcd
commit 10006400b9
2 changed files with 36 additions and 15 deletions

View File

@ -20,16 +20,22 @@ type WeightedNode struct {
} }
func (root *Node) WeightingTreeWithRecursion(parent *Node) map[string]*WeightedNode { func (root *Node) WeightingTreeWithRecursion(parent *Node) map[string]*WeightedNode {
weight := root.getNodeWeight() weightedNode := &Node{
ID: root.ID,
Name: root.Name,
Children: []*Node{},
}
weight := weightedNode.getNodeWeight()
var res = map[string]*WeightedNode{ var res = map[string]*WeightedNode{
root.ID: { root.ID: {
node: root, node: root,
parent: parent, parent: parent,
weight: weight,
}, },
} }
for _, v := range root.Children { for _, v := range root.Children {
maps.Copy(res, v.WeightingTreeWithRecursion(root)) maps.Copy(res, v.WeightingTreeWithRecursion(root))
//weight = weight + res[v.ID].weight weight = weight + res[v.ID].weight
res[root.ID].children = append(res[root.ID].children, v) res[root.ID].children = append(res[root.ID].children, v)
} }
res[root.ID].weight = weight res[root.ID].weight = weight
@ -49,16 +55,18 @@ func (root *Node) WeightingTreeWithStack(parent *Node) map[string]*WeightedNode
nodeWeight := current.getNodeWeight() nodeWeight := current.getNodeWeight()
counter[current.ID].weight = nodeWeight counter[current.ID].weight = nodeWeight
// Прибавляем вес всем родительским нодам до корня // Прибавляем вес всем родительским нодам до корня
parentID := counter[current.ID].parent
for { for {
if parentID == nil { if parent == nil {
break break
} }
currentNode := counter[parent.ID] currentNode := counter[parent.ID]
currentNode.weight = currentNode.weight + nodeWeight currentNode.weight = currentNode.weight + nodeWeight
parentID = currentNode.parent parent = currentNode.parent
}
counter[current.ID].node = current
if len(current.Children) > 0 {
counter[current.ID].children = current.Children
} }
for _, child := range current.Children { for _, child := range current.Children {
counter[child.ID] = &WeightedNode{ counter[child.ID] = &WeightedNode{
parent: current, parent: current,
@ -112,6 +120,11 @@ func (root *Node) WeightingTreeWithDLL(parent *Node) map[string]*WeightedNode {
//} //}
func (n *Node) getNodeWeight() uint16 { func (n *Node) getNodeWeight() uint16 {
nodeBytes, _ := json.Marshal(n) weighted := &Node{
ID: n.ID,
Name: n.Name,
Children: []*Node{},
}
nodeBytes, _ := json.Marshal(weighted)
return uint16(len(nodeBytes)) return uint16(len(nodeBytes))
} }

View File

@ -12,16 +12,24 @@ import (
func TestNode_WeighingTreeAllAlgo(t *testing.T) { func TestNode_WeighingTreeAllAlgo(t *testing.T) {
for name,tt := range map[string]TestTree{ for name,tt := range map[string]TestTree{
"Ordinary VFS tree": getTestTree(3, 3, nil), "Ordinary VFS tree": getTestTree(3, 3, nil),
//"VFS in the form of a pathological tree":getTestTree(1, 4, nil), "VFS in the form of a pathological tree":getTestTree(1, 7, nil),
}{ }{
t.Run(name,func(t *testing.T) { t.Run(name,func(t *testing.T) {
t.Run("WeightingTreeWithRecursion", func(t *testing.T) {
weightTreeWithRecursion := tt.Tree.WeightingTreeWithRecursion(nil) weightTreeWithRecursion := tt.Tree.WeightingTreeWithRecursion(nil)
printExpectations(tt.Weights, weightTreeWithRecursion) printExpectations(tt.Weights, weightTreeWithRecursion)
assert.EqualValues(t, tt.Weights, weightTreeWithRecursion) assert.EqualValues(t, tt.Weights, weightTreeWithRecursion)
//weightTreeWithStack := tt.Tree.WeightingTreeWithStack(nil) })
//assert.EqualValues(t, tt.Weights, weightTreeWithStack) t.Run("WeightingTreeWithStack", func(t *testing.T) {
weightTreeWithStack := tt.Tree.WeightingTreeWithStack(nil)
printExpectations(tt.Weights, weightTreeWithStack)
assert.EqualValues(t, tt.Weights, weightTreeWithStack)
})
//t.Run("WeightingTreeWithDLL", func(t *testing.T) {
// weightTreeWithDLL := tt.Tree.WeightingTreeWithDLL(nil) // weightTreeWithDLL := tt.Tree.WeightingTreeWithDLL(nil)
// printExpectations(tt.Weights, weightTreeWithDLL)
// assert.EqualValues(t, tt.Weights, weightTreeWithDLL) // assert.EqualValues(t, tt.Weights, weightTreeWithDLL)
//})
}) })
} }
} }