ebook img

Data structures and abstractions with Java PDF

1025 Pages·7.519 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Data structures and abstractions with Java

Data Structures and Abstractions with Java ™ Second Edition \ Frank M. Carrano University of Rhode Island Upper Saddle River, NJ 07458 Library of Congress Cataloging-in-Publication Data on File Vice President and Editorial Director, ECS: Marcia J. Horton Executive Editor: Tracy Dunkelberger Associate Editor: Carole Snyder Editorial Assistant: Christianna Lee Executive Managing Editor: Vince O’Brien Managing Editor: Camille Trentacoste Production Editor: Rose Kernan Director of Creative Services: Paul Belfanti Creative Director: Juan Lopez Art Director and Cover Manager: Maureen Eide Cover Design and Interior Illustrations: Liann Dunn Copy Editor: Rebecca Pepper Managing Editor, AV Management and Production: Patricia Burns Art Editor: Xiaohong Zhu Director, Image Resource Center: Melinda Reo Manager, Rights and Permissions: Zina Arabia Manager, Visual Research: Beth Brenzel Manager, Cover Visual Research and Permissions: Karen Sanatar Manufacturing Manager, ESM: Alexis Heydt-Long Manufacturing Buyer: Lisa McDowell Executive Marketing Manager: Robin O’Brien Marketing Assistant: Mack Patterson © 2007 Pearson Education, Inc. Pearson Prentice Hall Pearson Education, Inc. Upper Saddle River, NJ 07458 All rights reserved. No part of this book may be reproduced in any form or by any means, without permission in writing from the publisher. Pearson Prentice Hall™ is a trademark of Pearson Education, Inc. All other trademarks or product names are the property of their respective owners. The author and publisher of this book have used their best efforts in preparing this book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The author and publisher make no warranty of any kind, expressed or implied, with regard to these programs or the documentation contained in this book. The author and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs. Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 ISBN0-13-237045-X Pearson Education Ltd., London Pearson Education Australia Pty. Ltd., Sydney Pearson Education Singapore, Pte. Ltd. Pearson Education North Asia Ltd., Hong Kong Pearson Education Canada, Inc., Toronto Pearson Educación de Mexico, S.A. de C.V. Pearson Education—Japan, Tokyo Pearson Education Malaysia, Pte. Ltd. Pearson Education, Inc., Upper Saddle River, New Jersey To Walt, Alan, and Toni This page intentionally left blank Preface Welcome to the second edition of Data Structures and Abstractions with Java, a book for an introductory course in data structures, typically known as CS-2. Approach This book was created specifically with objects and Java in mind. Its approach makes learning easier by focusing the reader’s attention on one issue at a time, by providing flexibility in the order in which you can cover topics, and by clearly distinguishing between the specification and implementation of abstract data types, or ADTs. To accomplish these goals, the material is divided into short, numbered segments that are organized into 31 relatively short chapters. Each segment covers a single idea. Indi- vidual chapters deal with either the specification and use of an ADT or its implemen- tations. You can choose to cover the specification of an ADT followed by its various implementations, or you can treat the specification and use of several ADTs before you consider any implementation issues. The book’s organization makes it easy for you to choose the topic order that you prefer. New to This Edition ● New features of Java 5, including generic data types, the Scanner class, enumer- ations, for-each loops, the interface Iterable, and boxing and unboxing, are cov- ered and used throughout. ● Assertions within code are enforced using the assert statement. ● The linked implementation of the ADT list is covered in two chapters instead of one. The result provides an example of how to approach the implementation of a class by first focusing on core methods. v vi Preface ● The introduction to iterators occupies one chapter instead of two. The new interface Iterable is covered, and the nonstandard iterator of the first edition is no longer included. ● Hashing is covered in two chapters instead of one long chapter. ● The implementations of various ADTs, notably the linked implementation of the list and the binary search tree, have been improved. ● Important Java code is identified as a listing. ● Exercises, projects, and self-test questions have been supplemented and improved. ● Appendix B on exceptions has been revised to include the finally clause. ● Appendix C on file I/O has been revised to reflect aspects of Java 5. Features ● 31 relatively short chapters can be covered in several sequences. ● Individual but consecutive chapters separate the specification and implementation of ADTs. ● Short, bite-sized segments focus attention on one issue at a time. ● Many examples illustrate new concepts. ● Highlighted notes emphasize key material and provide supplementary comments. ● Programming tips give additional programming advice. ● Numerous figures make the presentation visual and accessible. ● Questions throughout the book—and their answers—elaborate on the material presented. ● Initial chapters cover Java classes, inheritance, polymorphism, and class design. ● Java code includes javadoc comments. ● Appendices review Java basics, exceptions, files, and documentation. ● The website at prenhall.com/carrano provides the Java code that appears in the book, as well as any updates or corrections. ● Supplements for instructors include PowerPoint slides, laboratory materials, complete source code, and answers to selected exercises and programming projects. A Note to Students After looking over this preface, you should read the Introduction. There you will quickly see what this book is about and what you need to know about Java before you begin. Appendices A through D review Java and serve as a primer on some new features of Java 5, exceptions, files, and javadoc comments. Note that inside the front and back covers you will find Java’s reserved words, its prim- itive data types, the precedence of its operators, and a list of Unicode characters. The topics that we cover in this book are fundamental to your future study of computer sci- ence. Even if you do not study all of these topics now, you are likely to encounter them later. We hope that you will enjoy reading the book now, and that it will serve as a useful reference for your future courses. Throughout each chapter, you will find questions that should help you to understand the mate- rial. The answers to these questions are in Appendix E at the back of the book. Unfortunately, we cannot give you answers to the exercises and programming projects that appear at the end of each chapter, even if you are not enrolled in a class. Only instructors who adopt the book can receive selected answers from the publisher. For help with these exercises and projects, you will have to contact your instructor. All of the Java code that appears in this book is available for download from the website at prenhall.com/carrano. An Overview vii An Overview Readers of this book should have completed a programming course, preferably in Java. Appendix A covers the essentials of Java that we assume readers will know. You can use this appendix as a review or as the basis for making the transition to Java from another programming language. You can also consult this appendix to learn about some of the new features of Java 5 that we use in this book. The book itself begins with the Introduction, which sets the stage for the data organizations that we will study. Chapter 1 thoroughly reviews classes and methods in Java. We follow this review with a discussion of composition, inheritance, and polymorphism in Chapter 2. This chapter also introduces generic data types, a new feature of Java that is important to our discussion. Chapter 3 presents object-oriented design, discusses Java interfaces, and provides an introduction to such design tools as the CRC card and the Unified Modeling Language. Chapters 4 through 7 introduce the list as an abstract data type. By dividing the material across several chapters, we clearly separate the specification, use, and implementation of the list. For example, Chapter 4 specifies the list and provides several examples of its use. Chapter 5 covers implementations that use arrays and vectors, while Chapters 6 and 7 discuss implementations that use chains of linked nodes. In a similar fashion, we separate specification from implementation throughout the book when we discuss various other ADTs. You can choose to cover the chapters that specify and use the ADTs and then later cover the chapters that implement them. Or you can cover the chapters as they appear, implementing each ADT right after studying its specification and use. A dependency chart appears later in this preface to help you plan your path through the book. Chapters 6 and 7 do more than simply implement the list. They show how to approach the implementation of a class. Chapter 6 begins the linked implementation of the ADT list by focusing on core methods. When defining a class, it is often useful to implement and test these core methods first and to leave definitions of the other methods for later. The next chapter completes the linked implementation of the list and explores some variations of that approach. Chapter 8 discusses iterators in the context of a list. It considers and implements Java’s iterator interfaces Iterator and ListIterator. This chapter also introduces the interface Iterable. Chapters 9 and 10 introduce the complexity of algorithms and recursion, two topics that we integrate into future chapters. For example, Chapters 11 and 12 discuss various sorting tech- niques and their relative complexities. We consider both iterative and recursive versions of these algorithms. The next two chapters, 13 and 14, return to the notion of a list. Chapter 13 discusses the sorted list, looking at two possible implementations and their efficiencies. Chapter 14 shows how to use the list as a base class for the sorted list and discusses the general design of a base class. Chapter 15 introduces mutable objects, immutable objects, and cloning. If a client can main- tain a reference to the data within an ADT, it can change that data without using the class’s public methods, if the data is mutable. We consider steps that you can take to prevent the client from doing so. Chapter 16 examines strategies for searching an array or chain in the context of a list or sorted list. This discussion is a good basis for Chapter 17, which covers the specification and use of the ADT dictionary. Chapter 18 presents implementations of the dictionary that are linked or that use arrays. Chapter 19 introduces hashing, and Chapter 20 uses it as a dictionary implementation. Chapter 21 discusses stacks, giving examples of their use and examining the relationship between stacks and recursion. Chapter 22 implements the stack using an array, a vector, and a chain. viii Preface Chapter 23 presents queues, deques, and priority queues, and Chapter 24 considers their imple- mentations. It is in this chapter that we discuss circularly linked and doubly linked chains. Chapter 25 discusses trees and their possible uses. Included among the several examples of trees is an introduction to the binary search tree and the heap. Chapter 26 considers implementa- tions of the binary tree and the general tree, and Chapter 27 focuses on the implementation of the binary search tree. Chapter 28 shows how to use an array to implement the heap. Chapter 29 intro- duces balanced search trees. Included in this chapter are the AVL, 2-3, 2-4, and red-black trees, as well as B-trees. Chapters 30 and 31 discuss graphs, looking at several applications and two implementations. Appendices A through D provide supplemental coverage of Java. As we mentioned earlier, Appendix A reviews Java up to but not including classes. This appendix also presents some new features of Java 5, including the Scanner class, enumerations, boxing and unboxing, and the for- each loop. Appendix B covers exception handling, and Appendix C discusses files. Appendix D considers programming style and comments. It introduces javadoc comments and defines the tags that we use in this book. Appendix E contains the answers to the questions that appear throughout each chapter. Instructor Resources We have prepared several supplements for instructors. Included are PowerPoint slides, laboratory materials, answers to selected exercises and programming projects, and a complete version of the Java code that appears in this book. While the book often leaves aspects of an implementation for readers to complete, the instructor’s version of the code is complete. Note that the code as it appears in the book is available on the website at prenhall.com/carrano. This website also contains addi- tional resources and any updates or corrections to the book. To obtain a copy of the supplements, instructors should contact their Prentice Hall sales repre- sentative. For the name and e-mail address of your sales representative, please visit the website prenhall.com/replocator or call Prentice Hall Faculty Services at 1-800-526-0485. Additional information on this book and other Prentice Hall products can be found on Prentice Hall’s home page at prenhall.com. Contact Us Your comments, suggestions, and corrections are always welcome. Please e-mail them to [email protected] Acknowledgments Thank you to the following reviewers for carefully reading the manuscript and making candid com- ments and suggestions that greatly improved the work: Harold Anderson—Marist College Razvan Andonie—Central Washington University Tom Blough—Rensselaer Polytechnic Institute Chris Brooks—University of San Francisco Adrienne Decker—University at Buffalo, SUNY Acknowledgments ix Henry Etlinger—Rochester Institute of Technology Derek Harter—Texas A&M University Timothy Henry—University of Rhode Island Robert Holloway—University of Wisconsin, Madison Charles Hoot—Oklahoma City University Teresa Leyk—Texas A&M University Robert McGlinn—Southern Illinois University, Carbondale Edward Medvid—Marymount University Charles Metzler—City College of San Francisco Daniel Zeng—University of Arizona Thank you once again to the reviewers of the first edition: David Boyd—Valdosta State University Dennis Brylow—Purdue University Michael Croswell—Industry trainer/consultant Matthew Dickerson—Middlebury College Robert Holloway—University of Wisconsin, Madison John Motil—California State University, Northridge Bina Ramamurthy—University at Buffalo, SUNY David Surma—Valparaiso University Special thanks go to my copy editor, Rebecca Pepper, who not only corrected my grammar but also ensured that my explanations are clear and correct. Charles Hoot was an invaluable resource providing additional exercises and projects and preparing some of the supplements for instructors. And thanks to Lianne Dunn, who designed the cover and drew all of the figures in the first edition of the book, and to Steve Armstrong, who prepared the PowerPoint slides. Thank you to all of the people at Prentice Hall who contributed to the development and pro- duction of this book and its supplements. In particular, I thank my editor, Tracy Dunkelberger, as well as Carole Snyder, Christianna Lee, Camille Trentacoste, Vince O’Brien, Xiaohong Zhu, Mike Giacobbe, and John Lovell. Their efforts are greatly appreciated. And special thanks to my produc- tion editor, Rose Kernan, who made all of our work tangible. For their contributions to both editions of this book, I especially thank my collaborator, Walt Savitch, and my former editors, Alan Apt and Toni Holm. Many others also helped with the first edition. They are Steve Armstrong, James Blanding, Charles Hoot, Brian Jepson, Patrick Lindner, Patty Roy, Ben Schomp, Heather Scott, Chirag Thakkar, Nate Walker, and Xiaohong Zhu. Other wonderful people have contributed in various ways. They are Doug McCreadie, Ted Emmott, Lorraine Berube, Marge White, Tom Manning, Jeff Barbeau, Joan Peckham, James Kow- alski, Ed Lamagna, Victor Fay-Wolfe, Gérard Baudet, Tim Henry, Lisa DiPippo, Jean-Yves Hervé, James Heltshe, and Bala Ravikumar. Thank you, everyone, for your expertise and good cheer. FRANK M. CARRANO

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.