Teach Yourself Data Structures and Algorithms In24 Hours
Total Page:16
File Type:pdf, Size:1020Kb
00 72316331 FM 10/31/02 6:54 AM Page i Robert Lafore Teach Yourself Data Structures and Algorithms in24 Hours 201 West 103rd St., Indianapolis, Indiana, 46290 USA 00 72316331 FM 10/31/02 6:54 AM Page ii Sams Teach Yourself Data Structures and EXECUTIVE EDITOR Algorithms in 24 Hours Brian Gill DEVELOPMENT EDITOR Copyright © 1999 by Sams Publishing Jeff Durham All rights reserved. No part of this book shall be reproduced, stored in a MANAGING EDITOR retrieval system, or transmitted by any means, electronic, mechanical, photo- Jodi Jensen copying, recording, or otherwise, without written permission from the pub- lisher. No patent liability is assumed with respect to the use of the information PROJECT EDITOR contained herein. Although every precaution has been taken in the preparation Tonya Simpson of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use COPY EDITOR of the information contained herein. Mike Henry International Standard Book Number: 0-672-31633-1 INDEXER Larry Sweazy Library of Congress Catalog Card Number: 98-83221 PROOFREADERS Printed in the United States of America Mona Brown Jill Mazurczyk First Printing: May 1999 TECHNICAL EDITOR 01 00 99 4 3 2 1 Richard Wright Trademarks SOFTWARE DEVELOPMENT All terms mentioned in this book that are known to be trademarks or service SPECIALIST marks have been appropriately capitalized. Sams Publishing cannot attest to Dan Scherf the accuracy of this information. Use of a term in this book should not be INTERIOR DESIGN regarded as affecting the validity of any trademark or service mark. Gary Adair Warning and Disclaimer COVER DESIGN Every effort has been made to make this book as complete and as accurate as Aren Howell possible, but no warranty or fitness is implied. The information provided is on COPY WRITER an “as is” basis. The authors and the publisher shall have neither liability or Eric Borgert responsibility to any person or entity with respect to any loss or damages aris- ing from the information contained in this book or from the use of the CD- LAYOUT TECHNICIANS ROM or programs accompanying it. Brian Borders Susan Geiselman 00 72316331 FM 10/31/02 6:54 AM Page iii Contents at a Glance Introduction 1 PART IINTRODUCING DATA STRUCTURES AND ALGORITHMS 9 Hour 1 Overview of Data Structures and Algorithms 11 2Arrays 31 3Ordered Arrays 51 4The Bubble Sort 75 5The Insertion Sort 89 PART II ABSTRACT DATA TYPES 105 Hour 6 Stacks 107 7 Queues and Priority Queues 125 8 Linked Lists 145 9 Abstract Data Types 165 10 Specialized Lists 183 PART III RECURSION AND QUICKSORT 205 Hour 11 Recursion 207 12 Applied Recursion 233 13 Quicksort 257 14 Improving Quicksort 279 PART IV TREES 295 Hour 15 Binary Trees 297 16 Traversing Binary Trees 317 17 Red-Black Trees 337 18 Red-Black Tree Insertions 359 19 2-3-4 Trees 379 20 Implementing 2-3-4 Trees 395 00 72316331 FM 10/31/02 6:54 AM Page iv PART VHASH TABLES 415 Hour 21 Hash Tables 417 22 Quadratic Probing 441 23 Separate Chaining 457 24 When to Use What 475 PART VI APPENDIXES 487 Appendix A Quiz Answers 489 BHow to Run the Workshop Applets and Sample Programs 505 C Further Reading 509 Index 513 00 72316331 FM 10/31/02 6:54 AM Page v Table of Contents INTRODUCTION 1 What This Book Is About ........................................................................................1 What’s Different About This Book..........................................................................2 Easy to Understand ............................................................................................2 Workshop Applets ..............................................................................................2 C++ Sample Code ..............................................................................................3 Who This Book Is For ............................................................................................3 What You Need to Know Before You Read This Book ..........................................4 The Software You Need to Use This Book..............................................................4 How This Book Is Organized ..................................................................................4 Enjoy Yourself! ........................................................................................................6 Conventions Used in This Book ..............................................................................6 PART IINTRODUCING DATA STRUCTURES AND ALGORITHMS 9 HOUR 1OVERVIEW OF DATA STRUCTURES AND ALGORITHMS 11 Some Uses for Data Structures and Algorithms....................................................12 Real-World Data Storage..................................................................................12 Programmer’s Tools..........................................................................................14 Real-World Modeling ......................................................................................14 Overview of Data Structures ................................................................................14 Overview of Algorithms ........................................................................................15 Some Initial Definitions ........................................................................................16 Datafile ............................................................................................................16 Record ..............................................................................................................16 Field..................................................................................................................16 Key....................................................................................................................16 Search Key........................................................................................................17 A Quick Introduction to Object-Oriented Programming ......................................18 Problems with Procedural Languages..............................................................18 Objects in a Nutshell ........................................................................................19 A Runnable Object-Oriented Program ............................................................21 Inheritance and Polymorphism ........................................................................24 New C++ Features ................................................................................................25 The string Class..............................................................................................25 The vector Class..............................................................................................26 Software Engineering ............................................................................................26 Summary................................................................................................................27 00 72316331 FM 10/31/02 6:54 AM Page vi vi Sams Teach Yourself Data Structures and Algorithms in 24 Hours Q&A ......................................................................................................................28 Workshop ..............................................................................................................28 Quiz ..................................................................................................................28 Exercise ............................................................................................................29 HOUR 2ARRAYS 31 The Array Workshop Applet..................................................................................31 Deletion ............................................................................................................34 The Duplicates Problem ..................................................................................35 Slow Array Algorithms ....................................................................................37 An Array Example ................................................................................................37 Inserting a New Item ........................................................................................39 Searching for an Item ......................................................................................39 Deleting an Item ..............................................................................................39 Displaying the Array Contents ........................................................................40 Program Organization ......................................................................................40 Dividing a Program into Classes ..........................................................................40 The LowArray Class and main() ......................................................................42 Class Interfaces......................................................................................................43 Making main()’s Job Easier ............................................................................43 Who’s Responsible for What?..........................................................................44 The highArray.cpp Example ..........................................................................44 The User’s Life Made Easier............................................................................48 Abstraction........................................................................................................48