diff --git a/weghting_the_tree/funcs.go b/weghting_the_tree/funcs.go index cc36121..db618b5 100644 --- a/weghting_the_tree/funcs.go +++ b/weghting_the_tree/funcs.go @@ -42,7 +42,7 @@ func (root *Node) WeightingTreeWithRecursion(parent *Node) map[string]*WeightedN return res } -func (root *Node) WeightingTreeWithStack(parent *Node) map[string]*WeightedNode { +func (root *Node) WeightingTreeWithStack() map[string]*WeightedNode { weight := root.getNodeWeight() res := map[string]*WeightedNode{root.ID: { weight: weight, @@ -79,7 +79,7 @@ func (root *Node) WeightingTreeWithStack(parent *Node) map[string]*WeightedNode return res } -func (root *Node) WeightingTreeWithDLL(parent *Node) map[string]*WeightedNode { +func (root *Node) WeightingTreeWithDLL() map[string]*WeightedNode { weight := root.getNodeWeight() counter := map[string]*WeightedNode{root.ID: { weight: weight, @@ -123,12 +123,11 @@ func (root *Node) WeightingTreeWithDLL(parent *Node) map[string]*WeightedNode { // return nil, nil //} -func (n *Node) getNodeWeight() uint16 { - weighted := &Node{ - ID: n.ID, - Name: n.Name, +func (root *Node) getNodeWeight() uint16 { + nodeBytes, _ := json.Marshal(&Node{ + ID: root.ID, + Name: root.Name, Children: []*Node{}, - } - nodeBytes, _ := json.Marshal(weighted) + }) return uint16(len(nodeBytes)) } diff --git a/weghting_the_tree/funcs_test.go b/weghting_the_tree/funcs_test.go index 9c3184e..823ba06 100644 --- a/weghting_the_tree/funcs_test.go +++ b/weghting_the_tree/funcs_test.go @@ -20,12 +20,12 @@ func TestNode_WeighingTreeAllAlgo(t *testing.T) { assert.EqualValues(t, tt.Weights, weightTreeWithRecursion) }) t.Run("WeightingTreeWithStack", func(t *testing.T) { - weightTreeWithStack := tt.Tree.WeightingTreeWithStack(nil) + weightTreeWithStack := tt.Tree.WeightingTreeWithStack() printExpectations(tt.Weights, weightTreeWithStack) assert.EqualValues(t, tt.Weights, weightTreeWithStack) }) t.Run("WeightingTreeWithDLL", func(t *testing.T) { - weightTreeWithDLL := tt.Tree.WeightingTreeWithDLL(nil) + weightTreeWithDLL := tt.Tree.WeightingTreeWithDLL() printExpectations(tt.Weights, weightTreeWithDLL) assert.EqualValues(t, tt.Weights, weightTreeWithDLL) }) @@ -41,9 +41,9 @@ func Benchmark(b *testing.B) { depth int }{ "Small tree (85 nodes)":{4, 4}, // 85 nodes, - "Wide tree (265K nodes)": {515, 3}, // 265'741 nodes, - "Deep tree (265K nodes)": {3, 12}, // 265'720 nodes, - "Huge tree (2,44M nodes)": {5, 10}, // 2'441'406 nodes, + //"Wide tree (265K nodes)": {515, 3}, // 265'741 nodes, + //"Deep tree (265K nodes)": {3, 12}, // 265'720 nodes, + //"Huge tree (2,44M nodes)": {5, 10}, // 2'441'406 nodes, "Pathological tree (10K nodes)": {1, 10000}, // 10'000 nodes, }{ 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) { for i := 0; i < b.N; i++ { b.StartTimer() - assert.EqualValues(b, tree.Weights, tree.Tree.WeightingTreeWithStack(nil)) + assert.EqualValues(b, tree.Weights, tree.Tree.WeightingTreeWithStack()) b.StopTimer() } }) b.Run("Weighting with DLL", func(b *testing.B) { for i := 0; i < b.N; i++ { b.StartTimer() - assert.EqualValues(b, tree.Weights, tree.Tree.WeightingTreeWithDLL(nil)) + assert.EqualValues(b, tree.Weights, tree.Tree.WeightingTreeWithDLL()) b.StopTimer() } })