CLASSIC DATA STRUCTURES IN JAVA

Similar documents
Data Structures in C++ Using the Standard Template Library

DATA ABSTRACTION AND PROBLEM SOLVING WITH JAVA

Data Structures and Abstractions with Java

Table of Contents. Chapter 1: Introduction to Data Structures... 1

WITH C+ + William Ford University of the Pacific. William Topp University of the Pacific. Prentice Hall, Englewood Cliffs, New Jersey 07632

DATA STRUCTURES AND PROBLEM SOLVING USING JAVA

Anany Levitin 3RD EDITION. Arup Kumar Bhattacharjee. mmmmm Analysis of Algorithms. Soumen Mukherjee. Introduction to TllG DCSISFI &

Data Structures and Algorithm Analysis in C++

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

CS 445: Data Structures Final Examination: Study Guide

Chapter 1 Introduction

ASSIGNMENTS. Progra m Outcom e. Chapter Q. No. Outcom e (CO) I 1 If f(n) = Θ(g(n)) and g(n)= Θ(h(n)), then proof that h(n) = Θ(f(n))

Programming. In Ada JOHN BARNES TT ADDISON-WESLEY

Objects First with Java

Prelude to Programming

PROGRAMMING IN C++ (Regulation 2008) Answer ALL questions PART A (10 2 = 20 Marks) PART B (5 16 = 80 Marks) function? (8)

PROBLEM SOLVING USING JAVA WITH DATA STRUCTURES. A Multimedia Approach. Mark Guzdial and Barbara Ericson PEARSON. College of Computing

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

PYTHON. p ykos vtawynivis. Second eciitiovl. CO Ve, WESLEY J. CHUN

Introduction to Algorithms Third Edition

World Wide Web PROGRAMMING THE PEARSON EIGHTH EDITION. University of Colorado at Colorado Springs

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Analysis of Algorithms

Course Name: B.Tech. 3 th Sem. No of hours allotted to complete the syllabi: 44 Hours No of hours allotted per week: 3 Hours. Planned.

Quality Code. Software Testing Principles, Practices, and Patterns. Stephen Vance. AAddison-Wesley

DATA STRUCTURES THROUGH C++

Acknowledgments I INTRODUCTION 1

The Unified Modeling Language User Guide

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE. Sample Final Exam

4.1.2 Merge Sort Sorting Lower Bound Counting Sort Sorting in Practice Solving Problems by Sorting...

CJT^jL rafting Cm ompiler

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Course Review for Finals. Cpt S 223 Fall 2008

MariaDB Crash Course. A Addison-Wesley. Ben Forta. Upper Saddle River, NJ Boston. Indianapolis. Singapore Mexico City. Cape Town Sydney.

SQL Queries. for. Mere Mortals. Third Edition. A Hands-On Guide to Data Manipulation in SQL. John L. Viescas Michael J. Hernandez

COURSE: DATA STRUCTURES USING C & C++ CODE: 05BMCAR17161 CREDITS: 05

About this exam review

Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest. Introduction to Algorithms

Programming in Python 3

Part I Basic Concepts 1

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

Discuss the following operations on One-Dimensional array with algorithms.

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

JAVA CONCEPTS Early Objects

Data Structures and Abstractions with Java

DESIGN AND ANALYSIS OF ALGORITHMS

MATHEMATICAL STRUCTURES FOR COMPUTER SCIENCE

INDEX. Cambridge University Press How to Think About Algorithms Jeff Edmonds Index More information

An Introduction to Object-Oriented Programming

CRYPTOGRAPHY AND NETWORK SECURITY

THE DESIGN AND ANALYSIS OF COMPUTER ALGORITHMS

ony Gaddis Haywood Community College STARTING OUT WITH PEARSON Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto

Real-Time Systems and Programming Languages

End-Term Examination Second Semester [MCA] MAY-JUNE 2006

Programming. Principles and Practice Using C++ Bjarne Stroustrup. / Addison-Wesley. Second Edition

DB2 SQL Tuning Tips for z/os Developers

Contents. 1 Introduction. 2 Searching and Traversal Techniques. Preface... (vii) Acknowledgements... (ix)

Fundamentals of. Database Systems. Shamkant B. Navathe. College of Computing Georgia Institute of Technology PEARSON.

CSC2100-Data Structures

What s New in the Second Edition

ощ 'ршорвшэш! цвн-эориэу ощ 'sajbpossv # PIPG DUJ 'ssjmoossv ^ PIPG pipa w н OX ЛЮН VAV

JAVASCRIPT FOR PROGRAMMERS

CS2013 Course Syllabus Spring 2018 Lecture: Mon/Wed 2:00 P.M. 2:50 P.M. SH C259 Lab: Mon/Wed 2:50 P.M. 4:00 P.M. SH C259

