Skip to content

Commit b5d8a46

Browse files
zonewaveshoenig
authored andcommitted
fix(treeset): infix can't stop correctly
1 parent 7741a0f commit b5d8a46

2 files changed

Lines changed: 13 additions & 11 deletions

File tree

treeset.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,10 @@ func (s *TreeSet[T, C]) Empty() bool {
342342
// Slice returns the elements of s as a slice, in order.
343343
func (s *TreeSet[T, C]) Slice() []T {
344344
result := make([]T, 0, s.Size())
345-
s.infix(func(n *node[T]) bool {
346-
result = append(result, n.element)
345+
s.ForEach(func(element T) bool {
346+
result = append(result, element)
347347
return true
348-
}, s.root)
348+
})
349349
return result
350350
}
351351

@@ -496,10 +496,10 @@ func (s *TreeSet[T, C]) String() string {
496496
// element into a string. The result contains elements in order.
497497
func (s *TreeSet[T, C]) StringFunc(f func(element T) string) string {
498498
l := make([]string, 0, s.Size())
499-
s.infix(func(n *node[T]) bool {
500-
l = append(l, f(n.element))
499+
s.ForEach(func(element T) bool {
500+
l = append(l, f(element))
501501
return true
502-
}, s.root)
502+
})
503503
return fmt.Sprintf("%s", l)
504504
}
505505

@@ -912,15 +912,17 @@ func (s *TreeSet[T, C]) compare(a, b *node[T]) int {
912912
// TreeNodeVisit is a function that is called for each node in the tree.
913913
type TreeNodeVisit[T any] func(*node[T]) (next bool)
914914

915-
func (s *TreeSet[T, C]) infix(visit TreeNodeVisit[T], n *node[T]) {
915+
func (s *TreeSet[T, C]) infix(visit TreeNodeVisit[T], n *node[T]) (next bool) {
916916
if n == nil {
917+
return true
918+
}
919+
if next = s.infix(visit, n.left); !next {
917920
return
918921
}
919-
s.infix(visit, n.left)
920-
if !visit(n) {
922+
if next = visit(n); !next {
921923
return
922924
}
923-
s.infix(visit, n.right)
925+
return s.infix(visit, n.right)
924926
}
925927

926928
func (s *TreeSet[T, C]) fillLeft(n *node[T], k *[]T) {

treeset_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ func TestTreeSet_infix(t *testing.T) {
909909
}
910910
odds := make([]int, 0, 5)
911911
ts.infix(func(n *node[int]) bool {
912-
if n.element > 8 {
912+
if n.element == 8 {
913913
return false
914914
}
915915
if isOdd(n) {

0 commit comments

Comments
 (0)