Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Jan 24, 2023
1 parent cc37554 commit 7c331fc
Showing 1 changed file with 8 additions and 22 deletions.
30 changes: 8 additions & 22 deletions treap.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,40 +367,25 @@ func (n *node[T]) lcp(item T) (result T, ok bool) {
cmp := compare(n.item, item)
switch {
case cmp > 0:
// left rec-descent
return n.left.lcp(item)
case cmp == 0:
// equality is always the shortest containing hull
return n.item, true
}

// now on proper depth in tree
// first try right subtree for shortest containing hull
if n.right != nil {
// rec-descent with n.right
if compare(n.right.item, item) <= 0 {
result, ok = n.right.lcp(item)
if ok {
return result, ok
}
} else {
// try n.right.left subtree for smallest containing hull
// take this path only if n.right.left.item > t.item (this node)
if n.right.left != nil && compare(n.right.left.item, n.item) > 0 {
// rec-descent with n.right.left
result, ok = n.right.left.lcp(item)
if ok {
return result, ok
}
}
}
// right backtracking
result, ok = n.right.lcp(item)
if ok {
return result, ok
}

// not found in right subtree, try this node
if covers(n.item, item) {
return n.item, true
}

// rec-descent with t.left
// left rec-descent
return n.left.lcp(item)
}

Expand Down Expand Up @@ -455,7 +440,7 @@ func (n *node[T]) scp(item T) (result T, ok bool) {
return
}

// rec-descent left subtree
// left backtracking
if result, ok = n.left.scp(item); ok {
return result, ok
}
Expand All @@ -465,6 +450,7 @@ func (n *node[T]) scp(item T) (result T, ok bool) {
return n.item, true
}

// right rec-descent
return n.right.scp(item)
}

Expand Down

0 comments on commit 7c331fc

Please sign in to comment.