parent
10006400b9
commit
685eeec2aa
weghting_the_tree
|
@ -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 {
|
||||
|
|
|
@ -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),
|
||||
|
|
Loading…
Reference in New Issue