2

The SICStus Prolog libraries assoc and avl both provide tree implementations of association lists.

While the binary trees used by library(assoc) are unbalanced (and thus can degrade to linear lists in the worst case), the binary trees used in library(avl) are kept balanced (using balancing operations upon insertion which utilize additional bookkeeping data).

So with library(avl) tree heights are guaranteed to be in O(log N)—even in the worst-case. With library(assoc), heights can range from log2(N) (best case) to O(log N) (average case) to N (worst case)—depending on the order of insertions.

So why then does library(avl) offer avl_height/2 while library(assoc) does not offer assoc_height/2? (I don't see a use case of avl_height/2, but I see a clear one of assoc_height/2.)

repeat
  • 18,496
  • 4
  • 54
  • 166
  • 1
    And why avl, when there are red black trees? – false May 28 '23 at 11:36
  • @false. Legend has it that AVL trees tend to be faster than RB trees (by some relatively small, but non-negligible amount.) – repeat May 28 '23 at 15:55
  • 1
    Faster for searching, as they are "more balanced". But slower for insertion. – false May 28 '23 at 16:01
  • @false. So it would be good to offer both and clarify when to use which one... (but that's a different subject) – repeat May 28 '23 at 16:05
  • @repeat The key is to offer both (and any others deemed useful) using the same _interface_ (see my comment to Per's answer). – Paulo Moura May 31 '23 at 17:00

1 Answers1

2

The reason these predicates exists respectively do not exist in the SICStus Prolog library is because this is how it was in Quintus Prolog, from which these libraries originated. I do not know why Quintus made this decision.

I agree that avl_height/2 seems useless whereas assoc_height/2 might be (marginally) useful.

On "why avl, when there are red black trees?": I do not know, but RB-trees are much trickier to implement, especially at the time when Quintus made the decision to offer AVL but not RB trees.

There are other alternatives than AVL and RB trees these days. I am not aware of any systematic investigation into which dictionary implementation would be "best" for Prolog (or SICStus Prolog).

Per Mildner
  • 10,469
  • 23
  • 30
  • Maybe the best dictionary implementation is application-dependent? No reason to provide a single implementation. See e.g. https://logtalk.org/manuals/libraries/dictionaries.html – Paulo Moura May 31 '23 at 16:57