Skip to content

Change traverse implemention to in-order traverse #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions augmentedtree/atree.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,21 @@ type tree struct {
dummy node
}

func (t *tree) Traverse(fn func(id Interval)) {
nodes := []*node{t.root}

for len(nodes) != 0 {
c := nodes[len(nodes)-1]
nodes = nodes[:len(nodes)-1]
func (t *tree) Traverse(fn func(id Interval) IterationResult) {
c := t.root
nodes := []*node{}
for len(nodes) > 0 || c != nil {
if c != nil {
fn(c.interval)
if c.children[0] != nil {
nodes = append(nodes, c.children[0])
}
if c.children[1] != nil {
nodes = append(nodes, c.children[1])
}
nodes = append(nodes, c)
c = c.children[0]
continue
}
c = nodes[len(nodes)-1]
nodes = nodes[:len(nodes)-1]
if !fn(c.interval) {
break
}
c = c.children[1]
}
}

Expand Down
6 changes: 4 additions & 2 deletions augmentedtree/atree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,17 +626,19 @@ func TestInsertDuplicateIntervalChildren(t *testing.T) {
func TestTraverse(t *testing.T) {
tree := newTree(1)

tree.Traverse(func(i Interval) {
tree.Traverse(func(i Interval) IterationResult {
assert.Fail(t, `traverse should not be called for empty tree`)
return IterationBreak
})

top := 30
for i := 0; i <= top; i++ {
tree.Add(constructSingleDimensionInterval(int64(i*10), int64((i+1)*10), uint64(i)))
}
found := map[uint64]bool{}
tree.Traverse(func(id Interval) {
tree.Traverse(func(id Interval) IterationResult {
found[id.ID()] = true
return IterationContinue
})
for i := 0; i <= top; i++ {
if found, _ := found[uint64(i)]; !found {
Expand Down
9 changes: 8 additions & 1 deletion augmentedtree/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ type Interval interface {
ID() uint64
}

type IterationResult bool

const (
IterationContinue IterationResult = true
IterationBreak IterationResult = false
)

// Tree defines the object that is returned from the
// tree constructor. We use a Tree interface here because
// the returned tree could be a single dimension or many
Expand All @@ -71,5 +78,5 @@ type Tree interface {
Query(interval Interval) Intervals
// Traverse will traverse tree and give alls intervals
// found in an undefined order
Traverse(func(Interval))
Traverse(func(Interval) IterationResult)
}