(This page has no text content)
(This page has no text content)
Michael McMillan Data Structures and Algorithms with JavaScript
Data Structures and Algorithms with JavaScript by Michael McMillan Copyright © 2014 Michael McMillan. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editors: Brian MacDonald and Meghan Blanchette Production Editor: Melanie Yarbrough Copyeditor: Becca Freed Proofreader: Amanda Kersey Indexer: Ellen Troutman-Zaig Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrators: Rebecca Demarest and Cynthia Clarke Fehrenbach March 2014: First Edition Revision History for the First Edition: 2014-03-06: First release See http://oreilly.com/catalog/errata.csp?isbn=9781449364939 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Data Structures and Algorithms with JavaScript, the image of an amur hedgehog, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-449-36493-9 [LSI]
Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix 1. The JavaScript Programming Environment and Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 The JavaScript Environment 1 JavaScript Programming Practices 2 Declaring and Intializing Variables 3 Arithmetic and Math Library Functions in JavaScript 3 Decision Constructs 4 Repetition Constructs 6 Functions 7 Variable Scope 8 Recursion 10 Objects and Object-Oriented Programming 10 Summary 12 2. Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 JavaScript Arrays Defined 13 Using Arrays 13 Creating Arrays 14 Accessing and Writing Array Elements 15 Creating Arrays from Strings 15 Aggregate Array Operations 16 Accessor Functions 17 Searching for a Value 17 String Representations of Arrays 18 Creating New Arrays from Existing Arrays 18 Mutator Functions 19 Adding Elements to an Array 19 Removing Elements from an Array 20 iii
Adding and Removing Elements from the Middle of an Array 21 Putting Array Elements in Order 22 Iterator Functions 23 Non–Array-Generating Iterator Functions 23 Iterator Functions That Return a New Array 25 Two-Dimensional and Multidimensional Arrays 27 Creating Two-Dimensional Arrays 27 Processing Two-Dimensional Array Elements 28 Jagged Arrays 30 Arrays of Objects 30 Arrays in Objects 31 Exercises 33 3. Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 A List ADT 35 A List Class Implementation 36 Append: Adding an Element to a List 37 Remove: Removing an Element from a List 37 Find: Finding an Element in a List 38 Length: Determining the Number of Elements in a List 38 toString: Retrieving a List’s Elements 38 Insert: Inserting an Element into a List 39 Clear: Removing All Elements from a List 39 Contains: Determining if a Given Value Is in a List 40 Traversing a List 40 Iterating Through a List 41 A List-Based Application 42 Reading Text Files 42 Using Lists to Manage a Kiosk 43 Exercises 47 4. Stacks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Stack Operations 49 A Stack Implementation 50 Using the Stack Class 53 Multiple Base Conversions 53 Palindromes 54 Demonstrating Recursion 56 Exercises 57 5. Queues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Queue Operations 59 iv | Table of Contents
An Array-Based Queue Class Implementation 60 Using the Queue Class: Assigning Partners at a Square Dance 63 Sorting Data with Queues 67 Priority Queues 70 Exercises 72 6. Linked Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Shortcomings of Arrays 73 Linked Lists Defined 74 An Object-Based Linked List Design 75 The Node Class 75 The Linked List Class 76 Inserting New Nodes 76 Removing Nodes from a Linked List 78 Doubly Linked Lists 81 Circularly Linked Lists 85 Other Linked List Functions 86 Exercises 86 7. Dictionaries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 The Dictionary Class 89 Auxiliary Functions for the Dictionary Class 91 Adding Sorting to the Dictionary Class 93 Exercises 94 8. Hashing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 An Overview of Hashing 97 A Hash Table Class 98 Choosing a Hash Function 98 A Better Hash Function 101 Hashing Integer Keys 103 Storing and Retrieving Data in a Hash Table 106 Handling Collisions 107 Separate Chaining 107 Linear Probing 109 Exercises 111 9. Sets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Fundamental Set Definitions, Operations, and Properties 113 Set Definitions 113 Set Operations 114 The Set Class Implementation 114 Table of Contents | v
More Set Operations 116 Exercises 120 10. Binary Trees and Binary Search Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Trees Defined 121 Binary Trees and Binary Search Trees 123 Building a Binary Search Tree Implementation 124 Traversing a Binary Search Tree 126 BST Searches 129 Searching for the Minimum and Maximum Value 130 Searching for a Specific Value 131 Removing Nodes from a BST 132 Counting Occurrences 134 Exercises 137 11. Graphs and Graph Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Graph Definitions 139 Real-World Systems Modeled by Graphs 141 The Graph Class 141 Representing Vertices 141 Representing Edges 142 Building a Graph 143 Searching a Graph 145 Depth-First Search 145 Breadth-First Search 148 Finding the Shortest Path 149 Breadth-First Search Leads to Shortest Paths 149 Determining Paths 150 Topological Sorting 151 An Algorithm for Topological Sorting 152 Implementing the Topological Sorting Algorithm 152 Exercises 157 12. Sorting Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 An Array Test Bed 159 Generating Random Data 161 Basic Sorting Algorithms 161 Bubble Sort 162 Selection Sort 165 Insertion Sort 167 Timing Comparisons of the Basic Sorting Algorithms 168 Advanced Sorting Algorithms 170 vi | Table of Contents
The Shellsort Algorithm 171 The Mergesort Algorithm 176 The Quicksort Algorithm 181 Exercises 186 13. Searching Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Sequential Search 187 Searching for Minimum and Maximum Values 190 Using Self-Organizing Data 193 Binary Search 196 Counting Occurrences 200 Searching Textual Data 202 Exercises 205 14. Advanced Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Dynamic Programming 207 A Dynamic Programming Example: Computing Fibonacci Numbers 208 Finding the Longest Common Substring 211 The Knapsack Problem: A Recursive Solution 214 The Knapsack Problem: A Dynamic Programming Solution 215 Greedy Algorithms 217 A First Greedy Algorithm Example: The Coin-Changing Problem 217 A Greedy Algorithm Solution to the Knapsack Problem 218 Exercises 220 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Table of Contents | vii
(This page has no text content)
Preface Over the past few years, JavaScript has been used more and more as a server-side com‐ puter programming language owing to platforms such as Node.js and SpiderMonkey. Now that JavaScript programming is moving out of the browser, programmers will find they need to use many of the tools provided by more conventional languages, such as C++ and Java. Among these tools are classic data structures such as linked lists, stacks, queues, and graphs, as well as classic algorithms for sorting and searching data. This book discusses how to implement these data structures and algorithms for server-side JavaScript programming. JavaScript programmers will find this book useful because it discusses how to implement data structures and algorithms within the constraints that JavaScript places them, such as arrays that are really objects, overly global variables, and a prototype-based object system. JavaScript has an unfair reputation as a “bad” programming language, but this book demonstrates how you can use JavaScript to develop efficient and effective data structures and algorithms using the language’s “good parts.” Why Study Data Structures and Algorithms I am assuming that many of you reading this book do not have a formal education in computer science. If you do, then you already know why studying data structures and algorithms is important. If you do not have a degree in computer science or haven’t studied these topics formally, you should read this section. The computer scientist Nicklaus Wirth wrote a computer programming textbook titled Algorithms + Data Structures = Programs (Prentice-Hall). That title is the essence of computer programming. Any computer program that goes beyond the trivial “Hello, world!” will usually require some type of structure to manage the data the program is written to manipulate, along with one or more algorithms for translating the data from its input form to its output form. ix
For many programmers who didn’t study computer science in school, the only data structure they are familiar with is the array. Arrays are great for some problems, but for many complex problems, they are simply not sophisticated enough. Most experienced programmers will admit that for many programming problems, once they come up with the proper data structure, the algorithms needed to solve the problem are easier to design and implement. An example of a data structure that leads to efficient algorithms is the binary search tree (BST). A binary search tree is designed so that it is easy to find the minimum and maximum values of a set of data, yielding an algorithm that is more efficient than the best search algorithms available. Programmers unfamiliar with BSTs will instead prob‐ ably use a simpler data structure that ends up being less efficient. Studying algorithms is important because there is always more than one algorithm that can be used to solve a problem, and knowing which ones are the most efficient is im‐ portant for the productive programmer. For example, there are at least six or seven ways to sort a list of data, but knowing that the Quicksort algorithm is more efficient than the selection sort algorithm will lead to a much more efficient sorting process. Or that it’s fairly easy to implement a sequential or linear search algorithm for a list of data, but knowing that the binary sort algorithm can sometimes be twice as efficient as the se‐ quential search will lead to a better program. The comprehensive study of data structures and algorithms teaches you not only which data structures and which algorithms are the most efficient, but you also learn how to decide which data structures and which algorithms are the most appropriate for the problem at hand. There will often be trade-offs involved when writing a program, es‐ pecially in the JavaScript environment, and knowing the ins and outs of the various data structures and algorithms covered in this book will help you make the proper decision for any particular programming problem you are trying to solve. What You Need for This Book The programming environment we use in this book is the JavaScript shell based on the SpiderMonkey JavaScript engine. Chapter 1 provides instructions on downloading the shell for your environment. Other shells will work as well, such as the Node.js Java‐ Script shell, though you will have to make some translations for the programs in the book to work in Node. Other than the shell, the only thing you need is a text editor for writing your JavaScript programs. x | Preface
Organization of the Book • Chapter 1 presents an overview of the JavaScript language, or at least the features of the JavaScript language used in this book. This chapter also demonstrates through use the programming style used throughout the other chapters. • Chapter 2 discusses the most common data structure in computer programming: the array, which is native to JavaScript. • Chapter 3 introduces the first implemented data structure: the list. • Chapter 4 covers the stack data structure. Stacks are used throughout computer science in both compiler and operating system implementations. • Chapter 5 discusses queue data structures. Queues are an abstraction of the lines you stand in at a bank or the grocery store. Queues are used extensively in simulation software where data has to be lined up before it is processed. • Chapter 6 covers Linked lists. A linked list is a modification of the list data structure, where each element is a separate object linked to the objects on either side of it. Linked lists are efficient when you need to perform multiple insertions and dele‐ tions in your program. • Chapter 7 demonstrates how to build and use dictionaries, which are data structures that store data as key-value pairs. • One way to implement a dictionary is to use a hash table, and Chapter 8 discusses how to build hash tables and the hash algorithms that are used to store data in the table. • Chapter 9 covers the set data structure. Sets are often not covered in data structure books, but they can be useful for storing data that is not supposed to have duplicates in the data set. • Binary trees and binary search trees are the subject of Chapter 10. As mentioned earlier, binary search trees are useful for storing data that needs to be stored orig‐ inally in sorted form. • Chapter 11 covers graphs and graph algorithms. Graphs are used to represent data such as the nodes of a computer network or the cities on a map. • Chapter 12 moves from data structures to algorithms and discusses various algo‐ rithms for sorting data, including both simple sorting algorithms that are easy to implement but are not efficient for large data sets, and more complex algorithms that are appropriate for larger data sets. • Chapter 13 also covers algorithms, this time searching algorithms such as sequential search and binary search. • The last chapter of the book, Chapter 14, discusses a couple more advanced algo‐ rithms for working with data—dynamic programming and greedy algorithms. Preface | xi
These algorithms are useful for solving hard problems where a more traditional algorithm is either too slow or too hard to implement. We examine some classic problems for both dynamic programming and greedy algorithms in the chapter. Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new terms, URLs, email addresses, filenames, and file extensions. Constant width Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords. Constant width bold Shows commands or other text that should be typed literally by the user. Constant width italic Shows text that should be replaced with user-supplied values or by values deter‐ mined by context. Using Code Examples Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/oreillymedia/data_structures_and_algorithms_using_javascript. This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of ex‐ ample code from this book into your product’s documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Data Structures and Algorithms Using Java‐ Script by Michael McMillian (O’Reilly). Copyright 2014 Michael McMillan, 978-1-449-36493-9.” If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com. xii | Preface
Safari® Books Online Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business. Technology professionals, software developers, web designers, and business and crea‐ tive professionals use Safari Books Online as their primary resource for research, prob‐ lem solving, learning, and certification training. Safari Books Online offers a range of product mixes and pricing programs for organi‐ zations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Pro‐ fessional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technol‐ ogy, and dozens more. For more information about Safari Books Online, please visit us online. How to Contact Us Please address comments and questions concerning this book to the publisher: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-998-9938 (in the United States or Canada) 707-829-0515 (international or local) 707-829-0104 (fax) We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://oreil.ly/data_structures_algorithms_JS. To comment or ask technical questions about this book, send email to bookques tions@oreilly.com. For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com. Find us on Facebook: http://facebook.com/oreilly Follow us on Twitter: http://twitter.com/oreillymedia Watch us on YouTube: http://www.youtube.com/oreillymedia Preface | xiii
Acknowledgments There are always lots of people to thank when you’ve finished writing a book. I’d like to thank my acquisition editor, Simon St. Laurent, for believing in this book and getting me started writing it. Meghan Blanchette worked hard to keep me on schedule, and if I went off schedule, it definitely wasn’t her fault. Brian MacDonald worked extremely hard to make this book as understandable as possible, and he helped make several parts of the text much clearer than I had written them originally. I also want to thank my technical reviewers for reading all the text as well as the code, and for pointing out places where both my prose and my code needed to be clearer. My colleague and illustrator, Cynthia Fehrenbach, did an outstanding job translating my chicken scratchings into crisp, clear illustrations, and she deserves extra praise for her willingness to redraw several illustrations at the very last minute. Finally, I’d like to thank all the people at Mozilla for designing an excellent JavaScript engine and shell and writing some excellent documentation for using both the language and the shell. xiv | Preface
CHAPTER 1 The JavaScript Programming Environment and Model This chapter describes the JavaScript programming environment and the programming constructs we’ll use in this book to define the various data structures and algorithms examined. The JavaScript Environment JavaScript has historically been a programming language that ran only inside a web browser. However, in the past few years, there has been the development of JavaScript programming environments that can be run from the desktop, or similarly, from a server. In this book we use one such environment: the JavaScript shell that is part of Mozilla’s comprehensive JavaScript environment known as SpiderMonkey. To download the JavaScript shell, navigate to the Nightly Build web page. Scroll to the bottom of the page and pick the download that matches your computer system. Once you’ve downloaded the program, you have two choices for using the shell. You can use it either in interactive mode or to interpret JavaScript programs stored in a file. To use the shell in interactive mode, type the command js at a command prompt. The shell prompt, js>, will appear and you are ready to start entering JavaScript ex‐ pressions and statements. The following is a typical interaction with the shell: js> 1 1 js> 1+2 3 js> var num = 1; js> num*124 124 1
js> for (var i = 1; i < 6; ++i) { print(i); } 1 2 3 4 5 js> You can enter arithmetic expressions and the shell will immediately evaluate them. You can write any legal JavaScript statement and the shell will immediately evaluate it as well. The interactive shell is great for exploring JavaScript statements to discover how they work. To leave the shell when you are finished, type the command quit(). The other way to use the shell is to have it interpret complete JavaScript programs. This is how we will use the shell throughout the rest of the book. To use the shell to intepret programs, you first have to create a file that contains a JavaScript program. You can use any text editor, making sure you save the file as plain text. The only requirement is that the file must have a .js extension. The shell has to see this extension to know the file is a JavaScript program. Once you have your file saved, you interpret it by typing the js command followed by the full filename of your program. For example, if you saved the for loop code fragment that’s shown earlier in a file named loop.js, you would enter the following: c:\js>js loop.js which would produce the following output: 1 2 3 4 5 After the program is executed, control is returned to the command prompt. JavaScript Programming Practices In this section we discuss how we use JavaScript. We realize that programmers have different styles and practices when it comes to writing programs, and we want to de‐ scribe ours here at the beginning of the book so that you’ll understand the more complex code we present in the rest of the book. This isn’t a tutorial on using JavaScript but is just a guide to how we use the fundamental constructs of the language. 2 | Chapter 1: The JavaScript Programming Environment and Model
Declaring and Intializing Variables JavaScript variables are global by default and, strictly speaking, don’t have to be declared before using. When a JavaScript variable is initialized without first being declared, it becomes a global variable. In this book, however, we follow the convention used with compiled languages such as C++ and Java by declaring all variables before their first use. The added benefit to doing this is that declared variables are created as local vari‐ ables. We will talk more about variable scope later in this chapter. To declare a variable in JavaScript, use the keyword var followed by a variable name, and optionally, an assignment expression. Here are some examples: var number; var name; var rate = 1.2; var greeting = "Hello, world!"; var flag = false; Arithmetic and Math Library Functions in JavaScript JavaScript utilizes the standard arithmetic operators: • + (addition) • - (subtraction) • * (multiplication) • / (division) • % (modulo) JavaScript also has a math library you can use for advanced functions such as square root, absolute value, and the trigonometric functions. The arithmetic operators follow the standard order of operations, and parentheses can be used to modify that order. Example 1-1 shows some examples of performing arithmetic in JavaScript, as well as examples of using several of the mathematical functions. Example 1-1. Arithmetic and math functions in JavaScript var x = 3; var y = 1.1; print(x + y); print(x * y); print((x+y)*(x-y)); var z = 9; print(Math.sqrt(z)); print(Math.abs(y/x)); The output from this program is: JavaScript Programming Practices | 3
4.1 3.3000000000000003 7.789999999999999 3 0.3666666666666667 If you don’t want or need the precision shown above, you can format a number to a fixed precision: var x = 3; var y = 1.1; var z = x * y; print(z.toFixed(2)); // displays 3.30 Decision Constructs Decision constructs allow our programs to make decisions on what programming statements to execute based on a Boolean expression. The two decision constructs we use in this book are the if statement and the switch statement. The if statement comes in three forms: • The simple if statement • The if-else statement • The if-else if statement Example 1-2 shows how to write a simple if statement. Example 1-2. The simple if statement var mid = 25; var high = 50; var low = 1; var current = 13; var found = -1; if (current < mid) { mid = (current-low) / 2; } Example 1-3 demonstrates the if-else statement. Example 1-3. The if-else statement var mid = 25; var high = 50; var low = 1; var current = 13; var found = -1; if (current < mid) { mid = (current-low) / 2; } 4 | Chapter 1: The JavaScript Programming Environment and Model
Comments 0
Loading comments...
Reply to Comment
Edit Comment