diff --git a/weighting_n_cutting_the_tree/cutting.go b/weighting_n_cutting_the_tree/cutting.go index 07adb53..37bf0d7 100644 --- a/weighting_n_cutting_the_tree/cutting.go +++ b/weighting_n_cutting_the_tree/cutting.go @@ -15,7 +15,7 @@ func DecomposeTree(tree map[string]*WeightedNode, threshold uint16) ([]SubTree, // Ищем ветку подходящую под лимит branchID, weight := GetBranch(tree, index, threshold) - // Опаределяем путь к найденной ветке + // Определяем путь к найденной ветке path, err := GetNodePath(tree, branchID) if err != nil { return nil, errors.Wrap(err, "[GetNodePath] ") @@ -110,17 +110,17 @@ func CutBranch(tree map[string]*WeightedNode, nodeID string) *Node { // Удаление ссылки на ноду из родительской ноды if tree[nodeID].parent != nil { parentID := tree[nodeID].parent.ID - for k, v := range tree[parentID].children { - if v.ID == nodeID { - tree[parentID].children = append(tree[parentID].children[:k], tree[parentID].children[k+1:]...) - break - } + k := slices.IndexFunc(tree[parentID].children, func(node *Node) bool { + return node.ID == nodeID + }) + if k > 0 { + tree[parentID].children = append(tree[parentID].children[:k], tree[parentID].children[k+1:]...) } - for k, v := range tree[parentID].node.Children { - if v.ID == nodeID { - tree[parentID].node.Children = append(tree[parentID].node.Children[:k], tree[parentID].node.Children[k+1:]...) - break - } + m := slices.IndexFunc(tree[parentID].node.Children, func(node *Node) bool { + return node.ID == nodeID + }) + if m > 0 { + tree[parentID].children = append(tree[parentID].node.Children[:m], tree[parentID].node.Children[m+1:]...) } } diff --git a/weighting_n_cutting_the_tree/cutting_test.go b/weighting_n_cutting_the_tree/cutting_test.go index 5eb2d70..f2e95a6 100644 --- a/weighting_n_cutting_the_tree/cutting_test.go +++ b/weighting_n_cutting_the_tree/cutting_test.go @@ -1,8 +1,7 @@ package main import ( - "fmt" -"testing" + "testing" "github.com/stretchr/testify/require" ) @@ -23,7 +22,6 @@ func TestDecomposeTree(t *testing.T){ }{ t.Run(name, func(t *testing.T) { result, err := DecomposeTree(tt.inputTree, tt.inputThreshold) - fmt.Printf("%+v\nn",result ) if tt.err == "" { require.NoError(t, err) require.ElementsMatch(t, result, tt.outputParts) diff --git a/weighting_n_cutting_the_tree/cutting_test_models.go b/weighting_n_cutting_the_tree/cutting_test_models.go index 7b7c7a1..5c31b67 100644 --- a/weighting_n_cutting_the_tree/cutting_test_models.go +++ b/weighting_n_cutting_the_tree/cutting_test_models.go @@ -63,12 +63,12 @@ func getSmallTree() testDecomposingData { Children: []*Node{ { ID: "1.1.3.1", - Name: "folder3", + Name: "folder1", Children: []*Node{}, }, { ID: "1.1.3.2", - Name: "folder3", + Name: "folder2", Children: []*Node{}, }, { @@ -280,12 +280,12 @@ func getSmallTree() testDecomposingData { Children: []*Node{ { ID: "1.1.3.1", - Name: "folder3", + Name: "folder1", Children: []*Node{}, }, { ID: "1.1.3.2", - Name: "folder3", + Name: "folder2", Children: []*Node{}, }, { @@ -307,11 +307,13 @@ func getSmallTree() testDecomposingData { Name: "folder1", Children: []*Node{}, }, - {ID: "1.2.1.2", + { + ID: "1.2.1.2", Name: "folder2", Children: []*Node{}, }, - {ID: "1.2.1.3", + { + ID: "1.2.1.3", Name: "folder3", Children: []*Node{}, }, @@ -377,11 +379,13 @@ func getSmallTree() testDecomposingData { Name: "folder1", Children: []*Node{}, }, - {ID: "1.3.1.2", + { + ID: "1.3.1.2", Name: "folder2", Children: []*Node{}, }, - {ID: "1.3.1.3", + { + ID: "1.3.1.3", Name: "folder3", Children: []*Node{}, }, @@ -423,11 +427,13 @@ func getSmallTree() testDecomposingData { Name: "folder1", Children: []*Node{}, }, - {ID: "1.3.3.2", + { + ID: "1.3.3.2", Name: "folder2", Children: []*Node{}, }, - {ID: "1.3.3.3", + { + ID: "1.3.3.3", Name: "folder3", Children: []*Node{}, },