Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo
Learning JavaScript Data Structures and Algorithms

Book Description

Understand and implement classic data structures and algorithms using JavaScript

In Detail

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.

This book begins by covering the basics of the JavaScript language and then moves on to discuss the most important data structures such as array, queue, stack, and linked list. You will also gain an in-depth knowledge of how hash tables and set data structure function. After this, you will be taught what trees are, and how to use the binary tree and the binary search tree.

In subsequent chapters, you will learn about graphs, DFS, and BFS. Finally, we will round off by learning how to differentiate between various searching and sorting algorithms such as sequential search, binary search, quick sort, bubble sort, and so on, and how to implement them. Towards the end of the book, you will also be introduced to dynamic programming.

What You Will Learn

  • Declare, initialize, add, and remove items from arrays, stacks, and queues
  • Create and use the most complex data structure, graphs, along with DFS and BFS algorithms
  • Grasp the power of linked lists, doubly linked lists, and circular linked lists
  • Store unique elements with hash tables, dictionaries, and sets
  • Explore the applications of binary trees and binary search trees
  • Sort data structures using bubble sort, selection sort, insertion sort, merge sort, and quick sort
  • Search elements in data structures using sequential sort and binary search
  • Understand the importance of big O notation, dynamic programming, and greedy algorithms
  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

    Table of Contents

    1. Learning JavaScript Data Structures and Algorithms
      1. Table of Contents
      2. Learning JavaScript Data Structures and Algorithms
      3. Credits
      4. About the Author
      5. Acknowledgments
      6. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      8. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. Questions
      9. 1. JavaScript – A Quick Overview
        1. Setting up the environment
          1. The browser is enough
          2. Using web servers (XAMPP)
          3. It's all about JavaScript (Node.js)
        2. JavaScript basics
          1. Variables
            1. Variable scope
          2. Operators
          3. Truthy and falsy
          4. The equals operators (== and ===)
        3. Control structures
          1. Conditional statements
          2. Loops
        4. Functions
        5. Object-oriented programming
        6. Debugging and tools
        7. Summary
      10. 2. Arrays
        1. Why should we use arrays?
        2. Creating and initializing arrays
        3. Adding and removing elements
        4. Two-dimensional and multi-dimensional arrays
        5. References for JavaScript array methods
          1. Joining multiple arrays
          2. Iterator functions
          3. Searching and sorting
            1. Custom sorting
            2. Sorting strings
            3. Searching
          4. Outputting the array into a string
        6. Summary
      11. 3. Stacks
        1. Creating a stack
          1. The complete Stack class
            1. Using the Stack class
        2. Decimal to binary
        3. Summary
      12. 4. Queues
        1. Creating a queue
          1. The complete Queue class
          2. Using the Queue class
        2. The priority queue
        3. The circular queue – Hot Potato
        4. Summary
      13. 5. Linked Lists
        1. Creating a linked list
          1. Appending elements to the end of the linked list
          2. Removing elements from the linked list
          3. Inserting an element at any position
          4. Implementing other methods
            1. The toString method
            2. The indexOf method
            3. The isEmpty, size, and getHead methods
        2. Doubly linked lists
          1. Inserting a new element at any position
          2. Removing elements from any position
        3. Circular linked lists
        4. Summary
      14. 6. Sets
        1. Creating a set
          1. The has (value) method
          2. The add method
          3. The remove and clear methods
          4. The size method
          5. The values method
          6. Using the Set class
        2. Set operations
          1. Set union
          2. Set intersection
          3. Set difference
          4. Subset
        3. Summary
      15. 7. Dictionaries and Hashes
        1. Dictionaries
          1. Creating a dictionary
            1. The has and set methods
            2. The remove method
            3. The get and values methods
            4. The clear, size, keys, and getItems methods
          2. Using the Dictionary class
        2. The hash table
          1. Creating a hash table
          2. Using the HashTable class
          3. Hash table versus hash set
          4. Handling collisions between hash tables
            1. Separate chaining
              1. The put method
              2. The get method
              3. The remove method
            2. Linear probing
              1. The put method
              2. The get method
              3. The remove method
          5. Creating better hash functions
        3. Summary
      16. 8. Trees
        1. Trees terminology
        2. Binary tree and binary search tree
          1. Creating the BinarySearchTree class
          2. Inserting a key in a tree
        3. Tree traversal
          1. In-order traversal
          2. Pre-order traversal
          3. Post-order traversal
        4. Searching for values in a tree
          1. Searching for minimum and maximum values
          2. Searching for a specific value
          3. Removing a node
            1. Removing a leaf node
            2. Removing a node with a left or right child
            3. Removing a node with two children
        5. More about binary trees
        6. Summary
      17. 9. Graphs
        1. Graph terminology
          1. Directed and undirected graphs
        2. Representing a graph
          1. The adjacency matrix
          2. The adjacency list
          3. The incidence matrix
        3. Creating the Graph class
        4. Graph traversals
          1. Breadth-first search (BFS)
            1. Finding the shortest paths using BFS
            2. Further studies on the shortest paths algorithms
          2. Depth-first search (DFS)
            1. Exploring the DFS algorithm
            2. Topological sorting using DFS
        5. Summary
      18. 10. Sorting and Searching Algorithms
        1. Sorting algorithms
          1. Bubble sort
            1. Improved bubble sort
          2. Selection sort
          3. Insertion sort
          4. Merge sort
          5. Quick sort
            1. The partition process
            2. Quick sort in action
        2. Searching algorithms
          1. Sequential search
          2. Binary search
        3. Summary
      19. Index