100% found this document useful (1 vote)
30 views

Instant Access to JavaScript Data Structures and Algorithms: An Introduction to Understanding and Implementing Core Data Structure and Algorithm Fundamentals 1st Edition Sammie Bae ebook Full Chapters

Algorithm

Uploaded by

hirterlydin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
30 views

Instant Access to JavaScript Data Structures and Algorithms: An Introduction to Understanding and Implementing Core Data Structure and Algorithm Fundamentals 1st Edition Sammie Bae ebook Full Chapters

Algorithm

Uploaded by

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

Download Full Version ebook - Visit 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

Discover More Ebook - Explore Now at ebookmeta.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

Start reading on any device today!

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

Introduction to Python for Engineers and Scientists: Open


Source Solutions for Numerical Computation 1st Edition
Sandeep Nagar
https://ebookmeta.com/product/introduction-to-python-for-engineers-
and-scientists-open-source-solutions-for-numerical-computation-1st-
edition-sandeep-nagar/
ebookmeta.com
The Networked Health Relevant Factors for Office Buildings
2nd Edition Werner Seiferlein Christine Kohlert

https://ebookmeta.com/product/the-networked-health-relevant-factors-
for-office-buildings-2nd-edition-werner-seiferlein-christine-kohlert/

ebookmeta.com

Painting and Publishing as Cultural Industries: The Fabric


of Creativity in the Dutch Republic, 1580-1800 1st Edition
Claartje Rasterhoff
https://ebookmeta.com/product/painting-and-publishing-as-cultural-
industries-the-fabric-of-creativity-in-the-dutch-
republic-1580-1800-1st-edition-claartje-rasterhoff/
ebookmeta.com

Assigning Responsibility for Children s Health When


Parents and Authorities Disagree Whose Child 1st Edition
Allan J. Jacobs
https://ebookmeta.com/product/assigning-responsibility-for-children-s-
health-when-parents-and-authorities-disagree-whose-child-1st-edition-
allan-j-jacobs/
ebookmeta.com

2023 National Electrical Safety Code 2023rd Edition


Accredited Standards Committee

https://ebookmeta.com/product/2023-national-electrical-safety-
code-2023rd-edition-accredited-standards-committee/

ebookmeta.com

The Language of Political Incorporation Chinese Migrants


in Europe 1st Edition Amy Liu

https://ebookmeta.com/product/the-language-of-political-incorporation-
chinese-migrants-in-europe-1st-edition-amy-liu/

ebookmeta.com
Networking and Kubernetes 1st Edition James Strong &
Vallery Lancey

https://ebookmeta.com/product/networking-and-kubernetes-1st-edition-
james-strong-vallery-lancey/

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 [email protected], 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
About the Technical Reviewer
Phil Nash is a developer evangelist for Twilio, serving
developer communities in London and all over the world.
He is a Ruby, JavaScript, and Swift developer; Google
Developers Expert; blogger; speaker; and occasional brewer.
He can be found hanging out at meetups and conferences,
playing with new technologies and APIs, or writing open
source code.

xvii
Acknowledgments
Thank you, Phil Nash, for the valuable feedback that helped me improve the technical
content of this book with clear explanations and concise code.
Special thanks to the Apress team. This includes James Markham, Nancy Chen, Jade
Scard, and Chris Nelson. Finally, I want to thank Steve Anglin for reaching out to me to
publish with Apress.

xix
Introduction
The motivation for writing this book was the lack of resources available about data
structures and algorithms written in JavaScript. This was strange to me because
today many of the job opportunities for software development require knowledge of
JavaScript; it is the only language that can be used to write the entire stack, including the
front-end, mobile (native and hybrid) platforms, and back-end. It is crucial for JavaScript
developers to understand how data structures work and how to design algorithms to
build applications.
Therefore, this book aims to teach data structure and algorithm concepts from
computer science for JavaScript rather than for the more typical Java or C++. Because
JavaScript follows the prototypal inheritance pattern, unlike Java and C++ (which follow
the inheritance pattern), there are some changes in writing data structures in JavaScript.
The classical inheritance pattern allows inheritance by creating a blueprint-­like form
that objects follow during inheritance. However, the prototypal inheritance pattern
means copying the objects and changing their properties.
This book first covers fundamental mathematics for Big-O analysis and then lays out
the basic JavaScript foundations, such as primitive objects and types. Then, this book
covers implementations and algorithms for fundamental data structures such as linked
lists, stacks, trees, heaps, and graphs. Finally, more advanced topics such as efficient
string search algorithms, caching algorithms, and dynamic programming problems are
explored in great detail.

xxi
CHAPTER 1

Big-O Notation
O(1) is holy.
—Hamid Tizhoosh

Before learning how to implement algorithms, you should understand how to analyze
the effectiveness of them. This chapter will focus on the concept of Big-O notation for
time and algorithmic space complexity analysis. By the end of this chapter, you will
understand how to analyze an implementation of an algorithm with respect to both time
(execution time) and space (memory consumed).

Big-O Notation Primer


The Big-O notation measures the worst-case complexity of an algorithm. In Big-O
notation, n represents the number of inputs. The question asked with Big-O is the
following: “What will happen as n approaches infinity?”
When you implement an algorithm, Big-O notation is important because it tells you
how efficient the algorithm is. Figure 1-1 shows some common Big-O notations.