Workbook for C++ THIRD EDITION. Yedidyah Langsam, Ph.D. Brooklyn College of the City University of New York. Custom Publishing

Database Concepts. David M. Kroenke UNIVERSITATSBIBLIOTHEK HANNOVER

Contents. Preface xvii Acknowledgments. CHAPTER 1 Introduction to Parallel Computing 1. CHAPTER 2 Parallel Programming Platforms 11

Object-Oriented Programming and Data Structures

QUESTION BANK. Prepared by,mrs.d.maladhy AP/IT,RGCET. Page 1

Structured Parallel Programming Patterns for Efficient Computation

Computational Discrete Mathematics

Algorithmic Graph Theory and Perfect Graphs

About the Author. Dependency Chart. Chapter 1: Logic and Sets 1. Chapter 2: Relations and Functions, Boolean Algebra, and Circuit Design

Course Review. Cpt S 223 Fall 2009

SARDAR RAJA COLLEGE OF ENGINEERING

Digital System Design with SystemVerilog

Notation Index. Probability notation. (there exists) (such that) Fn-4 B n (Bell numbers) CL-27 s t (equivalence relation) GT-5.

Visual C# Tony Gaddis. Haywood Community College STARTING OUT WITH. Piyali Sengupta. Third Edition. Global Edition contributions by.

CS301 - Data Structures Glossary By

LATEX. Leslie Lamport. Digital Equipment Corporation. Illustrations by Duane Bibby. v ADDISON-WESLEY

LOGIC AND DISCRETE MATHEMATICS

