Fix recursion algo
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
parent
e46e7c4fcd
commit
10006400b9
|
@ -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))
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
||||||
weightTreeWithRecursion := tt.Tree.WeightingTreeWithRecursion(nil)
|
t.Run("WeightingTreeWithRecursion", func(t *testing.T) {
|
||||||
printExpectations(tt.Weights, weightTreeWithRecursion)
|
weightTreeWithRecursion := tt.Tree.WeightingTreeWithRecursion(nil)
|
||||||
assert.EqualValues(t, tt.Weights, weightTreeWithRecursion)
|
printExpectations(tt.Weights, weightTreeWithRecursion)
|
||||||
//weightTreeWithStack := tt.Tree.WeightingTreeWithStack(nil)
|
assert.EqualValues(t, tt.Weights, weightTreeWithRecursion)
|
||||||
//assert.EqualValues(t, tt.Weights, weightTreeWithStack)
|
})
|
||||||
//weightTreeWithDLL := tt.Tree.WeightingTreeWithDLL(nil)
|
t.Run("WeightingTreeWithStack", func(t *testing.T) {
|
||||||
//assert.EqualValues(t, tt.Weights, weightTreeWithDLL)
|
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)
|
||||||
|
// printExpectations(tt.Weights, weightTreeWithDLL)
|
||||||
|
// assert.EqualValues(t, tt.Weights, weightTreeWithDLL)
|
||||||
|
//})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue