2

Basically:

  • Fast, sorted insert.
  • Returns position of where an item would be inserted if it's not found in the data structure.

An array with binary search satisfies my second requirement, but it's still prohibitively slow for insertion. What solution might work best?

Hamster
  • 2,962
  • 7
  • 27
  • 38

3 Answers3

1

Red-black trees and skip lists meet your requirements, among others. For an example in C++, look at std::set, std::map, etc. and their lower_/upper_bound and equal_range methods.

Fred Nurk
  • 13,952
  • 4
  • 37
  • 63
0

Many flavors of search tree fit your requirements. I'd use a 2-3 tree, or maybe a treap if I was feeling lazy.

xscott
  • 2,350
  • 16
  • 18
0

A binary search tree.

fabrizioM
  • 46,639
  • 15
  • 102
  • 119