Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Instant Download JavaScript Data Structures and Algorithms: An Introduction to Understanding and Implementing Core Data Structure and Algorithm Fundamentals 1st Edition Sammie Bae PDF All Chapters

The document promotes the ebook 'JavaScript Data Structures and Algorithms' by Sammie Bae, which focuses on understanding and implementing core data structures and algorithms. It provides links to download the ebook and additional recommended digital products related to data structures and algorithms. The document also includes details about the book's content, including chapters on Big-O notation, JavaScript specifics, and various data types.

Uploaded by

colawdivamik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Instant Download JavaScript Data Structures and Algorithms: An Introduction to Understanding and Implementing Core Data Structure and Algorithm Fundamentals 1st Edition Sammie Bae PDF All Chapters

The document promotes the ebook 'JavaScript Data Structures and Algorithms' by Sammie Bae, which focuses on understanding and implementing core data structures and algorithms. It provides links to download the ebook and additional recommended digital products related to data structures and algorithms. The document also includes details about the book's content, including chapters on Big-O notation, JavaScript specifics, and various data types.

Uploaded by

colawdivamik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Get the full ebook with Bonus Features for a Better Reading Experience on ebookmeta.

com

JavaScript Data Structures and Algorithms: An


Introduction to Understanding and Implementing
Core Data Structure and Algorithm Fundamentals 1st
Edition Sammie Bae
https://ebookmeta.com/product/javascript-data-structures-
and-algorithms-an-introduction-to-understanding-and-
implementing-core-data-structure-and-algorithm-
fundamentals-1st-edition-sammie-bae/

OR CLICK HERE

DOWLOAD NOW

Download more ebook instantly today at https://ebookmeta.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

A Textbook of Data Structures and Algorithms, Volume 3 :


Mastering Advanced Data Structures and Algorithm Design
Strategies 2nd Edition G. A. Vijayalakshmi Pai
https://ebookmeta.com/product/a-textbook-of-data-structures-and-
algorithms-volume-3-mastering-advanced-data-structures-and-algorithm-
design-strategies-2nd-edition-g-a-vijayalakshmi-pai/
ebookmeta.com

Data Structures and Algorithms with Python: With an


Introduction to Multiprocessing (Undergraduate Topics in
Computer Science) Lee
https://ebookmeta.com/product/data-structures-and-algorithms-with-
python-with-an-introduction-to-multiprocessing-undergraduate-topics-
in-computer-science-lee/
ebookmeta.com

Introduction to Algorithms & Data Structures 3: Learn


Linear Data Structures with Videos & Interview Questions.
1st Edition Aremu Bolakale
https://ebookmeta.com/product/introduction-to-algorithms-data-
structures-3-learn-linear-data-structures-with-videos-interview-
questions-1st-edition-aremu-bolakale/
ebookmeta.com

New Generation Korean Beginner Level Second Edition Mihyon


Jeon

https://ebookmeta.com/product/new-generation-korean-beginner-level-
second-edition-mihyon-jeon/

ebookmeta.com
Successful Supervision and Leadership Ensuring High
Performance Outcomes Using the PASE Model 1st Edition
Tracey Harris
https://ebookmeta.com/product/successful-supervision-and-leadership-
ensuring-high-performance-outcomes-using-the-pase-model-1st-edition-
tracey-harris/
ebookmeta.com

Clinical Interviewing, 6e Sixth Edition John Sommers-


Flanagan

https://ebookmeta.com/product/clinical-interviewing-6e-sixth-edition-
john-sommers-flanagan/

ebookmeta.com

One Road to Riches Haakon Gjerløw

https://ebookmeta.com/product/one-road-to-riches-haakon-gjerlow/

ebookmeta.com

Nationalism Referendums and Democracy Voting on Ethnic


Issues and Independence 2nd Edition Matt Qvortrup (Editor)

https://ebookmeta.com/product/nationalism-referendums-and-democracy-
voting-on-ethnic-issues-and-independence-2nd-edition-matt-qvortrup-
editor/
ebookmeta.com

A Strange Habit of Mind Cameron Winter 2 1st Edition


Andrew Klavan

https://ebookmeta.com/product/a-strange-habit-of-mind-cameron-
winter-2-1st-edition-andrew-klavan/

ebookmeta.com
Songs Words 2016th Edition Ginger Wildheart

https://ebookmeta.com/product/songs-words-2016th-edition-ginger-
wildheart/

ebookmeta.com
JavaScript Data
Structures and
Algorithms
An Introduction to Understanding and
Implementing Core Data Structure and
Algorithm Fundamentals

Sammie Bae
JavaScript Data Structures
and Algorithms
An Introduction to Understanding
and Implementing Core Data
Structure and Algorithm
Fundamentals

Sammie Bae
JavaScript Data Structures and Algorithms
Sammie Bae
Hamilton, ON, Canada

ISBN-13 (pbk): 978-1-4842-3987-2 ISBN-13 (electronic): 978-1-4842-3988-9


https://doi.org/10.1007/978-1-4842-3988-9
Library of Congress Control Number: 2019930417

