Book Image

Learning JavaScript Data Structures and Algorithms - Third Edition

Book Image

Learning JavaScript Data Structures and Algorithms - Third Edition

Overview of this book

A data structure is a particular way of organizing data in a computer to utilize resources efficiently. Data structures and algorithms are the base of every solution to any programming problem. With this book, you will learn to write complex and powerful code using the latest ES 2017 features. Learning JavaScript Data Structures and Algorithms begins by covering the basics of JavaScript and introduces you to ECMAScript 2017, before gradually moving on to the most important data structures such as arrays, queues, stacks, and linked lists. You will gain in-depth knowledge of how hash tables and set data structures function as well as how trees and hash maps can be used to search files in an HD or represent a database. This book serves as a route to take you deeper into JavaScript. You’ll also get a greater understanding of why and how graphs, one of the most complex data structures, are largely used in GPS navigation systems in social networks. Toward the end of the book, you’ll discover how all the theories presented in this book can be applied to solve real-world problems while working on your own computer networks and Facebook searches.
Table of Contents (22 chapters)
Title Page
Dedication
Packt Upsell
Contributors
Preface
Index

Preface

JavaScript is one of the most popular programming language nowadays. It is known as the internet language due the fact that the browser understands JavaScript natively, without installing any plugins in it. JavaScript has grown so much that is no longer just a frontend language; it is also present now on the server (NodeJS), database (MongoDB), and mobile devices and is also used in embedded and Internet of Things (IoT) devices.

Learning data structures is very important for any technology professional. Working as a developer means you are able to solve problems with the help of programming languages, and data structures are an indispensable piece of the solutions we need to create to solve these problems. Choosing a wrong data structure can also have an impact on the performance of the program we are writing. That is why, it's important to get to know different data structures and how to apply them properly.

Algorithms are the state of art of computer science. There are so many ways of solving the same problem, and some approaches are better than the others. That is why, it's also very important to know the most famous algorithms. This book was written for beginners who want to learn data structures and algorithms and also for those who are already familiar with data structures and algorithms, but want to learn it using JavaScript.

Happy coding!

Who this book is for

If you are a student of computer science or are at the start of your technology career and want to explore JavaScript’s optimum ability, this book is for you. If you are already familiar with programming, but want to hone your skills on algorithms and data structures, this book is also for you.

You just need a basic knowledge of JavaScript and programming logic to start having fun with algorithms.

What this book covers

Chapter 1, JavaScript - A Quick Overview, covers the basics of JavaScript needed prior to learning data structures and algorithms. It also covers the setup of the development environment needed for this book.

Chapter 2, ECMAScript and TypeScript Overview, covers some new JavaScript functionalities introduced since 2015 and also covers the basic functionalities of TypeScript, a JavaScript superset.

Chapter 3, Arrays, explains how to use the most basic and most used data structure, which are the arrays. This chapter demonstrates how to declare, initialize, add, and remove elements from an array. It also covers how to use native JavaScript Array methods.

Chapter 4, Stacks, introduces the stack data structure, demonstrating how to create a stack and add and remove elements. It also demonstrates how to use stack to solve some computer science-related problems.

Chapter 5, Queues and Deques, covers the queue data structure, demonstrating how to create a queue and add and remove its elements. It covers the deque data structure, a special type of the queue. It also demonstrates how to use queue to solve some computer science-related problems and the major differences between queues and stacks.

Chapter 6, Linked Lists, explains how to create the linked list data structure from scratch using objects and "pointer" concept. Besides covering how to declare, create, add, and remove elements, it also covers the various types of linked lists, such as the doubly linked list and circular linked list.

Chapter 7, Sets, introduces the set data structure and how it can be used to store non-repeated elements. It also explains the different types of set operations and how to implement and use them.

Chapter 8, Dictionaries and Hashes, explains the dictionary and hash data structures and the differences between them. This chapter covers how to declare, create, and use both data structures. It also explains how to handle collisions in hash and techniques for creating better hash functions.

Chapter 9, Recursion, introduces the concept of recursion and demonstrates the differences between declarative and recursive algorithms.

Chapter 10, Trees, covers the tree data structure, its terminology, focusing on Binary Search Tree data— its methods to search, traverse, add, and remove nodes. It also introduces self-balancing trees, such as the AVL and Red-Black trees.

Chapter 11, Binary Heap and Heap Sort, covers the min heap and max heap data structures, how to use the heap as a priority queue, and discusses the famous heap sort algorithm.

Chapter 12, Graphs, introduces the amazing world of graphs and its application in real-world problems. This chapter covers the most common graph terminology, the different way of representing a graph, how to traverse graphs using the Breadth-First Search and Depth-First Search algorithms and its applications.

Chapter 13, Sorting and Searching Algorithms, explores the most used sorting algorithms, such as the Bubble sort (and its improved version), Selection sort, Insertion sort, Merge sort, and Quick sort. It also covers the counting and radix sort, two distributed sorting algorithms. It also covers how to search algorithms, such as the sequential and binary search, and how to shuffle arrays.

Chapter 14, Algorithm Designs and Techniques, introduces some algorithm techniques and some of the most famous algorithms. It also covers an introduction to functional programming in JavaScript.

Chapter 15, Algorithm Complexity, introduces the Big-O notation and its concepts along with a cheat sheet of the complexity of the algorithms implemented in this book. It covers an introduction to NP-Completeness problems and heuristic solutions. At last, it explains how to take your algorithm knowledge to the next level.

 

To get the most out of this book

Although this book provides a brief introduction on JavaScript in its first chapter, you will need a basic knowledge of JavaScript and programming logic.

To test the code examples provided by this book, you will need a code editor (such as Atom or Visual Studio Code) so that you can read the code and also a browser (Chrome, Firefox, or Edge).

You can also test the examples online by accessing https://javascript-ds-algorithms-book.firebaseapp.com/. Also, remember to open the developer tools of the browser so that you can see what is being outputted in the browser's console.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Learning-JavaScript-Data-Structures-and-Algorithms-Third-Edition. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system."

A block of code is set as follows:

class Stack {
  constructor() {
    this.items = []; // {1}
  }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

const stack = new Stack();
console.log(stack.isEmpty()); // outputs true

Any command-line input or output is written as follows:

npm install http-server -g

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select System info from the Administration panel."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.