203
In this chapter
• You get a brief overview of 10 algorithms
that weren’t
covered in this book, and why
they’re useful.
• You get pointers on what to read next,
depending on what your interests are.
where to
go next
11
Trees
Let’s go back to the binary search example.
When a user logs in to Facebook,
Facebook
has to look through a big array to see if the
username exists. We said the fastest way to
search through this array is to run binary
search. But there’s a problem:
every time a new
user signs up, you insert their username into
the array. Then you have to re-sort the array,
because binary search
only works with sorted
arrays. Wouldn’t it be nice if you could insert
204
Chapter 11
I
Where to go next
the username into the right slot in the array right away, so you don’t
have to sort the array afterward? That’s
the idea behind the
binary search
tree
data structure.
A binary search tree looks like this.
For every node, the nodes to its left are
smaller
in value, and the nodes
to the right are
larger
in value.
Suppose you’re searching for Maggie. You start at the root node.
205
Trees
Maggie
comes after
David
, so go toward the right.
Maggie
comes before
Manning
, so go to the left.
You found Maggie! It’s almost like running a binary search!
Searching
for an element in a binary search tree takes O(log
n
) time
on average
and O(
n
) time in the
worst case
. Searching a sorted array takes O(log
n
)
time in the
worst case
, so you might think a sorted array is better. But a
binary search tree is a lot faster for insertions and deletions on average.
Binary search trees have some downsides too:
for one thing, you
don’t get random access. You can’t say, “Give me the fifth element of
this tree.” Those performance times are also on
average
and rely on
the tree being balanced. Suppose you have
an imbalanced tree like the
one shown next.