Fix stack algo

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

View File

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

View File

@ -8,7 +8,6 @@ import (
"github.com/stretchr/testify/assert"
)
func TestNode_WeighingTreeAllAlgo(t *testing.T) {
for name,tt := range map[string]TestTree{
"Ordinary VFS tree": getTestTree(3, 3, nil),