Book Image

Learning JavaScript Data Structures and Algorithms

By : Loiane Avancini
Book Image

Learning JavaScript Data Structures and Algorithms

By: Loiane Avancini

Overview of this book

Table of Contents (18 chapters)

Preface

JavaScript is currently the most popular programming language. It is known as "the Internet language" due to the fact that Internet browsers understand JavaScript natively, without installing any plugins. JavaScript has grown so much that it is no longer just a frontend language; it is now also present on the server (Node.js) and database as well (MongoDB).

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

Algorithms play a major role in the art of Computer Science. There are so many ways of solving the same problem, and some approaches are better than others. That is why it is also very important to know the most famous algorithms.

Happy coding!

What this book covers

Chapter 1, JavaScript – A Quick Overview, covers the basics of JavaScript you need to know prior to learning about data structures and algorithms. It also covers setting up the development environment needed for this book.

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

Chapter 3, Stacks, introduces the stack data structure, demonstrating how to create a stack and how to add and remove elements. It also demonstrates how to use stacks to solve some Computer Science problems.

Chapter 4, Queues, covers the queue data structure, demonstrating how to create a queue and add and remove elements. It also demonstrates how to use queues to solve some Computer Science problems and the major differences between queues and stacks.

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

Chapter 6, 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 7, 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 hashes and techniques to create better hash functions.

Chapter 8, Trees, covers the tree data structure and its terminologies, focusing on binary search tree data as well as the methods trees use to search, traverse, add, and remove nodes. It also introduces the next steps you can take to delve deeper into the world of trees, covering what tree algorithms should be learned next.

Chapter 9, Graphs, introduces the amazing world of the graphs data structure and its application in real-world problems. This chapter covers the most common graph terminologies, the different ways of representing a graph, how to traverse graphs using the breadth-first search and depth-first search algorithms, and its applications.

Chapter 10, Sorting and Searching Algorithms, explores the most used sorting algorithms such as bubble sort (and its improved version), selection sort, insertion sort, merge sort, and quick sort. It also covers searching algorithms such as sequential and binary search.

Chapter 11, More About Algorithms, introduces some algorithm techniques and the famous big-O notation. It covers the recursion concept and some advanced algorithm techniques such as dynamic programming and greedy algorithms. This chapter introduces big-O notation and its concepts. Finally, it explains how to take your algorithm knowledge to the next level. This is an online chapter available on the Packt Publishing website. You can download it from https://www.packtpub.com/sites/default/files/downloads/4874OS_Chapter11_More_About_Algorithms.pdf.

Appendix, Big-O Cheat Sheet, lists the complexities of the algorithms (using big-O notation) implemented in this book. This is also an online chapter, which can be downloaded from https://www.packtpub.com/sites/default/files/downloads/4874OS_Appendix_Big_O_Cheat_Sheet.pdf.

What you need for this book

You can set up three different development environments for this book. You do not need to have all three environments; you can select one or give all of them a try!

  • For the first option, you need a browser. It is recommended to use one of the following browsers:

  • For the second option, you will need:

    • One of the browsers listed in the first option

    • A web server; if you do not have any web server installed in your computer, you can install XAMPP (https://www.apachefriends.org)

  • The third option is an environment 100 percent JavaScript! For this, you will need the following elements:

    • One of the browsers listed in the first option

    • Node.js (http://nodejs.org/)

    • After installing Node.js, install http-server (package) as follows:

      npm install http-server –g
      

You can find more detailed instructions in Chapter 1, JavaScript – A Quick Overview, as well.

Who this book is for

This book is intended for students of Computer Science, people who are just starting their career in technology, and those who want to learn about data structures and algorithms with JavaScript. Some knowledge of programming logic is the only thing you need to know to start having fun with algorithms and JavaScript!

This book is written for beginners who want to learn about data structures and algorithms, and also for those who are already familiar with data structures and algorithms but who want to learn how to use them with JavaScript.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Inside the script tag, we have the JavaScript code."

A block of code is set as follows:

console.log("num: "+ num);
console.log("name: "+ name);
console.log("trueValue: "+ trueValue);
console.log("price: "+ price);
console.log("nullVar: "+ nullVar);
console.log("und: "+ und);

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

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
</head>
<body>
    <script>
        alert('Hello, World!');
    </script>
</body>
</html>

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

npm install http-server –g

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "The number of Node Packages Modules (https://www.npmjs.org/) also grows exponentially."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

For this book, the code files can be downloaded or forked from the following GitHub repository as well: https://github.com/loiane/javascript-datastructures-algorithms.

Downloading the color images of this book

We also provide you a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/4874OS_ColoredImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.