Skip to content

Commit

Permalink
augmentedtree: add traverse func
Browse files Browse the repository at this point in the history
  • Loading branch information
azr committed Aug 2, 2016
1 parent 6f39ffc commit fcaff5a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
16 changes: 16 additions & 0 deletions augmentedtree/atree.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ 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]
fn(c.interval)
nodes = nodes[:len(nodes)-1]
if c.children[0] != nil {
nodes = append(nodes, c.children[0])
}
if c.children[1] != nil {
nodes = append(nodes, c.children[1])
}
}
}

func (tree *tree) resetDummy() {
tree.dummy.children[0], tree.dummy.children[1] = nil, nil
tree.dummy.red = false
Expand Down
3 changes: 3 additions & 0 deletions augmentedtree/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ type Tree interface {
// interval. The provided interval's ID method is ignored so the
// provided ID is irrelevant.
Query(interval Interval) Intervals
// Traverse will traverse tree and give alls intervals
// found in an undefined order
Traverse(func(Interval))
}

0 comments on commit fcaff5a

Please sign in to comment.