From cc37554dfadfe1b04418f87b6ce071468f007eb4 Mon Sep 17 00:00:00 2001 From: Karl Gaissmaier Date: Sun, 22 Jan 2023 23:11:13 +0100 Subject: [PATCH] lookup without split() is a pity --- treap.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/treap.go b/treap.go index e82b667..9f248aa 100644 --- a/treap.go +++ b/treap.go @@ -376,25 +376,23 @@ func (n *node[T]) lcp(item T) (result T, ok bool) { // 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 } - } - - // 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 + } 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 + } } } - } // not found in right subtree, try this node