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

main
Андрей Иванов 2024-09-26 18:48:27 +03:00
parent 10006400b9
commit 685eeec2aa
2 changed files with 10 additions and 10 deletions

View File

@ -44,7 +44,7 @@ func (root *Node) WeightingTreeWithRecursion(parent *Node) map[string]*WeightedN
func (root *Node) WeightingTreeWithStack(parent *Node) map[string]*WeightedNode { func (root *Node) WeightingTreeWithStack(parent *Node) map[string]*WeightedNode {
weight := root.getNodeWeight() weight := root.getNodeWeight()
counter := map[string]*WeightedNode{root.ID: { res := map[string]*WeightedNode{root.ID: {
weight: weight, weight: weight,
}} }}
stack := []*Node{root} stack := []*Node{root}
@ -53,29 +53,30 @@ func (root *Node) WeightingTreeWithStack(parent *Node) map[string]*WeightedNode
stack = stack[:len(stack)-1] stack = stack[:len(stack)-1]
nodeWeight := current.getNodeWeight() nodeWeight := current.getNodeWeight()
counter[current.ID].weight = nodeWeight res[current.ID].weight = nodeWeight
// Прибавляем вес всем родительским нодам до корня // Прибавляем вес всем родительским нодам до корня
parentNode := res[current.ID].parent
for { for {
if parent == nil { if parentNode == nil {
break break
} }
currentNode := counter[parent.ID] currentNode := res[parentNode.ID]
currentNode.weight = currentNode.weight + nodeWeight currentNode.weight = currentNode.weight + nodeWeight
parent = currentNode.parent parentNode = currentNode.parent
} }
counter[current.ID].node = current res[current.ID].node = current
if len(current.Children) > 0 { if len(current.Children) > 0 {
counter[current.ID].children = current.Children res[current.ID].children = current.Children
} }
for _, child := range current.Children { for _, child := range current.Children {
counter[child.ID] = &WeightedNode{ res[child.ID] = &WeightedNode{
parent: current, parent: current,
} }
stack = append(stack, child) stack = append(stack, child)
} }
} }
return counter return res
} }
func (root *Node) WeightingTreeWithDLL(parent *Node) map[string]*WeightedNode { func (root *Node) WeightingTreeWithDLL(parent *Node) map[string]*WeightedNode {

View File

@ -8,7 +8,6 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
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),