Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Feb 1, 2023
1 parent e2923f5 commit ee1c3ba
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions treap.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,22 +361,30 @@ func (t Tree[T]) CoverLCP(item T) (result T, ok bool) {

// lcp rec-descent.
func (t *Tree[T]) lcp(n *node[T], item T) (result T, ok bool) {
if n == nil {
return
}
for {
if n == nil {
// stop condition
return
}

// fast exit, node has too small max upper interval value (augmented value)
if t.cmpRR(item, n.maxUpper.item) > 0 {
return
}
// fast exit, node has too small max upper interval value (augmented value)
if t.cmpRR(item, n.maxUpper.item) > 0 {
// stop condition
return
}

switch cmp := t.compare(n.item, item); {
case cmp > 0:
// too big, left rec-descent
return t.lcp(n.left, item)
case cmp == 0:
// equality is always the shortest containing hull
return n.item, true
cmp := t.compare(n.item, item)
if cmp == 0 {
// equality is always the shortest containing hull
return n.item, true
}

if cmp < 0 {
break
}

// item too big, go left
n = n.left
}

// LCP => right backtracking
Expand Down

0 comments on commit ee1c3ba

Please sign in to comment.