Data Structures and Algorithms with JavaScript

Book description

As an experienced JavaScript developer moving to server-side programming, you need to implement classic data structures and algorithms associated with conventional object-oriented languages like C# and Java. This practical guide shows you how to work hands-on with a variety of storage mechanisms—including linked lists, stacks, queues, and graphs—within the constraints of the JavaScript environment.

Determine which data structures and algorithms are most appropriate for the problems you’re trying to solve, and understand the tradeoffs when using them in a JavaScript program. An overview of the JavaScript features used throughout the book is also included.

This book covers:

  • Arrays and lists: the most common data structures
  • Stacks and queues: more complex list-like data structures
  • Linked lists: how they overcome the shortcomings of arrays
  • Dictionaries: storing data as key-value pairs
  • Hashing: good for quick insertion and retrieval
  • Sets: useful for storing unique elements that appear only once
  • Binary Trees: storing data in a hierarchical manner
  • Graphs and graph algorithms: ideal for modeling networks
  • Algorithms: including those that help you sort or search data
  • Advanced algorithms: dynamic programming and greedy algorithms

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Why Study Data Structures and Algorithms
    2. What You Need for This Book
    3. Organization of the Book
    4. Conventions Used in This Book
    5. Using Code Examples
    6. Safari® Books Online
    7. How to Contact Us
    8. Content Updates
      1. October 20, 2015
    9. Acknowledgments
  2. 1. The JavaScript Programming Environment and Model
    1. The JavaScript Environment
    2. JavaScript Programming Practices
      1. Declaring and Initializing Variables
      2. Arithmetic and Math Library Functions in JavaScript
      3. Decision Constructs
      4. Repetition Constructs
      5. Functions
      6. Variable Scope
      7. Recursion
    3. Objects and Object-Oriented Programming
    4. Summary
  3. 2. Arrays
    1. JavaScript Arrays Defined
    2. Using Arrays
      1. Creating Arrays
      2. Accessing and Writing Array Elements
      3. Creating Arrays from Strings
      4. Aggregate Array Operations
    3. Accessor Functions
      1. Searching for a Value
      2. String Representations of Arrays
      3. Creating New Arrays from Existing Arrays
    4. Mutator Functions
      1. Adding Elements to an Array
      2. Removing Elements from an Array
      3. Adding and Removing Elements from the Middle of an Array
      4. Putting Array Elements in Order
    5. Iterator Functions
      1. Non–Array-Generating Iterator Functions
      2. Iterator Functions That Return a New Array
    6. Two-Dimensional and Multidimensional Arrays
      1. Creating Two-Dimensional Arrays
      2. Processing Two-Dimensional Array Elements
      3. Jagged Arrays
    7. Arrays of Objects
    8. Arrays in Objects
    9. Exercises
  4. 3. Lists
    1. A List ADT
    2. A List Class Implementation
      1. Append: Adding an Element to a List
      2. Remove: Removing an Element from a List
      3. Find: Finding an Element in a List
      4. Length: Determining the Number of Elements in a List
      5. toString: Retrieving a List’s Elements
      6. Insert: Inserting an Element into a List
      7. Clear: Removing All Elements from a List
      8. Contains: Determining if a Given Value Is in a List
      9. Moving To and Retrieving a List Element
      10. Iterating Through a List
    3. Iterating Through a List
    4. A List-Based Application
      1. Reading Text Files
      2. Using Lists to Manage a Kiosk
    5. Exercises
  5. 4. Stacks
    1. Stack Operations
    2. A Stack Implementation
    3. Using the Stack Class
      1. Multiple Base Conversions
      2. Palindromes
      3. Demonstrating Recursion
    4. Exercises
  6. 5. Queues
    1. Queue Operations
    2. An Array-Based Queue Class Implementation
    3. Using the Queue Class: Assigning Partners at a Square Dance
    4. Sorting Data with Queues
    5. Priority Queues
    6. Exercises
  7. 6. Linked Lists
    1. Shortcomings of Arrays
    2. Linked Lists Defined
    3. An Object-Based Linked List Design
      1. The Node Class
      2. The Linked List Class
      3. Inserting New Nodes
      4. Removing Nodes from a Linked List
    4. Doubly Linked Lists
    5. Circularly Linked Lists
    6. Other Linked List Functions
    7. Exercises
  8. 7. Dictionaries
    1. The Dictionary Class
    2. Auxiliary Functions for the Dictionary Class
    3. Adding Sorting to the Dictionary Class
    4. Exercises
  9. 8. Hashing
    1. An Overview of Hashing
    2. A Hash Table Class
      1. Choosing a Hash Function
      2. A Better Hash Function
      3. Hashing Integer Keys
      4. Storing and Retrieving Data in a Hash Table
    3. Handling Collisions
      1. Separate Chaining
      2. Linear Probing
    4. Exercises
  10. 9. Sets
    1. Fundamental Set Definitions, Operations, and Properties
      1. Set Definitions
      2. Set Operations
    2. The Set Class Implementation
    3. More Set Operations
    4. Exercises
  11. 10. Binary Trees and Binary Search Trees
    1. Trees Defined
    2. Binary Trees and Binary Search Trees
      1. Building a Binary Search Tree Implementation
      2. Traversing a Binary Search Tree
    3. BST Searches
      1. Searching for the Minimum and Maximum Value
      2. Searching for a Specific Value
    4. Removing Nodes from a BST
    5. Counting Occurrences
      1. Exercises
  12. 11. Graphs and Graph Algorithms
    1. Graph Definitions
    2. Real-World Systems Modeled by Graphs
    3. The Graph Class
      1. Representing Edges
      2. Building a Graph
    4. Searching a Graph
      1. Depth-First Search
      2. Breadth-First Search
    5. Finding the Shortest Path
      1. Breadth-First Search Leads to Shortest Paths
      2. Determining Paths
    6. Topological Sorting
      1. An Algorithm for Topological Sorting
      2. Implementing the Topological Sorting Algorithm
    7. Exercises
  13. 12. Sorting Algorithms
    1. An Array Test Bed
      1. Generating Random Data
    2. Basic Sorting Algorithms
      1. Bubble Sort
      2. Selection Sort
      3. Insertion Sort
      4. Timing Comparisons of the Basic Sorting Algorithms
    3. Advanced Sorting Algorithms
      1. The Shellsort Algorithm
      2. The Mergesort Algorithm
      3. The Quicksort Algorithm
    4. Exercises
  14. 13. Searching Algorithms
    1. Commonly Used Functions in Examples
      1. Searching for Minimum and Maximum Values
      2. Using Self-Organizing Data
    2. Binary Search
      1. Counting Occurrences
    3. Searching Textual Data
    4. Exercises
  15. 14. Advanced Algorithms
    1. Dynamic Programming
      1. A Dynamic Programming Example: Computing Fibonacci Numbers
      2. Finding the Longest Common Substring
      3. The Knapsack Problem: A Recursive Solution
      4. The Knapsack Problem: A Dynamic Programming Solution
    2. Greedy Algorithms
      1. A First Greedy Algorithm Example: The Coin-Changing Problem
      2. A Greedy Algorithm Solution to the Knapsack Problem
    3. Exercises
  16. Index

Product information

  • Title: Data Structures and Algorithms with JavaScript
  • Author(s): Michael McMillan
  • Release date: March 2014
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449373955