Skip to content

Commit bcbd005

Browse files
committed
update
1 parent 4e15b0a commit bcbd005

File tree

3 files changed

+130
-38
lines changed

3 files changed

+130
-38
lines changed

internal/tree/red_black_tree.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ func (rb *RBTree[K, V]) inOrderTraversal(visit func(node *rbNode[K, V]) bool) {
144144
}
145145
curr = stack[len(stack)-1]
146146
stack = stack[:len(stack)-1]
147-
visit(curr)
147+
if !visit(curr) {
148+
break
149+
}
148150
curr = curr.right
149151
}
150152
}

internal/tree/red_black_tree_test.go

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,26 +1397,70 @@ func TestRBTree_KeyValues(t *testing.T) {
13971397
}
13981398
}
13991399

1400-
// 测试遍历
1401-
// 插入10000个数字,遍历到第8000个为止,并且是按顺序遍历
14021400
func TestRBTree_Iterate(t *testing.T) {
1403-
rbTree := NewRBTree[int, int](compare())
1404-
n := 10000
1405-
end := 8000
1406-
for i := n; i >= 1; i-- {
1407-
assert.Nil(t, rbTree.Add(i, i))
1408-
}
1409-
arr := make([]int, 0)
1410-
rbTree.Iterate(func(key, value int) bool {
1411-
if key > end {
1412-
return false
1413-
}
1414-
arr = append(arr, value)
1415-
return true
1416-
})
1417-
assert.Equal(t, end, len(arr))
1418-
for i := 1; i <= end; i++ {
1419-
assert.Equal(t, i, arr[i-1])
1401+
for _, testCase := range []struct {
1402+
name string
1403+
expectedLen int
1404+
inputStart int
1405+
inputEnd int
1406+
// 如果为true则遍历结束
1407+
endConditionFunc func(key int) bool
1408+
}{
1409+
{
1410+
name: "treeMap为空",
1411+
expectedLen: 0,
1412+
inputStart: 1,
1413+
inputEnd: 0,
1414+
endConditionFunc: func(key int) bool {
1415+
return false
1416+
},
1417+
},
1418+
{
1419+
name: "treeMap 有10000个元素,遍历所有小于等于8000的元素",
1420+
expectedLen: 8000,
1421+
inputStart: 1,
1422+
inputEnd: 10000,
1423+
endConditionFunc: func(key int) bool {
1424+
return key > 8000
1425+
},
1426+
},
1427+
{
1428+
name: "treeMap 有10000个元素,遍历所有元素",
1429+
expectedLen: 10000,
1430+
inputStart: 1,
1431+
inputEnd: 10000,
1432+
endConditionFunc: func(key int) bool {
1433+
return false
1434+
},
1435+
},
1436+
{
1437+
name: "treeMap 有10个元素,由于第一个就不符合条件所以遍历立刻中断",
1438+
expectedLen: 0,
1439+
inputStart: 1,
1440+
inputEnd: 10,
1441+
endConditionFunc: func(key int) bool {
1442+
return key < 5
1443+
},
1444+
},
1445+
} {
1446+
t.Run(testCase.name, func(t *testing.T) {
1447+
rbTree := NewRBTree[int, int](compare())
1448+
for i := testCase.inputStart; i <= testCase.inputEnd; i++ {
1449+
assert.Nil(t, rbTree.Add(i, i))
1450+
}
1451+
arr := make([]int, 0)
1452+
rbTree.Iterate(func(key, value int) bool {
1453+
if testCase.endConditionFunc(key) {
1454+
return false
1455+
}
1456+
arr = append(arr, value)
1457+
return true
1458+
})
1459+
assert.Equal(t, testCase.expectedLen, len(arr))
1460+
for i := 0; i < testCase.expectedLen; i++ {
1461+
assert.Equal(t, testCase.inputStart+i, arr[i])
1462+
}
1463+
})
14201464
}
14211465
}
14221466

mapx/treemap_test.go

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -446,24 +446,70 @@ func TestTreeMap_Len(t *testing.T) {
446446
}
447447

448448
func TestRBTree_Iterate(t *testing.T) {
449-
treeMap, err := NewTreeMap[int, int](compare())
450-
assert.Nil(t, err)
451-
n := 10000
452-
end := 8000
453-
for i := n; i >= 1; i-- {
454-
assert.Nil(t, treeMap.Put(i, i))
455-
}
456-
arr := make([]int, 0)
457-
treeMap.Iterate(func(key, value int) bool {
458-
if key > end {
459-
return false
460-
}
461-
arr = append(arr, value)
462-
return true
463-
})
464-
assert.Equal(t, end, len(arr))
465-
for i := 1; i <= end; i++ {
466-
assert.Equal(t, i, arr[i-1])
449+
for _, testCase := range []struct {
450+
name string
451+
expectedLen int
452+
inputStart int
453+
inputEnd int
454+
// 如果为true则遍历结束
455+
endConditionFunc func(key int) bool
456+
}{
457+
{
458+
name: "treeMap为空",
459+
expectedLen: 0,
460+
inputStart: 1,
461+
inputEnd: 0,
462+
endConditionFunc: func(key int) bool {
463+
return false
464+
},
465+
},
466+
{
467+
name: "treeMap 有10000个元素,遍历所有小于等于8000的元素",
468+
expectedLen: 8000,
469+
inputStart: 1,
470+
inputEnd: 10000,
471+
endConditionFunc: func(key int) bool {
472+
return key > 8000
473+
},
474+
},
475+
{
476+
name: "treeMap 有10000个元素,遍历所有元素",
477+
expectedLen: 10000,
478+
inputStart: 1,
479+
inputEnd: 10000,
480+
endConditionFunc: func(key int) bool {
481+
return false
482+
},
483+
},
484+
{
485+
name: "treeMap 有10个元素,由于第一个就不符合条件所以遍历立刻中断",
486+
expectedLen: 0,
487+
inputStart: 1,
488+
inputEnd: 10,
489+
endConditionFunc: func(key int) bool {
490+
return key < 5
491+
},
492+
},
493+
} {
494+
t.Run(testCase.name, func(t *testing.T) {
495+
treeMap, err := NewTreeMap[int, int](compare())
496+
assert.Nil(t, err)
497+
for i := testCase.inputStart; i <= testCase.inputEnd; i++ {
498+
assert.Nil(t, treeMap.Put(i, i))
499+
}
500+
arr := make([]int, 0)
501+
treeMap.Iterate(func(key, value int) bool {
502+
if testCase.endConditionFunc(key) {
503+
return false
504+
}
505+
arr = append(arr, value)
506+
return true
507+
})
508+
assert.Equal(t, testCase.expectedLen, len(arr))
509+
for i := 0; i < testCase.expectedLen; i++ {
510+
assert.Equal(t, testCase.inputStart+i, arr[i])
511+
}
512+
})
467513
}
468514
}
469515

0 commit comments

Comments
 (0)