teach yourself data structures and algorithms in 24 hours - robert lafore

548 2.6K 0
teach yourself data structures and algorithms in 24 hours - robert lafore

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

TeamLRN 201 West 103rd St., Indianapolis, Indiana, 46290 USA Robert Lafore Data Structures and Algorithms in 24 Hours TeachYourself 00 72316331 FM 10/31/02 6:54 AM Page i Sams Teach Yourself Data Structures and Algorithms in 24 Hours Copyright © 1999 by Sams Publishing All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photo- copying, recording, or otherwise, without written permission from the pub- lisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation 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 of the information contained herein. International Standard Book Number: 0-672-31633-1 Library of Congress Catalog Card Number: 98-83221 Printed in the United States of America First Printing: May 1999 01 00 99 4 3 2 1 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Warning and Disclaimer 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 an “as is” basis. The authors and the publisher shall have neither liability or 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- ROM or programs accompanying it. EXECUTIVE EDITOR Brian Gill DEVELOPMENT EDITOR Jeff Durham MANAGING EDITOR Jodi Jensen PROJECT EDITOR Tonya Simpson COPY EDITOR Mike Henry INDEXER Larry Sweazy PROOFREADERS Mona Brown Jill Mazurczyk TECHNICAL EDITOR Richard Wright SOFTWARE DEVELOPMENT SPECIALIST Dan Scherf INTERIOR DESIGN Gary Adair COVER DESIGN Aren Howell COPY WRITER Eric Borgert LAYOUT TECHNICIANS Brian Borders Susan Geiselman 00 72316331 FM 10/31/02 6:54 AM Page ii TeamLRN 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 iii 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 iv TeamLRN 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 v 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 Summary 48 Q&A 49 Workshop 49 Quiz 49 Exercise 50 HOUR 3ORDERED ARRAYS 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 OrdArray Class 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 vi Sams Teach Yourself Data Structures and Algorithms in 24 Hours 00 72316331 FM 10/31/02 6:54 AM Page vi TeamLRN Implementing the Person Class 64 Examining the classDataArray.cpp Program 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 HOUR 4THE BUBBLE SORT 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 HOUR 5THE INSERTION SORT 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 Contents vii 00 72316331 FM 10/31/02 6:54 AM Page vii 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 HOUR 6STACKS 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 StackX Class 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 HOUR 7QUEUES AND PRIORITY QUEUES 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 viii Sams Teach Yourself Data Structures and Algorithms in 24 Hours 00 72316331 FM 10/31/02 6:54 AM Page viii TeamLRN C++ Code for a Priority Queue 141 Efficiency of Priority Queues 143 Summary 143 Q&A 144 Workshop 144 Quiz 144 Exercise 144 HOUR 8LINKED LISTS 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 Link Class 150 The LinkList Class 151 The insertFirst() Member Function 151 The removeFirst() Member Function 153 The displayList() Member Function 153 The linkList.cpp Program 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 HOUR 9ABSTRACT DATA TYPES 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 Contents ix 00 72316331 FM 10/31/02 6:54 AM Page ix [...]... HOUR Page 11 1 Overview of Data Structures and Algorithms Welcome to Sams Teach Yourself Data Structures and Algorithms in 24 Hours! In this first hour you will ● Find out why you need to know about data structures and algorithms ● Discover what data structures and algorithms are ● Learn some terminology we’ll use in the rest of the book ● Review object-oriented programming As you start this book,... AM Page 6 Sams Teach Yourself Data Structures and Algorithms in 24 Hours balance the tree Hour 18, “Red-Black Tree Insertions,” shows how insertions are carried out using rotations and color changes In Hour 19, “ 2-3 -4 Trees,” we cover 2-3 -4 trees as an example of multiway trees A Workshop applet shows how they work Hour 20, “Implementing 2-3 -4 Trees,” presents C++ code for a 2-3 -4 tree and discusses... without involving this additional discipline, so we have deliberately de-emphasized software engineering in this book (We’ll discuss the relationship of data structures and algorithms to software engineering in Hour 1, “Overview of Data Structures and Alogorithms.”) Of course we use an object-oriented approach, and we discuss various aspects of objectoriented design as we go along, including a mini-tutorial... xii Sams Teach Yourself Data Structures and Algorithms in 24 Hours Q&A 278 Workshop 278 Quiz 278 Exercise 278 HOUR 14 IMPROVING QUICKSORT 279 Problems with Inversely Sorted Data 279 Median-of-Three Partitioning 280 Implementing Median-of-Three Partitioning in C++ 282 The quickSort2 Workshop Applet 286 Handling Small... xix About the Author Robert Lafore has degrees in Electrical Engineering and Mathematics, has worked as a systems analyst for the Lawrence Berkeley Laboratory, founded his own software company, and is a best-selling writer in the field of computer programming Some of his current titles are C++ Interactive Course, Object-Oriented Programming in C++, and Data Structures and Algorithms in Java by all Waite... This section is intended for teachers and others who want a quick overview of the contents of the book It assumes you’re already familiar with the topics and terms involved in a study of data structures and algorithms The first three hours are intended to ease the reader into data structures and algorithms as painlessly as possible TeamLRN 01 72316331 Intro 10/31/02 6:54 AM Page 5 Introduction Hour... class interface Searching, insertion, and deletion in arrays and ordered arrays are covered Linear searching and binary searching are explained Workshop applets demonstrate these algorithms with unordered and ordered arrays In Hour 4, “The Bubble Sort,” and Hour 5, “The Insertion Sort,” we introduce basic sorting concepts with two simple (but slow) sorting techniques Each sorting algorithm is demonstrated... programming 01 72316331 Intro 10/31/02 2 6:54 AM Page 2 Sams Teach Yourself Data Structures and Algorithms in 24 Hours What’s Different About This Book There are dozens of books on data structures and algorithms What’s different about this one? Three things: ● ● ● Our primary goal in writing this book is to make the topics we cover easy to understand Demonstration programs called Workshop applets bring... 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 computer program, even a simple one, uses data structures and algorithms For example, consider a program that prints address... Finally, for those of you who have not yet been exposed to object-oriented programming (OOP), we’ll briefly explain just enough about it to get you started Some Uses for Data Structures and Algorithms The subjects of this book are data structures and algorithms A data structure is an arrangement of data in a computer’s memory (or sometimes on a disk) Data structures include linked lists, stacks, binary . St., Indianapolis, Indiana, 46290 USA Robert Lafore Data Structures and Algorithms in 24 Hours TeachYourself 00 72316331 FM 10/31/02 6:54 AM Page i Sams Teach Yourself Data Structures and Algorithms. 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. 6:54 AM Page ix x Sams Teach Yourself Data Structures and Algorithms in 24 Hours Implementing a Queue Using a Linked List 175 Data Types and Abstraction 178 What We Mean by Data Types 178 What

Ngày đăng: 17/04/2014, 09:15

Từ khóa liên quan

Mục lục

  • Teach Yourself Data Structures and Algorithms in 24 Hours

  • Contents at a Glance

  • Table of Contents

  • About the Author

  • Acknowledgments

  • Introduction

  • PART I Introducing Data Structures and Algorithms

    • HOUR 1 Overview of Data Structures and Algorithms

    • HOUR 2 Arrays

    • HOUR 3 Ordered Arrays

    • HOUR 4 The Bubble Sort

    • HOUR 5 The Insertion Sort

    • PART II Abstract Data Types

      • HOUR 6 Stacks

      • HOUR 7 Queues and Priority Queues

      • HOUR 8 Linked Lists

      • HOUR 9 Abstract Data Types

      • HOUR 10 Specialized Lists

      • PART III Recursion and Quicksort

        • HOUR 11 Recursion

        • HOUR 12 Applied Recursion

        • HOUR 13 Quicksort

        • HOUR 14 Improving Quicksort

Tài liệu cùng người dùng

Tài liệu liên quan