1
© Sammie Bae 2019
S. Bae, JavaScript Data Structures and Algorithms, https://doi.org/10.1007/978-1-4842-3988-9_1
Chapter 1 Big-O Notation

Figure 1-1. Common Big-O complexities

The following sections illustrate these common time complexities with some simple
examples.

Common Examples
O(1) does not change with respect to input space. Hence, O(1) is referred to as being
constant time. An example of an O(1) algorithm is accessing an item in the array by its
index. O(n) is linear time and applies to algorithms that must do n operations in the
worst-case scenario.
An example of an O(n) algorithm is printing numbers from 0 to n-1, as shown here:

1 function  exampleLinear(n) {
2                   for  (var  i = 0 ; i <  n; i++ ) {

2
Chapter 1 Big-O Notation

3                              console.log(i);
4                   }
5  }

Similarly, O(n2) is quadratic time, and O(n3) is cubic time. Examples of these
complexities are shown here:

1 function  exampleQuadratic(n) {
2                   for  (var  i = 0 ; i <  n; i++ ) {
3                                 console.log(i);
4                                for  (var  j =  i; j <  n; j++ ) {
5                                             console.log(j);
6                              }
7                   }
8  }

1 function  exampleCubic(n) {
2                   for  (var  i = 0 ; i <  n; i++ ) {
3                                  console.log(i);
4                                for  (var  j =  i; j <  n; j++ ) {
5                                              console.log(j);
6                                                  for  (var  k =  j;
j <  n; j++ ) {
7                                                          console.log(k);
8                                                  }
9                                }
10           }
11 }

Finally, an example algorithm of logarithmic time complexity is printing elements


that are a power of 2 between 2 and n. For example, exampleLogarithmic(10) will print
the following:

2,4,8,16,32,64

3
Chapter 1 Big-O Notation

The efficiency of logarithmic time complexities is apparent with large inputs such
as a million items. Although n is a million, exampleLogarithmic will print only 19
items because log2(1,000,000) = 19.9315686. The code that implements this logarithmic
behavior is as follows:

1 function exampleLogarithmic(n) {
2                   for (var i = 2 ; i <= n; i= i*2 ) {
3                          console.log(i);
4                  }
5 }

Rules of Big-O Notation


Let’s represent an algorithm’s complexity as f(n). n represents the number of inputs,
f(n)time represents the time needed, and f(n)space represents the space (additional
memory) needed for the algorithm. The goal of algorithm analysis is to understand the
algorithm’s efficiency by calculating f(n). However, it can be challenging to calculate f(n).
Big-O notation provides some fundamental rules that help developers compute for f(n).

• Coefficient rule: If f(n) is O(g(n)), then kf(n) is O(g(n)), for any


constant k > 0. The first rule is the coefficient rule, which eliminates
coefficients not related to the input size, n. This is because as n
approaches infinity, the other coefficient becomes negligible.

• Sum rule: If f(n) is O(h(n)) and g(n) is O(p(n)), then f(n)+g(n) is


O(h(n)+p(n)). The sum rule simply states that if a resultant time
complexity is a sum of two different time complexities, the resultant
Big-O notation is also the sum of two different Big-O notations.
• Product rule: If f(n) is O(h(n)) and g(n) is O(p(n)), then f(n)g(n) is
O(h(n)p(n)). Similarly, the product rule states that Big-O is multiplied
when the time complexities are multiplied.

• Transitive rule: If f(n) is O(g(n)) and g(n) is O(h(n)), then f(n) is


O(h(n)). The transitive rule is a simple way to state that the same time
complexity has the same Big-O.

4
Chapter 1 Big-O Notation

• Polynomial rule: If f(n) is a polynomial of degree k, then f(n) is


O(nk). Intuitively, the polynomial rule states that polynomial time
complexities have Big-O of the same polynomial degree.

• Log of a power rule: log(nk) is O(log(n)) for any constant k > 0. With
the log of a power rule, constants within a log function are also
ignored in Big-O notation.

Special attention should be paid to the first three rules and the polynomial rule
because they are the most commonly used. I’ll discuss each of those rules in the
following sections.

Coefficient Rule: “Get Rid of Constants”


Let’s first review the coefficient rule. This rule is the easiest rule to understand. It simply
requires you to ignore any non-input-size-related constants. Coefficients in Big-O are
negligible with large input sizes. Therefore, this is the most important rule of Big-O
notations.

If f(n) is O(g(n)), then kf(n) is O(g(n)), for any constant k > 0.

This means that both 5f(n) and f(n) have the same Big-O notation of O(f(n)).
Here is an example of a code block with a time complexity of O(n):

1   function a(n){
2       var count =0;
3       for (var i=0;i<n;i++){
4           count+=1;
5       }
6       return count;
7   }

This block of code has f(n) = n. This is because it adds to count n times. Therefore,
this function is O(n) in time complexity:

1   function a(n){
2       var count =0;
3       for (var i=0;i<5*n;i++){

5
Chapter 1 Big-O Notation

4           count+=1;
5       }
6       return count;
7   }

