@@ -72,12 +72,20 @@ func (n *Node[T]) Backtrack() []*Node[T] {
72
72
// GetStructure retrieves the node structure.
73
73
func (n * Node [T ]) GetStructure () []string {
74
74
var structure []string
75
+ var current string
76
+
75
77
if n .previous == nil {
76
- structure = append ( structure , fmt .Sprintf ("(NULL) -> (%d)" , n .id ) )
78
+ current = fmt .Sprintf ("(NULL) -> (%d)" , n .id )
77
79
} else {
78
- structure = append (structure , fmt .Sprintf ("(%d) -> (%d)" , n .previous .id , n .id ))
80
+ current = fmt .Sprintf ("(%d) -> (%d)" , n .previous .id , n .id )
81
+ }
82
+
83
+ if n .nexts != nil {
84
+ current += ", "
79
85
}
80
86
87
+ structure = append (structure , current )
88
+
81
89
for _ , next := range n .nexts {
82
90
innerStructure := next .GetStructure ()
83
91
structure = append (structure , innerStructure ... )
@@ -91,3 +99,21 @@ func (n *Node[T]) AddNext(node *Node[T]) {
91
99
node .previous = n
92
100
n .nexts = append (n .nexts , node )
93
101
}
102
+
103
+ // Filter remove all sub-nodes that doesn´t respect a rule.
104
+ func (n * Node [T ]) Filter (filterFunc func (obj T ) bool ) (* Node [T ], bool ) {
105
+ if ! filterFunc (n .GetData ()) {
106
+ return nil , false
107
+ }
108
+
109
+ newNode := New (n .GetData ()).WithID (n .GetID ())
110
+
111
+ for _ , next := range n .nexts {
112
+ innerNode , ok := next .Filter (filterFunc )
113
+ if ok {
114
+ newNode .AddNext (innerNode )
115
+ }
116
+ }
117
+
118
+ return newNode , true
119
+ }
0 commit comments