Copyright © 2019 by Sammie Bae


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Louise Corrigan
Development Editor: Chris Nelson
Coordinating Editor: Nancy Chen
Cover designed by eStudioCalamar
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street,
6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-
sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member
(owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a
Delaware corporation.
For information on translations, please e-mail rights@apress.com, or visit www.apress.com/
rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at www.apress.com/9781484239872. For more
detailed information, please visit www.apress.com/source-code.
Printed on acid-free paper
This book is dedicated to Dr. Hamid R. Tizhoosh for inspiring
me in my studies and to my mother, Min Kyoung Seo, for her
kindness and support.
Table of Contents
About the Author�����������������������������������������������������������������������������������������������������xv

About the Technical Reviewer�������������������������������������������������������������������������������xvii


Acknowledgments��������������������������������������������������������������������������������������������������xix

Introduction������������������������������������������������������������������������������������������������������������xxi

Chapter 1: Big-O Notation����������������������������������������������������������������������������������������� 1


Big-O Notation Primer������������������������������������������������������������������������������������������������������������������� 1
Common Examples������������������������������������������������������������������������������������������������������������������ 2
Rules of Big-O Notation����������������������������������������������������������������������������������������������������������������� 4
Coefficient Rule: “Get Rid of Constants”���������������������������������������������������������������������������������� 5
Sum Rule: “Add Big-Os Up”����������������������������������������������������������������������������������������������������� 6
Product Rule: “Multiply Big-Os”���������������������������������������������������������������������������������������������� 7
Polynomial Rule: “Big-O to the Power of k”���������������������������������������������������������������������������� 8
Summary�������������������������������������������������������������������������������������������������������������������������������������� 8
Exercises��������������������������������������������������������������������������������������������������������������������������������������� 9
Answers��������������������������������������������������������������������������������������������������������������������������������� 11

Chapter 2: JavaScript: Unique Parts����������������������������������������������������������������������� 13


JavaScript Scope������������������������������������������������������������������������������������������������������������������������ 13
Global Declaration: Global Scope������������������������������������������������������������������������������������������� 13
Declaration with var: Functional Scope��������������������������������������������������������������������������������� 13
Declaration with let: Block Scope������������������������������������������������������������������������������������������ 15

v
Table of Contents

Equality and Types���������������������������������������������������������������������������������������������������������������������� 16


Variable Types������������������������������������������������������������������������������������������������������������������������ 16
Truthy/Falsey Check�������������������������������������������������������������������������������������������������������������� 17
=== vs ==���������������������������������������������������������������������������������������������������������������������������� 18
Objects���������������������������������������������������������������������������������������������������������������������������������� 18
Summary������������������������������������������������������������������������������������������������������������������������������������ 20

Chapter 3: JavaScript Numbers������������������������������������������������������������������������������ 21


Number System�������������������������������������������������������������������������������������������������������������������������� 21
JavaScript Number Object���������������������������������������������������������������������������������������������������������� 23
Integer Rounding������������������������������������������������������������������������������������������������������������������� 23
Number.EPSILON������������������������������������������������������������������������������������������������������������������� 24
Maximums����������������������������������������������������������������������������������������������������������������������������� 24
Minimums������������������������������������������������������������������������������������������������������������������������������ 25
Size Summary����������������������������������������������������������������������������������������������������������������������� 26
Number Algorithms���������������������������������������������������������������������������������������������������������������� 26
Prime Factorization��������������������������������������������������������������������������������������������������������������� 28
Random Number Generator�������������������������������������������������������������������������������������������������������� 29
Exercises������������������������������������������������������������������������������������������������������������������������������������� 29
Summary������������������������������������������������������������������������������������������������������������������������������������ 34

Chapter 4: JavaScript Strings��������������������������������������������������������������������������������� 35


JavaScript String Primitive��������������������������������������������������������������������������������������������������������� 35
String Access������������������������������������������������������������������������������������������������������������������������� 35
String Comparison����������������������������������������������������������������������������������������������������������������� 36
String Search������������������������������������������������������������������������������������������������������������������������� 36
String Decomposition������������������������������������������������������������������������������������������������������������ 38
String Replace����������������������������������������������������������������������������������������������������������������������� 38
Regular Expressions������������������������������������������������������������������������������������������������������������������� 38
Basic Regex��������������������������������������������������������������������������������������������������������������������������� 39
Commonly Used Regexes������������������������������������������������������������������������������������������������������ 39
Encoding������������������������������������������������������������������������������������������������������������������������������������� 41
Base64 Encoding������������������������������������������������������������������������������������������������������������������� 42

vi
Table of Contents

String Shortening������������������������������������������������������������������������������������������������������������������������ 43
Encryption����������������������������������������������������������������������������������������������������������������������������������� 45
RSA Encryption���������������������������������������������������������������������������������������������������������������������� 46
Summary������������������������������������������������������������������������������������������������������������������������������� 50

Chapter 5: JavaScript Arrays��������������������������������������������������������������������������������� 53


Introducing Arrays����������������������������������������������������������������������������������������������������������������������� 53
Insertion�������������������������������������������������������������������������������������������������������������������������������� 53
Deletion��������������������������������������������������������������������������������������������������������������������������������� 54
Access����������������������������������������������������������������������������������������������������������������������������������� 54
Iteration��������������������������������������������������������������������������������������������������������������������������������������� 54
for (Variables; Condition; Modification)��������������������������������������������������������������������������������� 55
for ( in )���������������������������������������������������������������������������������������������������������������������������������� 56
for ( of )���������������������������������������������������������������������������������������������������������������������������������� 56
forEach( )�������������������������������������������������������������������������������������������������������������������������������� 56
Helper Functions������������������������������������������������������������������������������������������������������������������������� 57
.slice(begin,end)�������������������������������������������������������������������������������������������������������������������� 57
.splice(begin,size,element1,element2…)������������������������������������������������������������������������������ 58
.concat()��������������������������������������������������������������������������������������������������������������������������������� 59
.length Property��������������������������������������������������������������������������������������������������������������������� 59
Spread Operator�������������������������������������������������������������������������������������������������������������������� 60
Exercises������������������������������������������������������������������������������������������������������������������������������������� 60
JavaScript Functional Array Methods����������������������������������������������������������������������������������������� 67
Map��������������������������������������������������������������������������������������������������������������������������������������� 67
Filter�������������������������������������������������������������������������������������������������������������������������������������� 68
Reduce���������������������������������������������������������������������������������������������������������������������������������� 68
Multidimensional Arrays������������������������������������������������������������������������������������������������������������� 68
Exercises������������������������������������������������������������������������������������������������������������������������������������� 71
Summary������������������������������������������������������������������������������������������������������������������������������������ 81

vii
Table of Contents

Chapter 6: JavaScript Objects�������������������������������������������������������������������������������� 83


JavaScript Object Property��������������������������������������������������������������������������������������������������������� 83
Prototypal Inheritance����������������������������������������������������������������������������������������������������������������� 84
Constructor and Variables����������������������������������������������������������������������������������������������������������� 85
Summary������������������������������������������������������������������������������������������������������������������������������������ 86
Exercises������������������������������������������������������������������������������������������������������������������������������������� 87

Chapter 7: JavaScript Memory Management��������������������������������������������������������� 89


Memory Leaks���������������������������������������������������������������������������������������������������������������������������� 89
Reference to an Object���������������������������������������������������������������������������������������������������������� 89
Leaking DOM������������������������������������������������������������������������������������������������������������������������� 90
Global window Object������������������������������������������������������������������������������������������������������������ 91
Limiting Object References��������������������������������������������������������������������������������������������������� 92
The delete Operator��������������������������������������������������������������������������������������������������������������� 92
Summary������������������������������������������������������������������������������������������������������������������������������������ 93
Exercises������������������������������������������������������������������������������������������������������������������������������������� 93

Chapter 8: Recursion���������������������������������������������������������������������������������������������� 99
Introducing Recursion����������������������������������������������������������������������������������������������������������������� 99
Rules of Recursion�������������������������������������������������������������������������������������������������������������������� 100
Base Case���������������������������������������������������������������������������������������������������������������������������� 100
Divide-and-Conquer Method����������������������������������������������������������������������������������������������� 101
Classic Example: Fibonacci Sequence�������������������������������������������������������������������������������� 101
Fibonacci Sequence: Tail Recursion������������������������������������������������������������������������������������ 102
Pascal’s Triangle������������������������������������������������������������������������������������������������������������������ 103
Big-O for Recursion������������������������������������������������������������������������������������������������������������������� 105
Recurrence Relations���������������������������������������������������������������������������������������������������������� 105
Master Theorem������������������������������������������������������������������������������������������������������������������ 106
Recursive Call Stack Memory��������������������������������������������������������������������������������������������������� 107
Summary���������������������������������������������������������������������������������������������������������������������������������� 109
Exercises����������������������������������������������������������������������������������������������������������������������������������� 109

viii
Table of Contents

Chapter 9: Sets����������������������������������������������������������������������������������������������������� 117


Introducing Sets������������������������������������������������������������������������������������������������������������������������ 117
Set Operations�������������������������������������������������������������������������������������������������������������������������� 117
Insertion������������������������������������������������������������������������������������������������������������������������������ 118
Deletion������������������������������������������������������������������������������������������������������������������������������� 118
Contains������������������������������������������������������������������������������������������������������������������������������� 118
Other Utility Functions�������������������������������������������������������������������������������������������������������������� 119
Intersection�������������������������������������������������������������������������������������������������������������������������� 119
isSuperSet��������������������������������������������������������������������������������������������������������������������������� 119
Union����������������������������������������������������������������������������������������������������������������������������������� 120
Difference���������������������������������������������������������������������������������������������������������������������������� 120
Summary���������������������������������������������������������������������������������������������������������������������������������� 121
Exercises����������������������������������������������������������������������������������������������������������������������������������� 122

Chapter 10: Searching and Sorting���������������������������������������������������������������������� 125


Searching���������������������������������������������������������������������������������������������������������������������������������� 125
Linear Search���������������������������������������������������������������������������������������������������������������������� 125
Binary Search���������������������������������������������������������������������������������������������������������������������� 127
Sorting�������������������������������������������������������������������������������������������������������������������������������������� 129
Bubble Sort�������������������������������������������������������������������������������������������������������������������������� 129
Selection Sort���������������������������������������������������������������������������������������������������������������������� 131
Insertion Sort����������������������������������������������������������������������������������������������������������������������� 132
Quicksort����������������������������������������������������������������������������������������������������������������������������� 134
Quickselect�������������������������������������������������������������������������������������������������������������������������� 137
Mergesort���������������������������������������������������������������������������������������������������������������������������� 138
Count Sort���������������������������������������������������������������������������������������������������������������������������� 140
JavaScript’s Built-in Sort����������������������������������������������������������������������������������������������������� 141
Summary���������������������������������������������������������������������������������������������������������������������������������� 142
Exercises����������������������������������������������������������������������������������������������������������������������������������� 143

ix
Table of Contents

Chapter 11: Hash Tables��������������������������������������������������������������������������������������� 151


Introducing Hash Tables������������������������������������������������������������������������������������������������������������ 151
Hashing Techniques������������������������������������������������������������������������������������������������������������������ 152
Prime Number Hashing������������������������������������������������������������������������������������������������������� 152
Probing�������������������������������������������������������������������������������������������������������������������������������� 154
Rehashing/Double-Hashing������������������������������������������������������������������������������������������������� 155
Hash Table Implementation������������������������������������������������������������������������������������������������������� 156
Using Linear Probing����������������������������������������������������������������������������������������������������������� 156
Using Quadratic Probing������������������������������������������������������������������������������������������������������ 158
Using Double-Hashing with Linear Probing������������������������������������������������������������������������� 160
Summary���������������������������������������������������������������������������������������������������������������������������������� 161

Chapter 12: Stacks and Queues���������������������������������������������������������������������������� 163


Stacks��������������������������������������������������������������������������������������������������������������������������������������� 163
Peek������������������������������������������������������������������������������������������������������������������������������������� 165
Insertion������������������������������������������������������������������������������������������������������������������������������ 165
Deletion������������������������������������������������������������������������������������������������������������������������������� 166
Access��������������������������������������������������������������������������������������������������������������������������������� 166
Search��������������������������������������������������������������������������������������������������������������������������������� 167
Queues�������������������������������������������������������������������������������������������������������������������������������������� 167
Peek������������������������������������������������������������������������������������������������������������������������������������� 169
Insertion������������������������������������������������������������������������������������������������������������������������������ 169
Deletion������������������������������������������������������������������������������������������������������������������������������� 169
Access��������������������������������������������������������������������������������������������������������������������������������� 170
Search��������������������������������������������������������������������������������������������������������������������������������� 171
Summary���������������������������������������������������������������������������������������������������������������������������������� 171
Exercises����������������������������������������������������������������������������������������������������������������������������������� 172

Chapter 13: Linked Lists��������������������������������������������������������������������������������������� 179


Singly Linked Lists�������������������������������������������������������������������������������������������������������������������� 179
Insertion������������������������������������������������������������������������������������������������������������������������������ 180
Deletion by Value����������������������������������������������������������������������������������������������������������������� 181

x
Table of Contents

Deletion at the Head������������������������������������������������������������������������������������������������������������ 182


Search��������������������������������������������������������������������������������������������������������������������������������� 183
Doubly Linked Lists������������������������������������������������������������������������������������������������������������������� 184
Insertion at the Head����������������������������������������������������������������������������������������������������������� 185
Insertion at the Tail�������������������������������������������������������������������������������������������������������������� 185
Deletion at the Head������������������������������������������������������������������������������������������������������������ 186
Deletion at the Tail��������������������������������������������������������������������������������������������������������������� 187
Search��������������������������������������������������������������������������������������������������������������������������������� 188
Summary���������������������������������������������������������������������������������������������������������������������������������� 189
Exercises����������������������������������������������������������������������������������������������������������������������������������� 190

Chapter 14: Caching��������������������������������������������������������������������������������������������� 193


Understanding Caching������������������������������������������������������������������������������������������������������������� 193
Least Frequently Used Caching������������������������������������������������������������������������������������������������� 194
Least Recently Used Caching���������������������������������������������������������������������������������������������������� 199
Summary���������������������������������������������������������������������������������������������������������������������������������� 203

Chapter 15: Trees�������������������������������������������������������������������������������������������������� 205


General Tree Structure�������������������������������������������������������������������������������������������������������������� 205
Binary Trees������������������������������������������������������������������������������������������������������������������������������ 206
Tree Traversal���������������������������������������������������������������������������������������������������������������������������� 207
Pre-order Traversal�������������������������������������������������������������������������������������������������������������� 207
In-Order Traversal���������������������������������������������������������������������������������������������������������������� 209
Post-order Traversal������������������������������������������������������������������������������������������������������������ 211
Level-Order Traversal���������������������������������������������������������������������������������������������������������� 212
Tree Traversal Summary������������������������������������������������������������������������������������������������������ 214
Binary Search Trees������������������������������������������������������������������������������������������������������������������ 214
Insertion������������������������������������������������������������������������������������������������������������������������������ 216
Deletion������������������������������������������������������������������������������������������������������������������������������� 218
Search��������������������������������������������������������������������������������������������������������������������������������� 220

xi
Table of Contents

AVL Trees����������������������������������������������������������������������������������������������������������������������������������� 221


Single Rotation�������������������������������������������������������������������������������������������������������������������� 221
Double Rotation������������������������������������������������������������������������������������������������������������������� 225
Balancing the Tree��������������������������������������������������������������������������������������������������������������� 228
Insertion������������������������������������������������������������������������������������������������������������������������������ 229
Putting It All Together: AVL Tree Example����������������������������������������������������������������������������� 231
Summary���������������������������������������������������������������������������������������������������������������������������������� 234
Exercises����������������������������������������������������������������������������������������������������������������������������������� 234

Chapter 16: Heaps������������������������������������������������������������������������������������������������ 245


Understanding Heaps���������������������������������������������������������������������������������������������������������������� 245
Max-Heap���������������������������������������������������������������������������������������������������������������������������� 246
Min-Heap����������������������������������������������������������������������������������������������������������������������������� 247
Binary Heap Array Index Structure�������������������������������������������������������������������������������������������� 248
Percolation: Bubbling Up and Down������������������������������������������������������������������������������������ 250
Implementing Percolation��������������������������������������������������������������������������������������������������� 253
Max-Heap Example������������������������������������������������������������������������������������������������������������� 254
Min-Heap Complete Implementation���������������������������������������������������������������������������������������� 258
Max-Heap Complete Implementation��������������������������������������������������������������������������������������� 259
Heap Sort���������������������������������������������������������������������������������������������������������������������������������� 261
Ascending-Order Sort (Min-Heap)��������������������������������������������������������������������������������������� 261
Descending-Order Sort (Max-Heap)������������������������������������������������������������������������������������ 264
Summary���������������������������������������������������������������������������������������������������������������������������������� 267
Exercises����������������������������������������������������������������������������������������������������������������������������������� 268

Chapter 17: Graphs����������������������������������������������������������������������������������������������� 273


Graph Basics����������������������������������������������������������������������������������������������������������������������������� 273
Undirected Graphs�������������������������������������������������������������������������������������������������������������������� 277
Adding Edges and Vertices�������������������������������������������������������������������������������������������������� 279
Removing Edges and Vertices��������������������������������������������������������������������������������������������� 280
Directed Graphs������������������������������������������������������������������������������������������������������������������������ 282

xii
Table of Contents

Graph Traversal������������������������������������������������������������������������������������������������������������������������� 285


Breadth-First Search����������������������������������������������������������������������������������������������������������� 286
Depth-First Search�������������������������������������������������������������������������������������������������������������� 289
Weighted Graphs and Shortest Path����������������������������������������������������������������������������������������� 293
Graphs with Weighted Edges����������������������������������������������������������������������������������������������� 293
Dijkstra’s Algorithm: Shortest Path�������������������������������������������������������������������������������������� 294
Topological Sort������������������������������������������������������������������������������������������������������������������������ 298
Summary���������������������������������������������������������������������������������������������������������������������������������� 300

Chapter 18: Advanced Strings������������������������������������������������������������������������������ 303


Trie (Prefix Tree)������������������������������������������������������������������������������������������������������������������������ 303
Boyer–Moore String Search������������������������������������������������������������������������������������������������������ 307
Knuth–Morris–Pratt String Search�������������������������������������������������������������������������������������������� 311
Rabin–Karp Search������������������������������������������������������������������������������������������������������������������� 316
The Rabin Fingerprint���������������������������������������������������������������������������������������������������������� 316
Applications in Real Life������������������������������������������������������������������������������������������������������ 319
Summary���������������������������������������������������������������������������������������������������������������������������������� 320

Chapter 19: Dynamic Programming��������������������������������������������������������������������� 321


Motivations for Dynamic Programming������������������������������������������������������������������������������������ 321
Rules of Dynamic Programming����������������������������������������������������������������������������������������������� 323
Overlapping Subproblems��������������������������������������������������������������������������������������������������� 323
Optimal Substructure���������������������������������������������������������������������������������������������������������� 323
Example: Ways to Cover Steps�������������������������������������������������������������������������������������������� 323
Classical Dynamic Programming Examples������������������������������������������������������������������������������ 325
The Knapsack Problem�������������������������������������������������������������������������������������������������������� 325
Longest Common Subsequence������������������������������������������������������������������������������������������ 328
Coin Change������������������������������������������������������������������������������������������������������������������������ 330
Edit (Levenshtein) Distance������������������������������������������������������������������������������������������������� 334
Summary���������������������������������������������������������������������������������������������������������������������������������� 338

xiii
Table of Contents

Chapter 20: Bit Manipulation�������������������������������������������������������������������������������� 339


Bitwise Operators��������������������������������������������������������������������������������������������������������������������� 339
AND�������������������������������������������������������������������������������������������������������������������������������������� 340
OR���������������������������������������������������������������������������������������������������������������������������������������� 340
XOR�������������������������������������������������������������������������������������������������������������������������������������� 341
NOT�������������������������������������������������������������������������������������������������������������������������������������� 341
Left Shift������������������������������������������������������������������������������������������������������������������������������ 342
Right Shift���������������������������������������������������������������������������������������������������������������������������� 342
Zero-Fill Right Shift������������������������������������������������������������������������������������������������������������� 343
Number Operations������������������������������������������������������������������������������������������������������������������� 343
Addition������������������������������������������������������������������������������������������������������������������������������� 343
Subtraction�������������������������������������������������������������������������������������������������������������������������� 344
Multiplication����������������������������������������������������������������������������������������������������������������������� 345
Division�������������������������������������������������������������������������������������������������������������������������������� 347
Summary���������������������������������������������������������������������������������������������������������������������������������� 349

Index��������������������������������������������������������������������������������������������������������������������� 351

xiv
About the Author
Sammie Bae is a data engineer at Yelp and previously
worked for the data platform engineering team at
NVIDIA. He developed a deep interest in JavaScript
during an internship at SMART Technologies (acquired by
Foxconn), where he developed Node.js-based JavaScript
APIs for serial port communication between electronic
board drivers and a web application. Despite how relevant
JavaScript is to the modern software engineering industry,
currently no books besides this one teach algorithms and
data structures using JavaScript. Sammie understands how
difficult these computer science concepts are and aims to
provide clear and concise explanations in this book.

xv
Other documents randomly have
different content
PLATE CCCXXV.

CODON ROYENI.
Prickly Codon.
CLASS XI. ORDER I.
DODECANDRIA MONOGYNIA. Twelve Chives. One Pointal.
GENERIC CHARACTER.
Calyx. Perianthium monophyllum, duodecempartitum; foliolis subulatis,
persistentibus, erectis, alternis brevioribus.
Corolla monopetala, campanulata, basi torulosa; limbo
duodecimpartito, aquali.
Nectarium duodecim loculare ex squamis 12, unguibus staminum insertis,
conniventibus, receptaculum tegentibus.
Stamina. Filamenta duodecim, longitudine corollæ. Antheræ crassæ.
Pistillum. Germen superum, conicum. Stylus simplex, longitudine
staminum. Stigmata duo, longa, setacea, divergentia.
Pericarpium. Capsula bilocularis.
Semina plurima, subrotunda, nigra.
Empalement. Cup one-leaf, twelve-divided; leaflets awl-shaped,
remaining, upright, the alternate ones shorter.
Blossom one petal, bell-shaped, rather swelled at the base; border twelve-
divided, equal.
Honey-cup twelve-celled, consisting of 12 scales, inserted into the claws of
the chives, approaching, covering the receptacle.
Chives. Twelve threads, the length of the blossom. Tips thick.
Pointal. Seed-bud above, conical. Shaft simple, the length of the chives.
Summits two, long, like bristles, straddling.
Seed Vessel. Capsule two-celled.
Seeds numerous, roundish, black,
SPECIFIC CHARACTER.
Codon foliis alternis, cordato-ovatis, acuminatis, setaceo-spinosis;
floribus subcapitatis, terminalibus.
Codon with alternate leaves, between heart-shaped and oval, tapered to
the point, with spine-like bristles; flowers grow nearly in heads, terminating
the branches.
REFERENCE TO THE PLATE.
1. The Empalement.
2. A Blossom spread open, with the chives in their place.
3. The same shewn from the outside.
4. The Seed-bud, Shaft and Summits.
5. The Seed-bud, cut transversely.
As an interesting desideratum, this plant has stood long amongst botanical
collectors; but till the year 1801, it was never seen, in a vegetating state, in
Britain; at which period, it was introduced to our gardens, by Mr. Niven,
from the Cape of Hope. It is not, we believe, (although considered hitherto
as such,) an annual; but rather, a half woody shrub, easily destroyed by
moisture. As the seed appears to ripen freely, although there seems no other
mode of perpetuating the plant, we shall stand in no danger of losing it; its
attractions are, indeed, not great as to beauty, but much so as to singularity;
since we have no plant to which it affines in general habit. The seeds should
be sown on a gentle hot-bed, in March, and transplanted when about two
inches high, into pots, to flower, which they will do about September. Our
drawing was made at the Hammersmith Nursery. Having had the opportunity
of examining the flowers, of more than twenty plants; we are convinced it
has been wrong placed by Van Royen to decandria; for although many
flowers may be found, even on the same plant, some with eight, others with
ten chives, yet at least 90 out of 100 have twelve; wherefore we have,
without scruple, placed it to dodecandria.
PLATE CCCXXVI.

PHARNACEUM LINEARE.
Linear-leaved Pharnaceum.
CLASS V. ORDER III.
PENTANDRIA TRIGYNIA. Five Chives. Three Pointal.
GENERIC CHARACTER.
Calyx. Perianthium pentaphyllum; foliolis subovatis, concavis,
patentibus, æqualibus, persistentibus, intus coloratus, margine tenui.
Corolla nulla; hinc calycis margo tenuis et calyx interne coloratus.
Stamina. Filamenta quinque, subulata, longitudine calycis. Antheræ basi
bifidæ.
Pistilla. Germen ovatum, trigonum. Styli tres, filiformes, longitudine
staminum. Stigmata obtusa.
Pericarpium. Capsula ovata, obsoletè trigona, tecta, trilocularis, trivalvis.
Semina numerosa, nitida, orbiculata, depressa, margine acuto cincta.
Empalement. Cup five-leaved; leaflets nearly egg-shaped, concave,
spreading, equal, permanent, coloured within, with a thin edge.
Blossom none; hence the edge of the cup is thin and the inside of it is
coloured.
Chives. Five-threads, awl-shaped, the length of the cup. Tips cleft at the
base.
Pointals. Seed-bud egg-shaped, three-sided. Shafts three, thread-shaped,
the length of the chives. Summits blunt.
Seed-vessel. Capsule egg-shaped, bluntly three-cornered, covered, three-
celled, three valves.
Seeds numerous, shining, orbicular, flattened, surrounded by a sharp rim.
SPECIFIC CHARACTER.
Pharnaceum foliis linearibus, verticillatis; umbellis inequalibus; calycibus
aurantiis; caule decumbente.
Pharnaceum with linear leaves, growing in whorls; umbels unequal; cups
orange coloured; stem bending downwards.
REFERENCE TO THE PLATE.
1. The Cup, shewn from the front, with the chives.
2. The same, shewn from the hind part attached to the fruit-stalk.
3. The Seed-bud and Pointals, natural size.
4. The same, magnified.
5. The Seed-bud, cut transversely, magnified.
This little plant much resembles our Corn Spurry, or Spergula arvensis, at
first sight. It is a native of the Cape of Good Hope, seldom grows more than
six inches high, and is very apt to be destroyed by the damps of our winters.
It should be placed in the airyest part of the greenhouse, and kept in light
sandy loam. To propagate it, the cuttings should be put in, about the month
of March, in the heat of a gentle hot-bed, or the bark-bed of the hot-house;
the seeds are sometimes perfected. It has been often introduced to us, but as
often lost; as it is not a plant of much shew, no great deal of pains has,
hitherto, been taken for its preservation. Our drawing was taken from a plant
in the Hibbertian Collection, in the month of July, 1802.
PLATE CCCXXVII.

XERANTHEMUM ARGENTEUM.
Silvery Everlasting-Flower.
CLASS XIX. ORDER II.
SYNGENESIA POLYGAMIA SUPERFLUA. Tips united. Superfluous
Pointals.
ESSENTIAL GENERIC CHARACTER.
Receptaculum paleaceum. Pappus setaceus. Calyx imbricatus, radiatus;
radio colorato.
Receptacle chaffy. Feather bristly. Cup tiled, rayed; ray coloured.
See Xeranthemum speciocissimum, V. I. Pl. LI.
SPECIFIC CHARACTER.
Xeranthemum fruticosum; foliis lanceolatis, carinatis, sericeis; floribus
paniculatis, argenteis.
Everlasting flower with a shrubby stem; leaves lance-shaped, keeled, and
silky; flowers grow in panicles, and silvery.
REFERENCE TO THE PLATE.
1. An hermaphrodite Floret, natural size.
2. The same, magnified.
3. An inner scale of the Empalement.
4. The Receptacle, cleared of the scales and florets.
5. A female Floret, magnified.
6. The Pointal of the above, natural size.
7. The same magnified.
This new species of Everlasting-flower was introduced to us, at the same
time, with the X. spirale this work, Pl. CCLXII. by Montague Burgoyne,
Esq. of Mark Hall, Essex, in the year 1801. It is a quick growing plant, and
has flowered for the first time last April, when our drawing was taken. The
flower stems grow to the length of eighteen inches, or more; and the buds,
upon their first appearance, have a fine tint of flesh colour, which, however,
totally disappears upon the expansion of the flower. It is propagated by
cuttings, seeds, (which ripen in this country,) or by parting the branches at
the root, as it makes many from one base, which are easily separated by
passing a knife between them. It thrives best in sandy peat with a small
portion of loam, and must be kept in an airy, dry part of the greenhouse.
PLATE CCCXXVIII.

GERANIUM BUBONIFOLIUM.
Macedonian-Parsley-leaved Geranium.
CLASS XVI. ORDER IV.
MONADELPHIA DECANDRIA. Threads united. Ten Chives.
ESSENTIAL GENERIC CHARACTER.
Monogyna. Stigmata quinque. Fructus rostratus, pentacoccus.
One Pointal. Five Summits. Fruit furnished with long awns, five dry
berries.
SPECIFIC CHARACTER.
Geranium foliis pinnatis, pinnis oppositis, lobatis, distantibus, lobis
inciso-dentatis, glabris; petalis emarginatis, albis; staminibus quinque
fertilibus; radice tuberosa.
Geranium with winged leaves, wings opposite, lobed, distant, lobes deep
cut and toothed, smooth; petals notched at the end, white, five fertile chives;
root tuberous.
REFERENCE TO THE PLATE.
1. The Cup cut and spread open.
2. The Chives and Pointal.
3. The Chives cut and spread open, magnified.
4. The Pointal, magnified.
This Geranium, from the Cape of Good Hope, is quite distinct in its habit,
from most of the tuberous species; the leaflets or wings of the leaves being
more hand-shaped, and at a greater distance from each other, than in any
which has come under our observation; having much the appearance of those
of the plant from which we have derived its specific title. The plant was
introduced by Mr. Niven, to the Clapham Collection, in 1800; where our
drawing was taken in the month of June, 1802. The cultivation as usual, for
the plants of this branch of the Geranium family.
PLATE CCCXXIX.

P H A R N A C E U M L I N E A R E . Va r. f l o . a l b o .
Linear-leaved Pharnaceum. White Var.
CLASS V. ORDER III.
PENTANDRIA TRIGYNIA. Five Chives. Three Pointals.
ESSENTIAL GENERIC CHARACTER.
Calyx 5-phyllus. Corolla nulla.
Capsula 3-locularis, polysperma.
Cup 5-leaved. Blossom none.
Capsule 3-celled, many-seeded.
See Pharnaceum lineare. Pl. CCCXXVI. V. V.
SPECIFIC CHARACTER.

Pharnaceum foliis linearibus, verticillatis; umbellis inequalibus; calycibus


albo-virentibus; caule decumbente.
Pharnaceum with linear leaves, growing in whorls; umbels unequal; cups
whitish green; stem bending downwards.
REFERENCE TO THE PLATE.
1. A Flower.
2. The Cup and Chives.
3. The Seed-bud and Pointals, natural size.
4. The same, magnified.
5. The Seed-bud, cut transversely, magnified.
We have given this variety of the linear-leaved Pharnaceum, at the same time
with the orange-coloured, as being the only two Cape species, at present, in
our gardens. It is equally tender and delicate with its fellow, is equally
difficult to propagate; and was introduced, this last time, to the Hibbertian
Collection in 1800, by Mr. Niven, with it.
PLATE CCCXXX.

N Y M P H Æ A S T E L L ATA .
Starr’d Water-Lily.
CLASS XIII. ORDER I.
POLYANDRIA MONOGYNIA. Many Chives. One Pointal.
ESSENTIAL GENERIC CHARACTER.
Calyx 4, 5, seu 6-phyllus. Cor. polypetala. Bacca multilocularis, truncata.
Cup 4, 5 or 6-leaved. Bloss. many petals. Berry many-celled, appearing
cut off at top.
See Nymphæa cœrulea, Pl. CXCVII. Vol. III.
SPECIFIC CHARACTER.
Nymphæa foliis subrotundis, sinuatis, subtus purpurascentibus; lobis
divaricatis, acutis; calyce tetraphyllo, petalis acutis cœruleis longiore.
Water-lily with nearly round leaves, scollopped and purplish underneath;
lobes straddling and pointed; cup four-leaved, longer than the pointed, blue
petals.
REFERENCE TO THE PLATE.
1. The Cup divested of the petals.
2. A Chive, natural size.
3. The seed-vessel, nearly arrived to maturity.
4. The same, cut transversely.
This fine species of Water-lily is a native of the East Indies, on the Malabar
coast; and has been given by Rheed, in his Hortus Malabaricus, Vol. II. p.
53. fig. 27. At first sight, it has much the appearance of N. cœrulea, but upon
closer inspection it is found to possess many strongly differing characters,
viz. In this, the under part of the leaves are purplish, the lobes straddle; in
that, they are tiled, or lay upon each other; in N. stellata the petals or chives
seldom exceed ten; whereas, the N. cœrulea has an indefinite number of
both. The roots, likewise, are perfectly distinct; in N. cœrulea the diverging
roots proceed from the lower part of the bulb, from whence the leaves issue,
but in N. stellata from the top, leaving below them a smooth top-shaped
bulb. The parts which compose the summit, or rather the summits, (for
indeed we should consider them, if it lay with us, as distinct pointals,) are
expanded horizontally when the flower is in its perfect state, forming a star
like appearance, whence its specific name. It is much tenderer than N.
cœrulea, and will not flower but in strong heat. Mr. Anderson informs us that
the plants which have produced perfect seeds, this year, were from seeds,
received by A. B. Lambert, Esq. (communicated by Dr. Roxburgh from
India) in the month of March, and raised by him for the collection of J. Vere,
Esq. Kensington Gore, where our drawing was made in August.
PLATE CCCXXXI.

X Y L O P H Y L L A FA L C ATA .
Cymitar-shaped-leaved Xylophylla.
CLASS XXI. ORDER III.
MONŒCIA TRIANDRIA. Chives and Pointals separate. Three Chives.
GENERIC CHARACTER.
* Masculi flores.
Calyx. Perianthium monophyllum, tripartitum, coloratum; laciniis ovatis,
patulis, ovatis.
Corolla. Petala tria, ovata, concava.
Stamina. Filamenta tria, brevissima, basi approximata. Antheræ
subrotundæ, didymæ.
* Feminei flores in eodem fasciculo cum masculis.
Calyx. Perianthium ut in mare.
Corolla ut in mare.
Nectarium corpusculis glandulosis germen ambientibus.
Pistillum. Germen subrotundum, trisulcatum. Styli tres, patuli, bifidi.
Stigmata obtusa.
Pericarpium. Capsula trigona, trisulca, trilocularis; loculis bivalvibus.
Semina solitaria.
* Male flowers.
Empalement. Cup one leaf, three divided, coloured; segments egg-shaped,
spreading, oval.
Blossom. Petals three, egg-shaped, concave.
Chives. Threads three, very short, and approaching at the base. Tips
roundish, double.
* Female flowers in the same tuft with the males.
Empalement. Cup as in the male.
Blossom as in the male.
Honey-cup is formed of small glandular bodies surrounding the seed-bud.
Pointal. Seed-bud roundish, three-furrowed. Shafts three, short.
Summits blunt.
Seed-vessel. Capsule three-sided, three-furrowed, three cells; cells two-
valved.
Seeds solitary.
SPECIFIC CHARACTER.
Xylophylla foliis sparsis, integris, falcatis, crenatis; crenis remotis,
floriferis; floribus rubris.
Xylophylla with leaves scattered on the stem, entire, cymitar-shaped and
scolloped; the scollops distant, and having the flowers on them; flowers red.
REFERENCE TO THE PLATE.
1. A male flower, magnified.
2. The blossom of male flowers, spread open, with the honey-cups.
3. A Female flower, magnified.
4. The Germ from a female flower, cut transversely, magnified.
It should seem that this Genus has not been observed with sufficient
accuracy, at least those plants known to us, and which have been treated of
as belonging to the class Pentandria. The present one, for instance, which is
unquestionably the plant designed by Swartz, Commelin, Seba, &c. an is
acknowledged by Swartz to be triandrous and monœcious, yet that it should
be continued in the Kew Catalogue and by Willdenow, &c. to Pentandria, is
astonishing. We have no hesitation in declaring our opinion that the whole
genus as it now stands ought to be thrown to Phyllanthus. But, as the title
Xylophylla is so well known for these plants; we have thought it more safe
to give the Genus those characters the plants bear under their true Class and
Order, than make any alteration in it. The Xylophylla falcata is a native of
the West India Islands, and has been long cultivated in our hot-houses; so
long ago as the year 1739. It is a tender plant, and will not flower without the
assistance of the bark-bed; is easily propagated by cuttings; grows best in
sandy peat, and flowers in August. Our drawing was made at the
Hammersmith Nursery.

You might also like