Андрей Иванов 2024-09-27 11:36:35 +03:00
parent 8829d0bec7
commit ecea2852f6
2 changed files with 14 additions and 15 deletions
weghting_the_tree

View File

@ -42,7 +42,7 @@ func (root *Node) WeightingTreeWithRecursion(parent *Node) map[string]*WeightedN
return res return res
} }
func (root *Node) WeightingTreeWithStack(parent *Node) map[string]*WeightedNode { func (root *Node) WeightingTreeWithStack() map[string]*WeightedNode {
weight := root.getNodeWeight() weight := root.getNodeWeight()
res := map[string]*WeightedNode{root.ID: { res := map[string]*WeightedNode{root.ID: {
weight: weight, weight: weight,
@ -79,7 +79,7 @@ func (root *Node) WeightingTreeWithStack(parent *Node) map[string]*WeightedNode
return res return res
} }
func (root *Node) WeightingTreeWithDLL(parent *Node) map[string]*WeightedNode { func (root *Node) WeightingTreeWithDLL() map[string]*WeightedNode {
weight := root.getNodeWeight() weight := root.getNodeWeight()
counter := map[string]*WeightedNode{root.ID: { counter := map[string]*WeightedNode{root.ID: {
weight: weight, weight: weight,
@ -123,12 +123,11 @@ func (root *Node) WeightingTreeWithDLL(parent *Node) map[string]*WeightedNode {
// return nil, nil // return nil, nil
//} //}
func (n *Node) getNodeWeight() uint16 { func (root *Node) getNodeWeight() uint16 {
weighted := &Node{ nodeBytes, _ := json.Marshal(&Node{
ID: n.ID, ID: root.ID,
Name: n.Name, Name: root.Name,
Children: []*Node{}, Children: []*Node{},
} })
nodeBytes, _ := json.Marshal(weighted)
return uint16(len(nodeBytes)) return uint16(len(nodeBytes))
} }

View File

@ -20,12 +20,12 @@ func TestNode_WeighingTreeAllAlgo(t *testing.T) {
assert.EqualValues(t, tt.Weights, weightTreeWithRecursion) assert.EqualValues(t, tt.Weights, weightTreeWithRecursion)
}) })
t.Run("WeightingTreeWithStack", func(t *testing.T) { t.Run("WeightingTreeWithStack", func(t *testing.T) {
weightTreeWithStack := tt.Tree.WeightingTreeWithStack(nil) weightTreeWithStack := tt.Tree.WeightingTreeWithStack()
printExpectations(tt.Weights, weightTreeWithStack) printExpectations(tt.Weights, weightTreeWithStack)
assert.EqualValues(t, tt.Weights, weightTreeWithStack) assert.EqualValues(t, tt.Weights, weightTreeWithStack)
}) })
t.Run("WeightingTreeWithDLL", func(t *testing.T) { t.Run("WeightingTreeWithDLL", func(t *testing.T) {
weightTreeWithDLL := tt.Tree.WeightingTreeWithDLL(nil) weightTreeWithDLL := tt.Tree.WeightingTreeWithDLL()
printExpectations(tt.Weights, weightTreeWithDLL) printExpectations(tt.Weights, weightTreeWithDLL)
assert.EqualValues(t, tt.Weights, weightTreeWithDLL) assert.EqualValues(t, tt.Weights, weightTreeWithDLL)
}) })
@ -41,9 +41,9 @@ func Benchmark(b *testing.B) {
depth int depth int
}{ }{
"Small tree (85 nodes)":{4, 4}, // 85 nodes, "Small tree (85 nodes)":{4, 4}, // 85 nodes,
"Wide tree (265K nodes)": {515, 3}, // 265'741 nodes, //"Wide tree (265K nodes)": {515, 3}, // 265'741 nodes,
"Deep tree (265K nodes)": {3, 12}, // 265'720 nodes, //"Deep tree (265K nodes)": {3, 12}, // 265'720 nodes,
"Huge tree (2,44M nodes)": {5, 10}, // 2'441'406 nodes, //"Huge tree (2,44M nodes)": {5, 10}, // 2'441'406 nodes,
"Pathological tree (10K nodes)": {1, 10000}, // 10'000 nodes, "Pathological tree (10K nodes)": {1, 10000}, // 10'000 nodes,
}{ }{
b.Run(name, func(b *testing.B) { b.Run(name, func(b *testing.B) {
@ -61,14 +61,14 @@ func Benchmark(b *testing.B) {
b.Run("Weighting with stack", func(b *testing.B) { b.Run("Weighting with stack", func(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
b.StartTimer() b.StartTimer()
assert.EqualValues(b, tree.Weights, tree.Tree.WeightingTreeWithStack(nil)) assert.EqualValues(b, tree.Weights, tree.Tree.WeightingTreeWithStack())
b.StopTimer() b.StopTimer()
} }
}) })
b.Run("Weighting with DLL", func(b *testing.B) { b.Run("Weighting with DLL", func(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
b.StartTimer() b.StartTimer()
assert.EqualValues(b, tree.Weights, tree.Tree.WeightingTreeWithDLL(nil)) assert.EqualValues(b, tree.Weights, tree.Tree.WeightingTreeWithDLL())
b.StopTimer() b.StopTimer()
} }
}) })