-2

Fastutil has nice class IntAVLTreeSet which has #firstInt() and #lastInt() method, that I require.

Unfortunately, AVL Tree is O(log N).

Are there O(1) implementations of this? Is it possible at all?

UPDATE

I want O(1) lookups. Finding margins may be slower.

Dims
  • 37,353
  • 77
  • 251
  • 478
  • Are you looking for better than O(logN) insert and O(1) firstInt() or constant time insert ? – Dave Durbin Apr 21 '17 at 09:01
  • Do you require an avl tree or will any kind of data structure do? You can get a constant time min lookup with a stack – Joni Apr 21 '17 at 09:06
  • Are you talking about `O(1)` for `lastInt()` or what? Saying "AVL Tree is O(log N)" is very unclear, since complexity is in operations and not the datastructures themselves. – Kayaman Apr 21 '17 at 09:13

1 Answers1

0

The class you mention: according to its source code, firstInt() and lastInt() are already O(1). The class caches the first and last entries in the tree.

If you want a more general lookup of any key to be O(1), you'll need a different data structure.

JeremyP
  • 80,230
  • 15
  • 117
  • 158