Grokking Algorithms



Yüklə 348,95 Kb.
Pdf görüntüsü
səhifə100/122
tarix05.12.2023
ölçüsü348,95 Kb.
#173611
1   ...   96   97   98   99   100   101   102   103   ...   122
grokking-algorithms-illustrated-programmers-curious

Chapter 10
 
 
I
 
 
k-nearest neighbors
• Feature extraction means converting an item (like a fruit or a user) 
into a list of numbers that can be compared.
• Picking good features is an important part of a successful KNN 
algorithm.


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.


206

Yüklə 348,95 Kb.

Dostları ilə paylaş:
1   ...   96   97   98   99   100   101   102   103   ...   122




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin