File tree 3 files changed +36
-0
lines changed
3 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -116,6 +116,22 @@ type tree struct {
116
116
dummy node
117
117
}
118
118
119
+ func (t * tree ) Traverse (fn func (id Interval )) {
120
+ nodes := []* node {t .root }
121
+
122
+ for len (nodes ) != 0 {
123
+ c := nodes [len (nodes )- 1 ]
124
+ fn (c .interval )
125
+ nodes = nodes [:len (nodes )- 1 ]
126
+ if c .children [0 ] != nil {
127
+ nodes = append (nodes , c .children [0 ])
128
+ }
129
+ if c .children [1 ] != nil {
130
+ nodes = append (nodes , c .children [1 ])
131
+ }
132
+ }
133
+ }
134
+
119
135
func (tree * tree ) resetDummy () {
120
136
tree .dummy .children [0 ], tree .dummy .children [1 ] = nil , nil
121
137
tree .dummy .red = false
Original file line number Diff line number Diff line change @@ -622,3 +622,20 @@ func TestInsertDuplicateIntervalChildren(t *testing.T) {
622
622
result := tree .Query (constructSingleDimensionInterval (0 , 10 , 0 ))
623
623
assert .Contains (t , result , iv1 )
624
624
}
625
+
626
+ func TestTraverse (t * testing.T ) {
627
+ tree := newTree (1 )
628
+ top := 30
629
+ for i := 0 ; i <= top ; i ++ {
630
+ tree .Add (constructSingleDimensionInterval (int64 (i * 10 ), int64 ((i + 1 )* 10 ), uint64 (i )))
631
+ }
632
+ found := map [uint64 ]bool {}
633
+ tree .Traverse (func (id Interval ) {
634
+ found [id .ID ()] = true
635
+ })
636
+ for i := 0 ; i <= top ; i ++ {
637
+ if found , _ := found [uint64 (i )]; ! found {
638
+ t .Errorf ("could not find expected interval %d" , i )
639
+ }
640
+ }
641
+ }
Original file line number Diff line number Diff line change @@ -69,4 +69,7 @@ type Tree interface {
69
69
// interval. The provided interval's ID method is ignored so the
70
70
// provided ID is irrelevant.
71
71
Query (interval Interval ) Intervals
72
+ // Traverse will traverse tree and give alls intervals
73
+ // found in an undefined order
74
+ Traverse (func (Interval ))
72
75
}
You can’t perform that action at this time.
0 commit comments