Skip to content
Merged
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
40 changes: 8 additions & 32 deletions pkg/adt/interval_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,38 +434,11 @@ func (ivt *intervalTree) createIntervalNode(ivl Interval, val any) *intervalNode
}
}

// TODO: make this consistent with textbook implementation
//
// "Introduction to Algorithms" (Cormen et al, 3rd ed.), chapter 13.3, p315
//
// RB-INSERT(T, z)
//
// y = T.nil
// x = T.root
//
// while x ≠ T.nil
// y = x
// if z.key < x.key
// x = x.left
// else
// x = x.right
//
// z.p = y
//
// if y == T.nil
// T.root = z
// else if z.key < y.key
// y.left = z
// else
// y.right = z
//
// z.left = T.nil
// z.right = T.nil
// z.color = RED
//
// RB-INSERT-FIXUP(T, z)

// Insert adds a node with the given interval into the tree.
//
// Cormen "Introduction to Algorithms", Chapter 14 Exercise 14.3.5.
// The algorithm follows Cormen "Introduction to Algorithms", Chapter 14 Exercise 14.3.5.
// for modifying an interval tree structure to support exact interval matching.
func (ivt *intervalTree) Insert(ivl Interval, val any) {
y := ivt.sentinel
z := ivt.createIntervalNode(ivl, val)
Expand Down Expand Up @@ -714,7 +687,10 @@ func (ivt *intervalTree) Visit(ivl Interval, ivv IntervalVisitor) {
ivt.root.visit(&ivl, ivt.sentinel, func(n *intervalNode) bool { return ivv(&n.iv) })
}

// find the exact node for a given interval
// find the exact node for a given interval. The implementation follows
// Cormen "Introduction to Algorithms", Chapter 14 Exercise 14.3.5. for
// exact interval matching. The search runs in O(log n) time on an n-node
// interval tree.
func (ivt *intervalTree) find(ivl Interval) *intervalNode {
x := ivt.root
// Search until hit sentinel or exact match.
Expand Down