This block has f(n) = 5n. This is because it runs from 0 to 5n. However, the first two
examples both have a Big-O notation of O(n). Simply put, this is because if n is close to
infinity or another large number, those four additional operations are meaningless. It is
going to perform it n times. Any constants are negligible in Big-O notation.
The following code block demonstrates another function with a linear time
complexity but with an additional operation on line 6:

1   function a(n){
2       var count =0;
3       for (var i=0;i<n;i++){
4           count+=1;
5       }
6       count+=3;
7       return count;
8   }

Lastly, this block of code has f(n) = n+1. There is +1 from the last operation
(count+=3). This still has a Big-O notation of O(n). This is because that 1 operation is not
dependent on the input n. As n approaches infinity, it will become negligible.

Sum Rule: “Add Big-Os Up”


The sum rule is intuitive to understand; time complexities can be added. Imagine a
master algorithm that involves two other algorithms. The Big-O notation of that master
algorithm is simply the sum of the other two Big-O notations.

If f(n) is O(h(n)) and g(n) is O(p(n)), then f(n)+g(n) is O(h(n)+p(n)).

It is important to remember to apply the coefficient rule after applying this rule.

6
Chapter 1 Big-O Notation

The following code block demonstrates a function with two main loops whose time
complexities must be considered independently and then summed:

1   function a(n){
2       var count =0;
3       for (var i=0;i<n;i++){
4           count+=1;
5       }
6       for (var i=0;i<5*n;i++){
7           count+=1;
8       }
9       return count;
10   }

In this example, line 4 has f(n) = n, and line 7 has f(n) = 5n. This results in 6n.
However, when applying the coefficient rule, the final result is O(n) = n.

Product Rule: “Multiply Big-Os”


The product rule simply states how Big-Os can be multiplied.

If f(n) is O(h(n)) and g(n) is O(p(n)), then f(n)g(n) is O(h(n)p(n)).

The following code block demonstrates a function with two nested for loops for
which the product rule is applied:

1   function (n){
2       var count =0;
3       for (var i=0;i<n;i++){
4           count+=1;
5           for (var i=0;i<5*n;i++){
6               count+=1;
7           }
8       }
9       return count;
10   }

7
Chapter 1 Big-O Notation

In this example, f(n) = 5n*n because line 7 runs 5n times for a total of n iterations.
Therefore, this results in a total of 5n2 operations. Applying the coefficient rule, the result
is that O(n)=n2.

Polynomial Rule: “Big-O to the Power of k”


The polynomial rule states that polynomial time complexities have a Big-O notation of
the same polynomial degree.
Mathematically, it’s as follows:

If f(n) is a polynomial of degree k, then f(n) is O(nk).


The following code block has only one for loop with quadratic time complexity:

1   function a(n){
2       var count =0;
3       for (var i=0;i<n*n;i++){
4           count+=1;
5       }
6       return count;
7   }

In this example, f(n) = nˆ2 because line 4 runs n*n iterations.


This was a quick overview of the Big-O notation. There is more to come as you
progress through the book.

Summary
Big-O is important for analyzing and comparing the efficiencies of algorithms.
The analysis of Big-O starts by looking at the code and applying the rules to simplify
the Big-O notation. The following are the most often used rules:

• Eliminating coefficients/constants (coefficient rule)

• Adding up Big-Os (sum rule)


• Multiplying Big-Os (product rule)

• Determining the polynomial of the Big-O notation by looking at loops


(polynomial rule)

8
Chapter 1 Big-O Notation

Exercises
Calculate the time complexities for each of the exercise code snippets.

EXERCISE 1

1   function someFunction(n) {
2
3       for (var i=0;i<n*1000;i++) {
4           for (var j=0;j<n*20;j++) {
5               console.log(i+j);
6           }
7       }
8
9   }

EXERCISE 2

1   function someFunction(n) {
2
3       for (var i=0;i<n;i++) {
4           for (var j=0;j<n;j++) {
5               for (var k=0;k<n;k++) {
6                   for (var l=0;l<10;l++) {
7                       console.log(i+j+k+l);
8                   }
9               }
10           }
11       }
12
13   }

9
Chapter 1 Big-O Notation

EXERCISE 3

1   function someFunction(n) {
2
3       for (var i=0;i<1000;i++) {
4           console.log("hi");
5       }
6
7   }

EXERCISE 4

1   function someFunction(n) {
2
3       for (var i=0;i<n*10;i++) {
4           console.log(n);
5       }
6
7   }

EXERCISE 5

1   function someFunction(n) {
2
3       for (var i=0;i<n;i*2) {
4           console.log(n);
5       }
6
7   }

10
Chapter 1 Big-O Notation

EXERCISE 6

1   function someFunction(n) {
2
3       while (true){
4           console.log(n);
5       }
6   }

Answers
1. O(n2)
There are two nested loops. Ignore the constants in front of n.

2. O(n3)
There are four nested loops, but the last loop runs only until 10.

3. O(1)
Constant complexity. The function runs from 0 to 1000. This does
not depend on n.

4. O(n)
Linear complexity. The function runs from 0 to 10n. Constants are
ignored in Big-O.
5. O(log2n)
Logarithmic complexity. For a given n, this will operate only log2n
times because i is incremented by multiplying by 2 rather than
adding 1 as in the other examples.

6. O(∞)

