1

I would like to know which balanced BST would be easy to code in C++, and still have a complexity roughly equal to O(logn).

I've already tried Red Black trees, but would like an alternative that is less complex to code. I have worked with Treaps in the past, but am interested in exploring options that either perform better or are easier to implement.

What are your suggestions?

Akshay
  • 814
  • 6
  • 19
Tamim Addari
  • 7,591
  • 9
  • 40
  • 59

1 Answers1

3

AVL trees generally perform better than Treaps in my experience, and they're not any harder to implement.

They work by rotating branches of the tree that become unbalanced after any insertion or deletion. This guarantees that they will have perfect balance so they can't be "tricked" by strange data.

Rotations in an AVL Tree

Treaps on the other hand are distributed randomly, which for large data sets is close to balanced, but you still don't get that perfect O(logn). Furthermore you could just happen to come across a data set that inserts in a very unbalanced way, and your access time can get close to O(n).

Check out wikipedia's page for more info: en.wikipedia.org/wiki/Avl_tree

Akshay
  • 814
  • 6
  • 19
  • 3
    Sorry to say, but you are wrong. The source of randomness in treaps is not the data set. Normal BST's without any balancing perform well on random data sets. In treaps we provide our own randomness, like in Quick Sort, so worst case scenario can not be forced by input. – Szymon Jul 13 '19 at 14:36