Hands-On Data Structures and Algorithms with JavaScript
eBook - ePub

Hands-On Data Structures and Algorithms with JavaScript

Kashyap Mukkamala

Share book
  1. 332 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Hands-On Data Structures and Algorithms with JavaScript

Kashyap Mukkamala

Book details
Book preview
Table of contents
Citations

About This Book

Increase your productivity by implementing complex data structures and algorithms using JavaScriptAbout This Bookā€¢ A step by step guide, which will provide you with a thorough discussion on the analysis and design of fundamental JavaScript data structuresā€¢ Get a better understanding of advanced concepts such as space and time complexity to optimize your codeā€¢ Focus more on solving the business problem and less on the technical challenges involvedWho This Book Is ForIf you are a JavaScript developer looking for practical examples to implement data structures and algorithms in your web applications, then this book is for you. Familiarity with data structures and algorithms will be helpful to get the most out of this book.What You Will Learnā€¢ Build custom Back buttons embedded within your applicationā€¢ Build part of a basic JavaScript syntax parser and evaluator for an online IDEā€¢ Build a custom activity user tracker for your applicationā€¢ Generate accurate recommendations for credit card approval using Decision Treesā€¢ Simplify complex problems using a graphsā€¢ Increase the performance of an application using micro-optimizationsIn DetailData structures and algorithms are the fundamental building blocks of computer programming. They are critical to any problem, provide a complete solution, and act like reusable code. Using appropriate data structures and having a good understanding of algorithm analysis are key in JavaScript to solving crises and ensuring your application is less prone to errors.Do you want to build applications that are high-performing and fast? Are you looking for complete solutions to implement complex data structures and algorithms in a practical way? If either of these questions rings a bell, then this book is for you!You'll start by building stacks and understanding performance and memory implications. You will learn how to pick the right type of queue for the application. You will then use sets, maps, trees, and graphs to simplify complex applications. You will learn to implement different types of sorting algorithm before gradually calculating and analyzing space and time complexity. Finally, you'll increase the performance of your application using micro optimizations and memory management.By the end of the book you will have gained the skills and expertise necessary to create and employ various data structures in a way that is demanded by your project or use case.Style and approachStep-by-step topics will help you get started with solutions for implementing classic data structures and algorithms. Learn by doing with hands-on code snippets that give you practical experience of the subject.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on ā€œCancel Subscriptionā€ - itā€™s as simple as that. After you cancel, your membership will stay active for the remainder of the time youā€™ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlegoā€™s features. The only differences are the price and subscription period: With the annual plan youā€™ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, weā€™ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Hands-On Data Structures and Algorithms with JavaScript an online PDF/ePUB?
Yes, you can access Hands-On Data Structures and Algorithms with JavaScript by Kashyap Mukkamala in PDF and/or ePUB format, as well as other popular books in Computer Science & Web Development. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788397544
Edition
1

Exploring Types of Algorithms

An algorithm in the computer science world is a set of instructions that takes finite space and time to execute. It starts at an initial state of the application and then performs a set of instructions step by step to achieve the end result.
Algorithms come in all shape and sizes, and all of them will fit the bill when you compare it with the overly generic definition of what an algorithm is. The big question is to decide which algorithm to use in which case and to make modifications to enhance its functionality based on the application's needs.
As I have shown in the use cases in previous chapters, most of the times, the algorithms that exist out there do not directly apply to the problems at hand. This is when a thorough understanding of the algorithm comes in handy. That is exactly what we will be doing in this chapter; we will take a look at a series of algorithms and then try to understand them better with the help of some examples.
In this chapter, we will discuss the following algorithms with some examples:
  • Recursion
  • Dijkstra
  • Breadth First Search (BFS)
  • Dynamic Programming
  • Greedy Algorithm
  • Branch And Bound
Let's set up a bare-bones Node.js project before we start looking at the use cases.

Creating a Node.js application

For this chapter, we will use a very simple and light Node.js application, which will be holding our example scripts. The main goal here is to be able to run each of the use cases individually and not have an entire web (client or server) application for each of them. This helps us to have a uniform base project.
  1. The first step is to create your application's project folder. From the Terminal, run the following command:
mkdir <project-name>
  1. Then, to initialize a Node.js project, run the init command in the root folder of the project. This will prompt a series of questions to generate the package.json file. You can fill out the answers you wish or just click on return to accept default values for the prompts:
cd <project-name>
npm init
  1. Let's also install our beloved lodash to help us out with some of the trivial array and object manipulations and utilities:
npm install --save lodash

Use cases

Once your project is ready to go, we can now add the necessary scripts in the project's root and then run them independently.

Using recursion to serialize data

Recursion is a very popular programming paradigm in which a problem statement can be broken down into several smaller problems, which can be defined in terms of itself. Recursion is usually confused with divide and concur, in which the problem statement is broken into non-overlapping sub-problems which can be solved simultaneously.
In the following section, we will take a simple tree in which we have a root element followed by some child elements. We will be serializing this tree data, which can then be easily sent to the UI or persisted in the database.
Let's first create a folder called recursion within our project, which we created based on the preceding section. Then, we can create our serializer.js file within this folder, which will contain the class for serializing tree data.

Pseudocode

Let's formulate our algorithm in pseudo code before implementing the recursive serializer:
INITIALIZE response

FOR each node

extract child nodes

add current node info to serialized string

IF childNodes exist

repeat process for child nodes

ELSE

add ^...

Table of contents