Notation Index 9 (there exists) Fn-4 8 (for all) Fn-4 3 (such that) Fn-4 B n (Bell numbers) CL-25 s ο t (equivalence relation) GT-4 n k (binomial coef

Algorithms and programs, basic idea of pseudo-code.algorithm efficiency and analysis, time and space analysis of algorithms order notations.

DYNAMIC MEMORY ALLOCATION AND DEALLOCATION

CSE373 Winter 2014, Final Examination March 18, 2014 Please do not turn the page until the bell rings.

Contents. Chapter 1 SPECIFYING SYNTAX 1

School of Computing and Information Sciences. Course Title: Data Structures Date: 3/30/2010 Course Number: COP 3530 Number of Credits: 3

Structured Parallel Programming

Big Java Late Objects

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Bachelor Level/ First Year/ Second Semester/ Science Full Marks: 60 Computer Science and Information Technology (CSc. 154) Pass Marks: 24

Systems:;-'./'--'.; r. Ramez Elmasri Department of Computer Science and Engineering The University of Texas at Arlington

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

INSTITUTE OF AERONAUTICAL ENGINEERING

Data Structures Question Bank Multiple Choice

Engineering Real- Time Applications with Wild Magic

Transcription:

CLASSIC DATA STRUCTURES IN JAVA Timothy Budd Oregon State University Boston San Francisco New York London Toronto Sydney Tokyo Singapore Madrid Mexico City Munich Paris Cape Town Hong Kong Montreal

CONTENTS PREFACE XV THE MANAGEMENT OF COMPLEXITY 1 1.1 The Control of Complexity 2 1.2 Abstraction, Information Hiding, and Layering 3 1.3 Division into Parts 6 1.3.1 Encapsulation and Interchangeability 6 1.3.2 Interface and Implementation 7 1.3.3 The Service View 8 1.3.4 Repetition and Recursion 9 1.4 Composition 11 1.5 Layers of Specialization 14 1.6 Multiple Views 16 1.7 Patterns 16 1.8 Chapter Summary 18 Further Information 19 Study Questions 20 Exercises 21 Programming Projects 21 ABSTRACT DATA TYPES 23 2.1 What Is a Type? 24 2.1.1 Classes 25 2.1.2 Interfaces and Polymorphism 27 2.2 Abstract Data Types 30 2.3 The Fundamental ADTs 34 2.3.1 Collection 34

vi CONTENTS 2.3.2 Bag 36 2.3.3 Set 37 2.3.4 Sorted, Comparator, and Comparable 38 2.3.5 Stack, Queue, and Deque 39 2.3.6 FindMin and FindNth 41 2.3.7 Indexed Collections and Sorting Algorithms 2.3.8 Map 43 2.3.9 Matrix 44 2.4 Chapter Summary 45 Further Information 46 Study Questions 46 Exercises 46 Programming Projects 47 3 ALGORITHMS 49 3.1 Characteristics of Algorithms 50 3.2 Recipes as Algorithms 52 3.3 Analyzing Computer Algorithms 53 3.3.1 Specification of the Input 54 3.3.2 Description of the Result 56 3.3.3 Instruction Precision 57 3.3.4 Time to Execute 57 3.3.5 Space Utilization 60 3.4 Recursive Algorithms 60 3.5 Chapter Summary 64 Further Information 64 Study Questions 65 Exercises 65 Programming Projects 66 4 EXECUTION-TIME MEASUREMENT 69 4.1 Algorithmic Analysis and Big-Oh Notation 70 4.2 Execution Time of Programming Constructs 71 4.2.1 Constant Time 71 4.2.2 Simple Loops 72 4.2.3 Nested Loops 75 4.2.4 While Loops 77 4.2.5 Function Calls 79

Contents 4.3 Summing Algorithmic Execution Times 80 4.4 The Importance of Fast Algorithms 84 4.5 Benchmarking Execution Times 86 4.6 Chapter Summary 90 Further Information 91 Study Questions 91 Exercises 91 Programming Projects 94 5 INCREASING CONFIDENCE IN CORRECTNESS 97 5.1 Program Proofs 97 5.1.1 Invariants 98 5.1.2 Analyzing Loops 100 5.1.3 Asserting That the Outcome Is Correct 103 5.1.4 Progress toward an Objective 104 5.1.5 Manipulating Unnamed Quantities 105 5.1.6 Function Calls 106 5.1.7 Recursive Algorithms 107 5.2 Program Testing 109 5.3 Chapter Summary 111 Further Information 111 Study Questions 111 Exercises 112 Programming Projects 114 6 VECTORS 117 6.1 The Vector Data Structure 117 6.2 Enumeration 127 6.3 Application-Silly Sentences 128 6.4 Application-Memory Game 131 6.5 Application-Shell Sort 136 6.6 A Visual Vector 140 6.7 Chapter Summary 144 Further Information 144 Study Questions 144 Exercises 145 Programming Projects 149

viii CONTENTS 7 SORTING VECTORS 153 7.1 Divide and Conquer 153 7.1.1 Binary Search 155 7.2 Sorted Vectors 158 7.3 Merge Sort 161 7.4 Partitioning 165 7.4.1 The Pivot Algorithm 166 7.4.2 Finding the nth Element 168 7.4.3 Quicksort 171 7.5 Chapter Summary 175 Further Information 175 Study Questions 176 Exercises 176 Programming Projects 179 8 LINKED LISTS 181 8.1 Varieties of Linked Lists 185 8.2 LISP-Style Lists 187 8.3 The LinkedList Abstraction 189 8.4 Application-Asteroids Game 197 8.5 Application-Infinite-Precision Integers 207 8.6 Chapter Summary 211 Further Information 212 Study Questions 212 Exercises 212 Programming Projects 214 9 LIST VARIATIONS 217 9.1 Sorted Lists 217 9.1.1 Fast Merge 219 9.1.2 Execution Timings for Merge Operations 220 9.2 Self-Organizing Lists 221 9.3 Skip Lists 223 9.4 Chapter Summary 232 Further Information 232 Study Questions 233

Contents ix Exercises 234 Programming Projects 235 10 STACKS 237 10.1 The Stack ADT 239 10.2 Checking for Balanced Parentheses 240 10.3 Towers of Hanoi, Revisited 242 10.4 A Four-Function Calculator 244 10.5 A Solitaire Program 250 10.6 Implementation of the Stack Abstraction 257 10.7 Chapter Summary 260 Further Information 260 Study Questions 260 Exercises 261 Programming Projects 264 11 DEQUES 267 11.1 A Fractal Snowflake 269 11.2 Depth-and Breadth-First Search 273 11.3 An Implementation: The IndexedDeque 281 11.4 Chapter Summary 287 Further Information 287 Study Questions 287 Exercises 288 Programming Projects 291 12 QUEUES 293 12.1 The Queue ADT 294 12.2 The Caterpillar Game 295 12.3 A Pastry Factory Simulation 302 12.4 Implementation of the Queue Abstraction 308 12.4.1 A Vector-Based Queue 312 12.4.2 The Ring Buffer Queue 313 12.4.3 Piped Input/Output Streams 317 12.5 Chapter Summary 318 Further Information 318

X CONTENTS Study Questions 318 Exercises 319 Programming Projects 321 13 TREES 325 13.1 Binary Trees 330 13.2 Vector Implementation 333 13.3 Dynamic Memory Implementation 334 13.3.1 Application Guess the Animal Game 335 13.4 Tree Traversals 338 13.4.1 Postorder Tree Traversal 340 13.4.2 Preorder Tree Traversal 343 13.4.3 In-Order Tree Traversal 344 13.4.4 Level-Order Tree Traversal 345 13.5 Euler Tours 346 13.6 Binary Tree Representation of General Trees 351 13.7 Chapter Summary 353 Further Information 354 Study Questions 354 Exercises 354 Programming Projects 355 14 BINARY SEARCH TREES 357 14.1 The Importance of Balance 365 14.2 ÄVL Trees 369 14.3 Application-Tree Sort 374 14.4 Chapter Summary 376 Further Information 377 Study Questions 377 Exercises 378 Programming Projects 380 15 PRIORITY QUEUES 383 15.1 The Priority Queue ADT 384 15.2 Heaps 386 15.3 Skew Heaps 397 15.4 Application-Discrete Event-Driven Simulation 403 15.4.1 A Framework for Simulations 405

Contents 15.4.2 Ice Cream Store Simulation 408 15.5 Chapter Summary 409 Further Information 410 Study Questions 410 Exercises 411 Programming Projects 413 HASH TABLES 417 16.1 Hash Functions 418 16.1.1 Hash Functions 421 16.1.2 Hash Functions in the Java Standard Library 16.2 Collision Resolution 424 16.3 Hash Table Sorting Algorithms 427 16.3.1 Counting Sort 428 16.3.2 Radix Sorting 430 16 A Open-Address Hashing 433 16.5 The Hashtable Data Type 437 16.6 Application-Ranking Poker Hands 440 16.7 Chapter Summary 442 Further Information 443 Study Questions 444 Exercises 445 Programming Projects 447 MAPS 451 17.1 Example Programs 453 17.1.1 Silly-Sentence Generation, Revisited 453 17.1.2 An Address Database 458 17.1.3 A Concordance 461 17.2 An Implementation 463 17.3 Searching Problems and Maps 469 17.4 Chapter Summary 471 Further Information 471 Study Questions 472 Exercises 473 Programming Projects 474

xii CONTENTS 18 SETS 479 18.1 Changing a Bag into a Set 480 18.2 Set Union, Intersection, and Differences 18.3 Sorted List Sets 485 18.4 Application-A Spelling Checker 490 18.5 The Union-Find Problem 491 18.6 The BitSet Abstraction 495 18.7 Application-Prime Number Sieve 498 18.8 Chapter Summary 500 Further Information 501 Study Questions 501 Exercises 501 Programming Projects 503 482-19 MATRICES 507 19.1 Java Matrices 507 19.2 Application-Rain Game 510 19.3 Binary Matrices 514 19.4 Application The Game of Life 515 19.5 Sparse Vectors 519 19.6 An Application-(Almost) Infinitely Large Hash Tables 521 19.7 Sparse Matrices 523 19.8 Noninteger Keys 524 19.9 Chapter Summary 526 Further Information 526 Study Questions 527 Exercises 527 Programming Projects 528 20 GRAPHS 531 20.1 Adjacency-Matrix Representation 533 20.2 Edge-List Representation 537 20.3 Weighted-Graph Representation 541 20.3.1 Weighted-Adjacency Matrix 542 20.3.2 Floyd's Algorithm 542 20.3.3 Weighted-Edge-List Representation 543

Contents xiii 20.3.4 Dijkstra's Algorithm 544 20.4 Other Graph Problems 549 20.4.1 Topological Sorting 549 20.4.2 Depth-First Search Spanning Tree 550 20.4.3 Problem-The Traveling Salesman 551 20.5 Chapter Summary 553 Further Information 553 Study Questions 553 Exercises 554 Programming Projects 556 APPENDIX A JAVA SYNTAX 559 A.l Program Structure 559 A.l.l Packages 559 A.l.2 Import Declaration 560 A.l.3 Class Declaration 560 A.l.4 Interface Declaration 561 A.l.5 Method Declaration 561 A. 1.6 Constructors 563 A.1.7 Data Field Declaration 563 A.2 Statements 564 A.2.1 Declaration Statement 564 A.2.2 Assignment Statement 564 A.2.3 Procedure Calls 564 A.2.4 if Statement 565 A.2.5 switch Statement 565 A.2.6 while Statement 566 A.2.1 for Statement 566 A.2.8 return Statement 566 A.2.9 throw Statement 567 A.2.10 try Statement 567 A.3 Expressions 567 A. 3.1 Literal 568 A.3.2 Variables 568 A.3.3 Data Field and Method Access 569 A.3.4 Operators 570 A.3.5 Object Creation 570 A.3.6 Arrays 571 A.4 Files 571

xiv CONTENTS APPENDIX B IMPORT LIBRARIES 573 APPENDIX C DATA STRUCTURES IN THE JAVA STANDARD LIBRARY 577 C.l Collection 577 C.2 Enumerators and Iterators 578 C.3 Vectors 578 C.4 Lists 579 C.5 Stack, Queue, and Deque 580 C.6 Priority Queue 580 C.7 Binary Search Tree 580 C.8 Hash Tables 581 C.9 Set 581 CIO Map 582 BIBLIOGRAPHY 583 INDEX 589