Infinite loop. This function will not end.

11
CHAPTER 2

JavaScript: Unique Parts


This chapter will briefly discuss some exceptions and cases of JavaScript’s syntax and
behavior. As a dynamic and interpreted programming language, its syntax is different
from that of traditional object-oriented programming languages. These concepts are
fundamental to JavaScript and will help you to develop a better understanding of the
process of designing algorithms in JavaScript.

J avaScript Scope
The scope is what defines the access to JavaScript variables. In JavaScript, variables
can belong to the global scope or to the local scope. Global variables are variables that
belong in the global scope and are accessible from anywhere in the program.

Global Declaration: Global Scope


In JavaScript, variables can be declared without using any operators. Here’s an example:

1  test = "sss";
2  console.log(test); // prints "sss"

However, this creates a global variable, and this is one of the worst practices in
JavaScript. Avoid doing this at all costs. Always use var or let to declare variables.
Finally, when declaring variables that won’t be modified, use const.

Declaration with var: Functional Scope


In JavaScript, var is one keyword used to declare variables. These variable declarations
“float” all the way to the top. This is known as variable hoisting. Variables declared at the
bottom of the script will not be the last thing executed in a JavaScript program during
runtime.
13
© Sammie Bae 2019
S. Bae, JavaScript Data Structures and Algorithms, https://doi.org/10.1007/978-1-4842-3988-9_2
Exploring the Variety of Random
Documents with Different Content
and a question is a perceptible rebuff to their confidingness. The
prevailing feeling appears to be one of affectionate admiration, a
matter of personal attraction rather than of critical esteem. Such a
claim in any man is very far from being negligible. It is clear that the
need of most people is an object of affection. They must love, or they
cannot appreciate. The modern school of novelists, which tries to be
very stern and almost legally unjust, provides little enough material
for the loving hearts. The modern school says to its readers: “You
are wicked, selfish, diseased, but horribly fascinating, and I’m going
to set you right by diagnosis”; and the reader feels a sting in the
fascination. Stevenson says, “We are all mighty fine fellows; and life
is a field of battle; but it is better to be a fool than to be dead; and the
true success is to labour”; and the reader feels that Stevenson is
One of Us! He is not, that is to say, austere; he does not ask
uncomfortable questions; he makes no claim upon his readers’
judgment, but only upon their self-esteem and their gratified assent.
He even tells them about himself. He says, “I knew a little boy”; and
his readers say: “It’s himself!” They read with enormous satisfaction.
Well, all that is delightful; but in its way it is a red-herring. It does not
help us to assay the literary value of Stevenson’s work. It is simply a
wide illustration of the fascination which Stevenson had for his
friends. It is an extension of that rare thing, personal charm. We may
say that it ought not to influence readers; and no doubt it influences
some too-critical readers adversely (criticism being understood by all
admirers of Stevenson as the merest corrosion); but the fact is that it
cannot be ignored by anyone who seeks to account for Stevenson’s
continued, and even now barely declining, popularity. Another very
good reason is that Stevenson had extraordinarily good friends. I
think it probable that no writer ever had friends more loyal and
affectionate. They criticised his work privately to its great
improvement, and then sold his work when it was completed, acting
as counsellors and agents. And this was done with the same
affectionate admiration which readers of his work still feel. He had
few intimate friends, says Mrs. Stevenson: if friendship consisted in
affection received (as distinguished from affection exchanged), I
think Stevenson would have been in friends the richest man of his
own generation. And since his death he has found a hundred
thousand friends for every one he had during his lifetime. No man
was ever richer in well-wishers. If he had few intimate friends that
was because he was naturally reserved, or, as Mrs. Strong says,
“secretive.” No doubt it was a part of his charm that his friends were
mystified by his reserve: I do not see why his readers also should be
mystified, for his writing is free of any mystery. I can only assume
that a slight air of sentimentalism which runs through essays and
romances alike, and over into such short stories as Will o’ the Mill
and Markheim, combines with the thin optimism of the essays and
the picturesque variety of incident of the romances to give body to
this charm. I have stated in an earlier chapter the features of the
romances which seem to me to be merits: it is not necessary to
repeat the merits here. They include occasional pieces of
distinguished imagination, a frequent exuberance of fancy, and a
great freshness of incident which conceals lack of central or unifying
idea and poverty of imagined character. Intrinsically, although their
literary quality is much higher, the romances—with the possible
exception of Kidnapped—are inferior to the work of Captain Marryat.

