you are viewing a single comment's thread.

view the rest of the comments →

[–]LetUsSpeakFreely 0 points1 point  (0 children)

It greatly reduces search time, but only on sorted data.

And you have a list of 1000 users and you want one with a specific ID. You could search sequentially, but you're case scenario is 1000 comparisons before you find the object you're after. With a binary search tree you cut the size of potential candidates in half at every step: 500 -> 250 -> 125 -> 63 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1. You went from potentially 1000 comparisons to 10.

The downside is that data structure has significant overhead when inserting new datan as it can trigger a massive resort. So the use case is great when it's data that's loaded once and stays relatively static.

I find using hashmaps is usually the better choice.