diff --git a/treap.go b/treap.go index 9f248aa..84ae17b 100644 --- a/treap.go +++ b/treap.go @@ -367,32 +367,17 @@ 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 @@ -400,7 +385,7 @@ func (n *node[T]) lcp(item T) (result T, ok bool) { return n.item, true } - // rec-descent with t.left + // left rec-descent return n.left.lcp(item) } @@ -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 } @@ -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) }