V
Finally, the fact which all must recognise in connection with
Stevenson’s work is the versatility of talent which is displayed. From
essays personal to essays critical; from short-stories picturesque to
short-stories metaphysical, and stories of bogles to fairy stories of
princes and magic bottles and wondrous enchanted isles; from tales
of treasure to the politics of a principality, from Scottish history to
tales of the South Seas; from travel-books to poems for men and
children; from the thermal influences of forests to a defence of a
Roman Catholic hero-priest; from Samoan politics to the story of the
Justice Clerk; from plays to topographical history and imaginary war-
news and the cutting of wood-blocks (to the satisfaction of Mr.
Joseph Pennell)—that is a dazzling record. Quite obviously one
cannot contemplate it without great admiration. When it is
remembered also that it is the product of a man who was very
frequently (though not, as is generally supposed, continuously) an
invalid, the amount of it, and the variety, seems to be impossible. Yet
it is possible, and this fact it is which finally explains our attitude to
Stevenson. We think it marvellous that he should have been able to
write at all, forgetting, as we do, that “writing his best was very life to
him.” We do forget that; we ought not to forget it. We ought not to
forget that Stevenson was a writer. He meant to be a writer, and a
writer he became. He is known chiefly in these days as a writer; and
in the future he will be still more clearly seen as a writer. The
weaknesses of his work will be realised; to some extent his writing
will fall in popular esteem; but he will be less the brave soul travelling
hopefully and labouring to arrive, and more the deliberate writer.
When other men sing and walk and talk and play chess and loiter,
Stevenson wrote. In his life there is no question that he sang and
walked and loitered and talked and played chess; but when he could
do none of these things he could write. Writing was as the breath of
his body; writing was his health, his friends, his romance. He will go
down into literary history as the man who became a professional
writer, who cared greatly about the form and forms of expression.
The fact that he concentrated upon expression left his mind to some
extent undeveloped, so that he could express very excellently
perceptions more suitable to his youth than to his maturer years. It
made his earlier writing too scented and velvet-coated. But it
enabled him, when his feeling was aroused, as it only could have
been in the last years of his life, to write at great speed, with great
clearness, an account of the political troubles in Samoa and in
particular of German diplomacy there, which seems to us still
valuable—not because the facts it records are of extreme
significance, but because at the end of his life Stevenson was at last
to be found basing his work upon principles, really and consciously
grasped, from which the incidental outcome was of less importance
than the main realisation. Where he had hitherto been shuttlecocked
by his impulses, and tethered by his moralism, he became capable
of appreciating ideas as of more importance than their expression. If
he had been less prolific, less versatile, less of a virtuoso, Stevenson
might have been a greater man. He would have been less popular.
He would have been less generally admired and loved. But with all
his writing he took the road of least resistance, the road of limited
horizons; because with all his desire for romance, his desire for the
splendour of the great life of action, he was by physical delicacy
made intellectually timid and spiritually cautious. He was obliged to
take care of himself, to be home at night, to allow himself to be
looked after. Was not that the greatest misfortune that could have
befallen him? Is the work that is produced by nervous reaction from
prudence ever likely to enjoy an air of real vitality? In the versatility of
Stevenson we may observe his restlessness, the nervous fluttering
of the mind which has no physical health to nourish it. In that, at
least, and the charming and not at all objectionable inclination to
pose. He was a poseur because if he had not pretended he would
have died. It was absolutely essential to him that he should pose and
that he should write, just as it was essential that he should be
flattered and anxiously guarded from chill and harm. But it was
necessary for the same reason, lest the feeble flame should perish
and the eager flicker of nervous exuberance be extinguished. That
Stevenson was deliberately brave in being cheerful and fanciful I do
not for one moment believe; I think such a notion is the result of pure
ignorance of nervous persons and their manifestations. But that
Stevenson, beneath all his vanity, realised his own disabilities,
seems to me to be certain and pathetic. That is what makes so much
of the extravagant nonsense written and thought about Stevenson
since his death as horrible to contemplate as would be any dance of
ghouls. The authors of all this posthumous gloating over Stevenson’s
illnesses have been concerned to make him a horribly piteous figure,
to harrow us in order that we should pity. How much more is
Stevenson to be pitied for his self-constituted apostles! We shall do
ill to pity Stevenson, because pity is the obverse of envy, and is as
much a vice. Let us rather praise Stevenson for his real
determination and for that work of his which we can approve as well
as love. To love uncritically is to love ill. To discriminate with mercy is
very humbly to justify one’s privilege as a reader.

