ebook img

Sams Teach Yourself Data Structures and Algorithms in 24 Hours PDF

548 Pages·1999·11.76 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 Sams Teach Yourself Data Structures and Algorithms in 24 Hours

00 72316331 FM 10/31/02 6:54 AM Page i Robert Lafore Teach Yourself Data Structures and Algorithms 24 in 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 EXECUTIVEEDITOR Algorithms in 24 Hours Brian Gill Copyright ©1999 by Sams Publishing DEVELOPMENTEDITOR Jeff Durham All rights reserved. No part of this book shall be reproduced,stored in a MANAGINGEDITOR 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 PROJECTEDITOR 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 COPYEDITOR 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 TECHNICALEDITOR 01 00 99 4 3 2 1 Richard Wright Trademarks SOFTWAREDEVELOPMENT 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 INTERIORDESIGN regarded as affecting the validity of any trademark or service mark. Gary Adair Warning and Disclaimer COVERDESIGN Aren Howell Every effort has been made to make this book as complete and as accurate as possible,but no warranty or fitness is implied. The information provided is on COPYWRITER 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- LAYOUTTECHNICIANS ROM or programs accompanying it. Brian Borders Susan Geiselman TeamLRN 00 72316331 FM 10/31/02 6:54 AM Page iii Contents at a Glance Introduction 1 PART I INTRODUCING DATA STRUCTURES AND ALGORITHMS 9 Hour 1 Overview of Data Structures and Algorithms 11 2 Arrays 31 3 Ordered Arrays 51 4 The Bubble Sort 75 5 The 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 V HASH 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 B How to Run the Workshop Applets and Sample Programs 505 C Further Reading 509 Index 513 TeamLRN 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 I INTRODUCING DATA STRUCTURES AND ALGORITHMS 9 HOUR1 OVERVIEWOFDATASTRUCTURESANDALGORITHMS 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 stringClass..............................................................................................25 The vectorClass..............................................................................................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 HOUR2 ARRAYS 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 LowArrayClass and main() ......................................................................42 Class Interfaces......................................................................................................43 Making main()’s Job Easier ............................................................................43 Who’s Responsible for What?..........................................................................44 The highArray.cppExample ..........................................................................44 The User’s Life Made Easier............................................................................48 Abstraction........................................................................................................48 Summary................................................................................................................48 Q&A......................................................................................................................49 Workshop ..............................................................................................................49 Quiz..................................................................................................................49 Exercise............................................................................................................50 HOUR3 ORDEREDARRAYS 51 The Ordered Workshop Applet..............................................................................51 Demonstrating the Linear Search ....................................................................52 Demonstrating the Binary Search....................................................................53 C++ Code for an Ordered Array............................................................................55 Conducting a Binary Search with the find()Member Function....................56 Investigating the OrdArrayClass......................................................................57 The Advantages of Using Ordered Arrays ......................................................60 Logarithms ............................................................................................................61 An Equation Relating Range Size and Number of Steps................................62 The Opposite of Raising Two to a Power........................................................63 Storing Objects......................................................................................................64 TeamLRN 00 72316331 FM 10/31/02 6:54 AM Page vii Contents vii Implementing the PersonClass........................................................................64 Examining the classDataArray.cppProgram ................................................65 Big O Notation......................................................................................................69 Inserting into an Unordered Array:Constant ..................................................69 Linear Searching:Proportional to N................................................................69 Binary Searching:Proportional to log(N)........................................................70 Eliminating the Constant K..............................................................................70 Why Not Use Arrays for Everything? ..................................................................72 Summary................................................................................................................72 Q&A......................................................................................................................72 Workshop ..............................................................................................................73 Quiz..................................................................................................................73 Exercise............................................................................................................73 HOUR4 THEBUBBLESORT 75 Sorting....................................................................................................................75 Inventing Your Own Sorting Algorithm................................................................76 Bubble-Sorting the Baseball Players ....................................................................77 The bubbleSort Workshop Applet..........................................................................79 Sorting at Full Speed with the Run Button......................................................80 Starting a New Sort with the New Button........................................................80 Single-Stepping with the Step Button..............................................................81 Changing the Array Size with the Size Button................................................81 Fixing the Picture with the Draw Button ........................................................82 Implementing C++ Code for a Bubble Sort..........................................................83 Invariants................................................................................................................86 Efficiency of the Bubble Sort................................................................................86 Summary................................................................................................................87 Q&A......................................................................................................................87 Workshop ..............................................................................................................88 Quiz..................................................................................................................88 Exercise............................................................................................................88 HOUR5 THEINSERTIONSORT 89 Insertion Sort on the Baseball Players..................................................................90 Demonstrating Partial Sorting..........................................................................90 Inserting the Marked Player in the Appropriate Location ..............................90 The insertSort Workshop Applet............................................................................92 Implementing the Insertion Sort in C++................................................................94 Invariants in the Insertion Sort ........................................................................97 Efficiency of the Insertion Sort..............................................................................97 Sorting Objects......................................................................................................98 Implementing C++ Code to Sort Objects........................................................98 00 72316331 FM 10/31/02 6:54 AM Page viii viii Sams Teach Yourself Data Structures and Algorithms in 24 Hours Another Feature of Sorting Algorithms:Stability ..............................................101 Comparing the Simple Sorts................................................................................102 Summary..............................................................................................................102 Q&A....................................................................................................................103 Workshop ............................................................................................................103 Quiz................................................................................................................103 Exercise..........................................................................................................103 PART II ABSTRACT DATA TYPES 105 HOUR6 STACKS 107 A Different Way to Think About Data Structure................................................107 Uses for Stacks and Queues:Programmer’s Tools........................................108 Stacks and Queues:Restricted Access to Data..............................................108 Stacks and Queues:More Abstract................................................................108 Understanding Stacks..........................................................................................109 Two Real-World Stack Analogies..................................................................109 The Stack Workshop Applet ..........................................................................111 Implementing a Stack in C++..............................................................................113 StackXClass Member Functions....................................................................114 Error Handling................................................................................................116 Stack Example 1:Reversing a Word ..................................................................116 Stack Example 2:Delimiter Matching................................................................118 Opening Delimiters on the Stack ..................................................................119 C++ Code for brackets.cpp..........................................................................120 Using the Stack as a Conceptual Aid ............................................................123 Efficiency of Stacks ............................................................................................123 Summary..............................................................................................................123 Q&A....................................................................................................................124 Workshop ............................................................................................................124 Quiz................................................................................................................124 Exercise..........................................................................................................124 HOUR7 QUEUESANDPRIORITYQUEUES 125 Queues..................................................................................................................125 The Queue Workshop Applet ........................................................................126 A Circular Queue............................................................................................130 C++ Code for a Queue ..................................................................................132 Efficiency of Queues......................................................................................137 Priority Queues....................................................................................................137 The PriorityQ Workshop Applet....................................................................138 TeamLRN 00 72316331 FM 10/31/02 6:54 AM Page ix Contents ix C++ Code for a Priority Queue......................................................................141 Efficiency of Priority Queues ........................................................................143 Summary..............................................................................................................143 Q&A....................................................................................................................144 Workshop ............................................................................................................144 Quiz................................................................................................................144 Exercise..........................................................................................................144 HOUR8 LINKEDLISTS 145 Understanding Links............................................................................................146 Structure Defined by Relationship,Not Position ..........................................147 The LinkList Workshop Applet ..........................................................................147 Inserting a New Link......................................................................................147 Using the Find Button....................................................................................148 Using the Del Button......................................................................................149 Creating Unsorted and Sorted Lists ..............................................................149 Implementing a Simple Linked List....................................................................149 The LinkClass................................................................................................150 The LinkListClass........................................................................................151 The insertFirst()Member Function..........................................................151 The removeFirst()Member Function..........................................................153 The displayList()Member Function..........................................................153 The linkList.cppProgram............................................................................155 Finding and Removing Specified Links..............................................................157 The find()Member Function........................................................................160 The remove()Member Function....................................................................161 Avoiding Memory Leaks................................................................................162 The Efficiency of Linked Lists............................................................................162 Summary..............................................................................................................163 Q&A....................................................................................................................163 Workshop ............................................................................................................164 Quiz................................................................................................................164 Exercise..........................................................................................................164 HOUR9 ABSTRACTDATATYPES 165 A Stack Implemented By a Linked List..............................................................166 Implementing push()and pop()....................................................................166 Implementing a Stack Based on a Linked List..............................................167 Focusing on Class Relationships....................................................................170 Double-Ended Lists ............................................................................................170 Accessing Both Ends of a List ......................................................................170 Implementing a Double-Ended List ..............................................................171 Pointers to Both Ends of the List ..................................................................174 Insertion and Deletion Routines ....................................................................174

Description:
This book is about data structures and algorithms as used in computer programming. Data structures are ways in which data is arranged in your computer’s memory (or stored on disk). Algorithms are the procedures a software program uses to manipulate the data in these structures. Almost every comput
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.