VI
It is sufficient here to maintain that Stevenson’s literary reputation, as
distinct from the humanitarian aspect of his fortitude, is seriously
impaired. It is no longer possible for a serious critic to place him
among the great writers, because in no department of letters—
excepting the boy’s book and the short story—has he written work of
first-class importance. His plays, his poems, his essays, his
romances—all are seen nowadays to be consumptive. What remains
to us, apart from a fragment, a handful of tales, and two boy’s books
(for Kidnapped, although finely romantic, was addressed to boys,
and still appeals to the boy in us) is a series of fine scenes—what I
have called “plums”—and the charm of Stevenson’s personality.
Charm as an adjunct is very well; charm as an asset is of less
significance. We find that Stevenson, reviving the never-very-
prosperous romance of England, created a school which has brought
romance to be the sweepings of an old costume-chest. I am afraid
we must admit that Stevenson has become admittedly a writer of the
second class, because his ideals have been superseded by other
ideals and shown to be the ideals of a day, a season, and not the
ideals of an age. In fact, we may even question whether his ideals
were those of a day, whether they were not merely treated by
everybody as so much pastime; whether the revival of the pernicious
notion that literature is only a pastime is not due to his influence. We
may question whether Stevenson did not make the novel a toy when
George Eliot had finished making it a treatise. If that charge could be
upheld, I am afraid we should have another deluge of critical articles
upon Stevenson, written as blindly as the old deluge, but this time
denouncing him as a positive hindrance in the way of the novel’s
progress. However that may be, Stevenson seems very decidedly to
have betrayed the romantics by inducing them to enter a cul-de-sac;
for romantic literature in England at the present time seems to show
no inner light, but only a suspicious phosphorescence. And that fact
we may quite clearly trace back to Stevenson, who galvanised
romance into life after Charles Reade had volubly betrayed it to the
over-zealous compositor.
Stevenson, that is to say, was not an innovator. We can find his
originals in Wilkie Collins, in Scott, in Mayne Reid, in Montaigne,
Hazlitt, Defoe, Sterne, and in many others. No need for him to admit
it: the fact is patent. “It is the grown people who make the nursery
stories; all the children do, is jealously to preserve the text.” That is
what Stevenson was doing; that is what Stevenson’s imitators have
been doing ever since. And if romance rests upon no better base
than this, if romance is to be conventional in a double sense, if it
spring not from a personal vision of life, but is only a tedious
virtuosity, a pretence, a conscious toy, romance as an art is dead.
The art was jaded when Reade finished his vociferous carpet-
beating; but it was not dead. And if it is dead, Stevenson killed it.
BIBLIOGRAPHY
(The dates within brackets are those of composition or of first
periodical publication.)
The Pentland Rising, 1866.
A New Form of Intermittent Light, 1871.
The Thermal Influence of Forests, 1873.
An Appeal to the Clergy of the Church of Scotland, 1875.
An Inland Voyage, 1878.
Edinburgh: Picturesque Notes, 1879.
Travels with a Donkey, 1879.
Virginibus Puerisque, 1881.
Virginibus Puerisque: four parts (1876-1879);
Crabbed Age and Youth (1878); An Apology for
Idlers (1877); Ordered South (1874); Æs Triplex
(1878); El Dorado (1878); The English Admirals
(1878); Some Portraits by Raeburn; Child’s Play
(1878); Walking Tours (1876); Pan’s Pipes (1878); A
Plea for Gas Lamps (1878).
Familiar Studies of Men and Books, 1882.
Victor Hugo’s Romances (1874); Some Aspects of
Robert Burns (1879); Walt Whitman (1878); Henry
David Thoreau (1880); Yoshida Torajiro (1880);
François Villon (1877); Charles of Orleans (1876);
Samuel Pepys (1881); John Knox, and his Relations
to Women (1875).
New Arabian Nights, 1882.
The Suicide Club and the Rajah’s Diamond (1878);
The Pavilion on the Links (1880); A Lodging for the
Night (1877); The Sire de Malétroit’s Door (1878);
Providence and the Guitar (1878).
The Silverado Squatters, 1883.
Treasure Island, 1883.
Prince Otto, 1885.
A Child’s Garden of Verses, 1885.
The Dynamiter, 1885.
Dr. Jekyll and Mr. Hyde, 1886.
Kidnapped, 1886.
The Merry Men, 1887.
The Merry Men (1882); Will o’ the Mill (1878);
Markheim (1885); Thrawn Janet (1881); Olalla
(1885); The Treasure of Franchard (1883).
Memories and Portraits, 1887.
The Foreigner at Home (1882); Some College
Memories (1886); Old Mortality (1884); A College
Magazine; An Old Scotch Gardener (1871); Pastoral
(1887); The Manse (1887); Memoirs of an Islet;
Thomas Stevenson (1887); Talk and Talkers (1882);
The Character of Dogs (1883); “A Penny Plain and
Twopence Coloured” (1884); A Gossip on A Novel of
Dumas’s; A Gossip on Romance (1882); A Humble
Remonstrance (1884).
Underwoods, 1887.
Memoir of Fleeming Jenkin (in “Papers Literary, Scientific,
etc.,” by Fleeming Jenkin), 1887.
The Black Arrow, 1888.
The Master of Ballantrae, 1889.
The Wrong Box, 1889.
Father Damien, 1890.
Ballads, 1890.
Across the Plains, 1892.
Across the Plains (1883); The Old Pacific Capital
(1880); Fontainebleau (1884); Epilogue to “An Inland
Voyage” (1888); The Coast of Fife (1888); The
Education of an Engineer (1888); The Lantern-
Bearers (1888); A Chapter on Dreams (1888);
Beggars (1888); Letter to a Young Gentleman
(1888); Pulvis et Umbra (1888); A Christmas
Sermon (1888).
The Wrecker, 1892.
A Footnote to History, 1892.
Three Plays, 1892.
Deacon Brodie (1880); Beau Austin (1884); Admiral
Guinea (1884).
Island Nights Entertainments, 1893.
The Beach of Falesá (1892); The Bottle Imp (1891);
The Isle of Voices (1893).
Catriona, 1893.
The Ebb Tide, 1894.
Vailima Letters, 1895.
[Dr. Jekyll and Mr. Hyde and] Fables, 1896.
Weir of Hermiston, 1896.
Songs of Travel, 1896.
A Mountain Town in France, 1896.
Four Plays, 1896.
Deacon Brodie; Beau Austin; Admiral Guinea;
Macaire (1885).
St. Ives, 1898.
Letters to His Family and Friends, 1899.
In the South Seas, 1900.
The Pocket R. L. S. (containing “Prayers”), 1902.
Essays in the Art of Writing, 1905.
On Some Technical Elements of Style in Literature
(1885); The Morality of the Profession of Letters
(1881); Books which have Influenced Me (1887); A
Note on Realism (1883); My First Book: Treasure
Island (1894); The Genesis of “The Master of
Ballantrae” (1890); Preface to “The Master of
Ballantrae” (1889).
Tales and Fantasies, 1905.
The Misadventures of John Nicholson (1887); The
Body-Snatcher (1884); The Story of a Lie (1879).
Essays of Travel, 1905.
The Amateur Emigrant (1879); Cockermouth and
Keswick (1871); An Autumn Effect (1875); A
Winter’s Walk (1876); Forest Notes (1875-6); A
Mountain Town in France (1879); Rosa quo Locorum
(1890); The Ideal House; Davos in Winter (1881);
Health and Mountains (1881); Alpine Diversions
(1881); The Stimulation of the Alps (1881); Roads
(1873); On the Enjoyment of Unpleasant Places
(1874).
Poems, 1906.
Underwoods; Ballads; Songs of Travel.
Lay Morals and Other Papers, 1911.
Lay Morals (1879); Father Damien (1890); The
Pentland Rising (1866); The Day after To-morrow
(1887); [College Papers] Edinburgh Students in
1824 (1871); The Modern Student considered
generally (1871); Debating Societies (1871); The
Philosophy of Umbrellas (1871); The Philosophy of
Nomenclature (1871); [Criticisms] Lord Lytton’s
“Fables in Song” (1874); Salvini’s Macbeth (1876);
Bagster’s “Pilgrim’s Progress” (1882); [Sketches]
The Satirist (? 1870); Nuits Blanches (? 1870); The
Wreath of Immortelles (? 1870); Nurses (1870); A
Character (? 1870); The Great North Road (1895);
The Young Chevalier (1892); Heathercat (1894).
Records of a Family of Engineers, 1912.
Poems, 1918.
Underwoods; Ballads; Songs of Travel; A Child’s
Garden.
The Edinburgh Edition of the Works. 27 vols. 1894-97.
The Pentland Edition ” 20 vols. 1906-07.
The Swanston Edition ” 25 vols. 1911-12.
WILLIAM BRENDON AND SON, LTD.
PRINTERS, PLYMOUTH
MARTIN SECKER’S
COMPLETE CATALOGUE OF
BOOKS PUBLISHED BY HIM AT
NUMBER FIVE JOHN STREET
ADELPHI LONDON
AUTUMN
MCMXIV
The Books in this list should be obtainable from all
Booksellers and Libraries, and if any difficulty is
experienced the Publisher will be glad to be informed of
the fact. He will also be glad if those interested in
receiving from time to time Announcement Lists,
Prospectuses, etc., of new and forthcoming books from
Number Five John Street, will send their names and
addresses to him for this purpose. Any book in this list
may be obtained on approval through the booksellers, or
direct from the Publisher, on remitting him the published
price, plus the postage.
Telephone City 4779
Telegraphic Address:
Psophidian London
PART I
INDEX OF AUTHORS
ABERCROMBIE, LASCELLES
Speculative Dialogues. Wide Crown 8vo. 5s. net.
Thomas Hardy: A Critical Study. Demy 8vo. 7s.
6d. net.
The Epic (The Art and Craft of Letters). F’cap 8vo.
1s. net.
AFLALO, F. G.
Behind the Ranges. Wide Demy 8vo. 10s. 6d. net.
Regilding the Crescent. Demy 8vo. 10s. 6d. net.
Birds in the Calendar. Crown 8vo. 3s. 6d. net.
ALLSHORN, LIONEL
Stupor Mundi. Medium Octavo. 16s. net.
APPERSON, G. L.
The Social History of Smoking. Post 8vo. 6s.
net.
ARMSTRONG, DONALD
The Marriage of Quixote. Crown 8vo. 6s.
BARRINGTON, MICHAEL
Grahame of Claverhouse. Imperial 8vo. 30s. net.
Edition de Luxe 63s. net.
BENNETT, ARNOLD
Those United States. Post 8vo. 5s. net.
BLACK, CLEMENTINA
The Linleys of Bath. Medium 8vo. 16s. net.
The Cumberland Letters. Medium 8vo. 16s. net.
BOULGER, D. C.
The Battle of the Boyne. Med. 8vo. 21s. net.
The Irish Exiles at St. Germains. Med. 8vo. 21s.
net.
BOTTOME, PHYLLIS
The Common Chord. Crown 8vo. 6s.
BURROW, C. KENNETT
Carmina Varia. F’cap 8vo. 2s. 6d. net.
CALDERON, GEORGE (With St. John Hankin)
Thompson: A Comedy. Sq. Cr. 8vo. 2s. net.
CANNAN, GILBERT
Round the Corner. Crown 8vo. 6s.
Old Mole. Crown 8vo. 6s.
Samuel Butler: A Critical Study. Demy 8vo. 7s.
6d. net.
Satire (The Art and Craft of Letters). F’cap 8vo. 1s.
net.
CHESTERTON, G. K.
Magic: A Fantastic Comedy. Sq. Cr. 8vo. 2s. net.
CLAYTON, JOSEPH
The Underman. Crown 8vo. 6s.
Leaders of the People. Demy 8vo. 12s, 6d. net.
Robert Kett and the Norfolk Rising. Demy 8vo.
8s. 6d. net.
COKE, DESMOND
The Art of Silhouette. Demy 8vo. 10s. 6d. net.
CRAVEN, A. SCOTT
The Fool’s Tragedy. F’cap 8vo. 6s.
DE SÉLINCOURT, BASIL
Walt Whitman: A Critical Study. Demy 8vo. 7s.
6d. net.
DRINKWATER, JOHN
William Morris: A Critical Study. Demy 8vo. 7s.
6d. net.
D. G. Rossetti: A Critical Study. Demy 8vo. 7s.
6d. net.
The Lyric (The Art and Craft of Letters). F’cap 8vo.
1s. net.
DOUGLAS, NORMAN
Fountains in the Sand. Wide Demy 8vo. 7s. 6d.
net.
Old Calabria. Demy 8vo. 10s. 6d. net.
DOUGLAS, THEO
White Webs. Crown 8vo. 6s.
FEA, ALLAN
Old English Houses. Demy 8vo. 10s. 6d. net.
Nooks and Corners of Old England. Small
Crown 8vo. 5s. net.
The Real Captain Cleveland. Demy 8vo. 8s. 6d.
net.
FRANCIS, RENÉ
Egyptian Æsthetics. Wide Demy 8vo. 7s. 6d. net.
FREEMAN, A. M.
Thomas Love Peacock: A Critical Study. Demy
8vo. 7s. 6d. net.
GRETTON, R. H.
History (The Art and Craft of Letters). F’cap 8vo.
1s. net.
HANKIN, ST. JOHN
The Dramatic Works, with an Introduction by John
Drinkwater. Small 4to. Definitive Limited Edition in
Three Volumes. 25s. net.
The Return of the Prodigal. Sq. Cr. 8vo. 2s. net.
The Cassilis Engagement. Sq. Cr. 8vo. 2s. net.
The Charity that Began at Home. Sq. Cr. 8vo. 2s.
net.
The Constant Lover, etc. Sq. Cr. 8vo. 2s. net.
HAUPTMANN, GERHART
The Complete Dramatic Works. 6 vols. Crown
8vo. 5s. net per volume.
HEWLETT, WILLIAM
Telling the Truth. Crown 8vo. 6s.
Uncle’s Advice: A Novel in Letters. Cr. 8vo. 6s.
HORSNELL, HORACE
The Bankrupt. Crown 8vo. 6s.
HOWE, P.P.
The Repertory Theatre. Cr. 8vo. 2s. 6d. net.
Dramatic Portraits. Crown 8vo. 5s. net.
Bernard Shaw: A Critical Study. Demy 8vo. 7s.
6d. net.
J. M. Synge: A Critical Study. Demy 8vo. 7s. 6d.
net.
Criticism (The Art and Craft of Letters). F’cap 8vo.
1s. net.
HUEFFER, FORD MADOX
Henry James: A Critical Study. Demy 8vo. 7s. 6d.
net.
IBSEN, HENRIK
Peer Gynt. A New Translation by R. Ellis Roberts.
Wide Crown 8vo. 5s. net.
JACOB, HAROLD
Perfumes of Araby. Wide Demy 8vo. 7s. 6d. net.
LAMONT, L. M.
A Coronal: An Anthology. F’cap 8vo. 2s. 6d. net.
Thomas Armstrong, C.B.: A Memoir. Demy 8vo.
10s. 6d. net.
LLUELLYN, RICHARD
The Imperfect Branch. Crown 8vo. 6s.
LOW, IVY
The Questing Beast. Crown 8vo. 6s.
MACHEN, ARTHUR
Hieroglyphics: A Note upon Ecstasy in
Literature. F’cap 8vo. 2s. 6d. net.
MACKENZIE, COMPTON
Carnival. Crown 8vo. 6s. and 1s. net.
Sinister Street. I. Crown 8vo. 6s.
Sinister Street. II. Crown 8vo. 6s.
The Passionate Elopement. Crown 8vo. 6s. and
2s. net.
Poems. Crown 8vo. 5s. net.
Kensington Rhymes. Crown 4to. 5s. net.
MAKOWER, S. V.
The Outward Appearance. Crown 8vo. 6s.
MAVROGORDATO, JOHN
Letters from Greece. F’cap 8vo. 2s. net.
MELVILLE, LEWIS
Some Eccentrics and a Woman. Demy 8vo. 10s.
6d. net.
METHLEY, VIOLET
Camille Desmoulins: A Biography. Demy 8vo. 15s.
net.
MEYNELL, VIOLA
Lot Barrow. Crown 8vo. 6s.
Modern Lovers. Crown 8vo. 6s.
NIVEN, FREDERICK
A Wilderness of Monkeys. Crown 8vo. 6s.
Above Your Heads. Crown 8vo. 6s.
Dead Men’s Bells. Crown 8vo. 6s.
The Porcelain Lady. Crown 8vo. 6s.
Hands Up! Crown 8vo. 6s.
NORTH, LAURENCE
Impatient Griselda. Crown 8vo. 6s.
The Golightlys: Father and Son. Cr. 8vo. 6s.
ONIONS, OLIVER

You might also like