1. Trang chủ
  2. » Giáo án - Bài giảng

teach yourself data structures and algorithms in 24 hours lafore 1999 05Cấu trúc dữ liệu và giải thuật

548 36 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 548
Dung lượng 4,36 MB

Nội dung

CuuDuongThanCong.com TeamLRN 00 72316331 FM 10/31/02 6:54 AM Page i Robert Lafore Teach Yourself Data Structures and Algorithms in 24 Hours 201 West 103rd St., Indianapolis, Indiana, 46290 USA CuuDuongThanCong.com 00 72316331 FM 10/31/02 6:54 AM Page ii 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, photocopying, recording, or otherwise, without written permission from the publisher 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 EXECUTIVE EDITOR Brian Gill DEVELOPMENT EDITOR Jeff Durham MANAGING EDITOR Jodi Jensen PROJECT EDITOR Tonya Simpson COPY EDITOR Mike Henry INDEXER Larry Sweazy Library of Congress Catalog Card Number: 98-83221 Printed in the United States of America Mona Brown Jill Mazurczyk First Printing: May 1999 01 00 99 TECHNICAL EDITOR Richard Wright 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 SOFTWARE DEVELOPMENT SPECIALIST Dan Scherf INTERIOR DESIGN Gary Adair COVER DESIGN 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 arising from the information contained in this book or from the use of the CDROM or programs accompanying it TeamLRN CuuDuongThanCong.com PROOFREADERS Aren Howell COPY WRITER Eric Borgert LAYOUT TECHNICIANS Brian Borders Susan Geiselman 00 72316331 FM 10/31/02 6:54 AM Page iii Contents at a Glance Introduction PART I INTRODUCING DATA STRUCTURES Hour 1 AND ALGORITHMS Overview of Data Structures and Algorithms 11 Arrays 31 Ordered Arrays 51 The Bubble Sort 75 The Insertion Sort 89 PART II ABSTRACT DATA TYPES 105 Hour Stacks 107 Queues and Priority Queues 125 Linked Lists 145 Abstract Data Types 165 Specialized Lists 183 10 PART III RECURSION Hour 11 AND QUICKSORT 205 Recursion 207 12 Applied Recursion 233 13 Quicksort 257 14 Improving Quicksort 279 PART IV TREES Hour 15 295 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 CuuDuongThanCong.com 00 72316331 FM 10/31/02 6:54 AM Page iv PART V HASH TABLES Hour 21 415 Hash Tables 417 22 Quadratic Probing 441 23 Separate Chaining 457 24 When to Use What 475 PART VI APPENDIXES Appendix A 487 Quiz Answers 489 B How to Run the Workshop Applets and Sample Programs 505 C Further Reading 509 Index 513 TeamLRN CuuDuongThanCong.com 00 72316331 FM 10/31/02 6:54 AM Page v Table of Contents INTRODUCTION What This Book Is About What’s Different About This Book Easy to Understand Workshop Applets C++ Sample Code Who This Book Is For What You Need to Know Before You Read This Book The Software You Need to Use This Book How This Book Is Organized Enjoy Yourself! Conventions Used in This Book PART I INTRODUCING DATA STRUCTURES HOUR OVERVIEW OF DATA STRUCTURES AND AND ALGORITHMS 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 CuuDuongThanCong.com 00 72316331 FM vi 10/31/02 6:54 AM Page vi Sams Teach Yourself Data Structures and Algorithms in 24 Hours Q&A 28 Workshop 28 Quiz 28 Exercise 29 HOUR 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 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 ORDERED 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 TeamLRN CuuDuongThanCong.com 00 72316331 FM 10/31/02 6:54 AM Page vii Contents vii 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 THE 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 THE 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 CuuDuongThanCong.com 00 72316331 FM viii 10/31/02 6:54 AM Page 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 HOUR 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 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 QUEUES 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 TeamLRN CuuDuongThanCong.com 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 HOUR LINKED 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 ABSTRACT 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 CuuDuongThanCong.com 34 72316331 appC 10/31/02 6:57 AM Page 511 Further Reading 511 C++ Distilled, by Ira Pohl (Addison Wesley, 1997) is a short book that summarizes C++ syntax It’s handy if you’ve forgotten how to use some language feature and need to look it up in a hurry After you’ve mastered the fundamentals of C++ syntax, you can learn a great deal about how to use the language from Effective C++ by Scott Meyers (Addison Wesley, 1997) and the sequel More Effective C++ (1996) The Java programming language is similar to C++ One important difference is that it dispenses with pointers, which makes it easier to learn (but not quite as flexible) If you’re interested in Java programming, Java How to Program by H M Deitel and P J Deitel (Prentice Hall, 1997) is a good textbook, complete with many exercises Core Java 1.2 by Cay S Horstmann and Gary Cornell (Prentice Hall, 1998) is a multivolume series that covers advanced Java topics in depth Object-Oriented Design and Software Engineering For an easy, non-academic introduction- to software engineering, try The Object Primer: The Application Developer’s Guide to Object-Orientation by Scott W Ambler (Sigs Books, 1998) This short book explains in plain language how to design a large software application The title is a bit of a misnomer; it goes way beyond mere OO concepts A classic in the field of OOD is Object-Oriented Analysis and Design with Applications by Grady Booch (Addison Wesley, 1994) The author is one of the pioneers in this field and the creator of the Booch notation for depicting class relationships This book isn’t easy for beginners, but is essential for more advanced readers An early book on OOD is The Mythical Man-Month by Frederick- P Brooks, Jr (Addison Wesley, 1975, reprinted in 1995), which explains in a very clear and literate way some of the reasons why good software design is necessary It is said to have sold more copies than any other computer book Mitchell Waite Signature Series: Object-Oriented Design in Java by Stephen Gilbert and Bill McCarty (Waite Group Press, 1998) is an unusually accessible introduction to OOD and software engineering Other good texts on OOD are An Introduction to Object-Oriented Programming, by Timothy Budd (Addison Wesley, 1996); Object-Oriented Design Heuristics, by Arthur J Riel, (Addison Wesley, 1996); and Design Patterns: Elements of Reusable ObjectOriented Software, by Erich Gamma, et al (Addison Wesley, 1995) CuuDuongThanCong.com C 34 72316331 appC 10/31/02 6:57 AM Page 512 512 Appendix C Programming Style Books on other aspects of good programming: Programming Pearls by Jon- Bentley (Addison Wesley, 1986) was written before OOP but is nevertheless stuffed full of great advice for the programmer Much of the material deals with data structures and algorithms Writing Solid Code, by Steve Maguire (Microsoft Press, 1993) and Code Complete by Steve McConnell (Microsoft Press, 1993) contain good ideas for software development and coding and will help you develop good programming practices TeamLRN CuuDuongThanCong.com 35 72316331 mcp.com ad 10/31/02 6:57 AM Page 525 Get the best Resource Centers Books & Software Personal Bookshelf WWW Yellow Pages information and learn about latest developments in: Online Learning ▼ Special Offers Choose the online ebooks that you can view from your personal workspace on our site About MCP Site Map Site Search Industry News ■ Design ■ Graphics and Multimedia Product Support ■ Enterprise Computing and DBMS Turn to the Authoritative Encyclopedia of Computing You'll find over 150 full text books online, hundreds of shareware/freeware applications, online computing classes and 10 computing resource centers full of expert advice from the editors and publishers of: • Adobe Press • BradyGAMES • Cisco Press • Hayden Books • Lycos Press • New Riders • Que • Que Education & Training • Sams Publishing • Waite Group Press • Ziff-Davis Press ■ General Internet Information ■ Operating Systems ■ Networking and Hardware ■ PC and Video Gaming ■ Productivity Applications ■ Programming ■ Web Programming and Administration The Authoritative Encyclopedia of Computing When you’re looking for computing information, consult the authority The Authoritative Encyclopedia of Computing at mcp.com CuuDuongThanCong.com ■ Web Publishing 35 72316331 mcp.com ad 10/31/02 6:57 AM Page 526 TeamLRN CuuDuongThanCong.com 36 72316331 index 10/31/02 6:57 AM Page 513 INDEX Symbols 2-3-4 trees, 379-382 C++, 395-405 efficiency, 409-412 root splits, 385-386 Tree234 Workshop applet, 387-392 2-node, 381 234Tree Workshop applet, 388-389 3-node, 381 3-node trees, 319-320 4-node, 381 A Abstract Data Types, see ADTs abstraction, 48 access modifiers, 24 CuuDuongThanCong.com accessing double-ended lists, 170 items, 109 object member functions, 21 restricted queues/stacks, 108 ADTs (Abstract Data Types), 166-181 algebraic expressions, trees, 322 algorithms defined, 12 divide-and conquer, 228 further reading, 509 overview, 15 partitioning, 262-264 quicksort, 265-272 recursive, 236-238 sorting, 101 speed, 477 The Towers of Hanoi, 236, 238 anagramming, see anagrams anagrams, 216-222 answers, quiz, 489-504 applets Array, 37 Binary Tree Workshop, 304-314 BubbleSort Workshop, 79-83 Hash Workshop, 427-432 HashChain Workshop, 458-461 HashDouble, 442-451 insertsort Workshop, 92-93 LinkList Workshop, 147-149, 184-189 mergesort Workshop, 246-251 Ordered Workshop, 51-54 36 72316331 index 10/31/02 514 6:57 AM Page 514 applets Partition Workshop, 258-259 PriorityQ Workshop, 138-141 Queue Workshop, 126-137 quickSort1 Workshop, 272 277 quickSort2 Workshop, 286 RBTree Workshop, 343-350 Stack Workshop, 111-112 Towers Workshop, 234-236, 238 Workshops arrays, 31-35 duplicate keys, 35-37 executing, 505-507 opening, 506 traversing trees, 320-322 applying Del button, 149 Find button, 148 Arguments, recQuickSort(), 266 Array applet, 37 array.cpp, organization, 40 arrays, 478 contents, 40 deletion, 31-35 duplicate keys, 35-37 example, 37-39 expanding, 454 impractical uses, 72 insertion, 31-35 items, 39 mergesort, 240-251 ordered, 51-54 binary trees, 298 C++ code, 55-60 rear arrows, 130 searching, 31-35 sizes, 81-82 Workshop applet, 31-37 AVL trees, 375 avoiding memory leaks, 162 B B-trees, storing, 412 balanced trees, 338-339, 479 2-3-4, 379-382 implementing, 395-405 inserting items, 383 node splits, 384-387 root splits, 385-386 searching items, 383 Tree234 Workshop applet, 387-392 AVL, 375 multiway, 375 O(log N), 340 balancing trees, 351-355 base classes, 24 Big O Notations, 69-71 binary search trees, 328-335 binary searches, 51-54, 70 C++ code, 55-60 divide-and-conquer algorithms, 228 maximum/minimum values, 324-325 recursion, 223-228 TeamLRN CuuDuongThanCong.com Binary Tree Workshop applet, 304-314 binary trees, 297-327 black height, 342 color flips, 361 Big O Notations, 69-70 bottom-up insertion, 360 see also inserting boundaries, finding, 266 brackets.cpp, C++ code, 120-122 bubble sorts, 75-79 C++ code, 83-86 efficiency, 86-87 Workshop applet, 79-83 buckets HashChain Workshop applet, 461 see also arrays buttons Draw, 82-83 New, 111 priority queues, 140-141 sorting, 80 Peek, 112 priority queues, 140-141 Pop, 112 Push, 111 Run, 80 Size, 81-82 Step, 81 C C++ 2-3-4 trees, 395-405 anagramming, 220-222 binary search trees, 328-335 36 72316331 index 10/31/02 6:57 AM Page 515 datafiles binary trees, 306-308 code, 120-122 double hashing, 446-451 hash tables, 432-438 insertion function, 185-186 insertion sorts, 94-95, 97 median-of-three partitioning, 282-286 member functions, 19 mergesort, 247-251 new features, 25 nodes, 311-314 objects creating, 20 sorting, 98-100 ordered arrays, 55-60 queues, 132-137 separate chaining, 461-466 stacks, 113-116 string class, 25-26 traversing, 318-319 vector class, 26 Cardfile programs, 13 cells empty, 35 mergesort, 251-252 child trees, 302 classes, 24 see also derived classes circular queues, 130-132 classDataArray.cpp, 65-68 classes 2-3-4 trees (C++), 396-397 base, 24 derived, 24 CuuDuongThanCong.com hash.cpp, 436 interfaces, 43-48 Link, 150-151 LinkList, 151 member functions, 114-115 Node, 306-307 OOP, 20 OrdArray, 57-60 Person, 64-68 programs, 40-43 queues, 135-137 relationship, 170 self-referential, 146 stack, 167-170 string, 25-26 Tree, 307 vector, 26 clicking nodes, red-black trees, 343 closing, see terminating clustering hash tables, 432 primary, 444 codes C++ (bubbleSort), 83-86 ordered arrays, 55-60 separate chaining, 461-466 traversing, 318-319 collisions hashing, 426-427 separate chaining, 458-461 color flips, 348 black heights, 361 red-black trees, 361-372 red-red conflicts, 362 root flipping, 363 colored nodes, 341 515 columns, finding, 210-211 commands File menu, Open, 506 Search menu, Find, 13 comparing general purpose data structures, 480 mergesort, 252-253 simple sorts, 102 special purpose data structures, 483 compiling sample programs, 508 computations, hash functions, 469-470 conflicts, red-red, 362 constant K, eliminating, 70-71 constants, inserting, 69 constructors, 23 contents, viewing, 40 converting words to numbers, 420 copies, mergesort, 251-252 CPU performance, 477 D data structures defined, 12 further reading, 509 general purpose, 476-480 overview, 14-15 sorting, 483-484 special purpose, 481-483 datafiles, 16 36 72316331 index 10/31/02 516 6:57 AM Page 516 Del button Del button linked lists, 149 RBTree Workshop applet, 344 deleting arrays, 31-35 duplicate keys, 36-37 HashChain Workshop applet, 460 items arrays, 39 priority queues, 139 Queue workshop applet, 127-128 links, 149-160 nodes, 314 red-black trees, 373 deletion routines, doubleended lists, 174 delicate code, 263 delimiters matching, 118-119 stacks, 119-120 deleting items, 35 links, 154 deques, 137 derived classes, 24 descending-priority queues, 139 design tools, ADTs, 180 displaying lists, 194 displayList() member function, 153-154 divide-and-conquer algorithms, 228 dividing programs, 40-43 dot operators, 21 double hashing, 444-454 double rotation, 367 double-ended lists, 170-174 double-ended queues, 137 see also deque doubly linked lists, 192-202 Draw button, 82-83 duplicate keys, 35-36 deleting, 36-37 inserting, 36 red-black trees, 342 trees, 327 duplicates, HashChain Workshop applet, 460 E efficiency, 123 2-3-4 trees, 409, 411-412 insertion sorts, 97-98 linked lists, 162 open addressing, 451-452 red-black trees, 374 separate chaining, 466-467 eliminating constant K, 70-71 embedding items, 146-147 empty cells, 35 empty errors, 129 engineering software, 26 errors empty, 129 full, 129 stacks, 116 see also troubleshooting TeamLRN CuuDuongThanCong.com examples arrays, 37, 39 highArray.cpp, 44-47 stacks, 116-118 executing sample programs, 508 Workshop applets, 505-507 expanding arrays, 454 F fields, defined, 16 FIFO (first to be removed), 125 File menu commands, Open, 506 files, multiple class, 507 Fill button, 234Tree Workshop applet, 388 Find button 234Tree Workshop applet, 388 linked lists, 148-149 Find command (Search menu), 13 find() binary searches, 56-57 find() member function, 160 finding boundaries, 266 links, 148-149 nodes, 308-310 nth terms, 208-209 remaining columns, 210-211 see also searching first to be removed, see FIFO 36 72316331 index 10/31/02 6:57 AM Page 517 insertion sorts Flip button, RBTree Workshop applet, 344 flipping roots, 363 full errors, 129 functions class functions, 114-115 hash, 469-473 insert() member, 134 insertion (C++), 185-186 main(), 23, 308, 398 member accessing, 21 C++, 19 constructors, 23 displayList(), 153-154 find(), 160 insertFirst(), 151-153 remove(), 161 removeFirst(), 153 peek() member, 134 recursive, 215-216 remove() member, 134 triangle(), 211, 213, 215 further reading, 509-512 H handed variations, inserting, 363 hash functions, 469-473 hash tables, 417-438, 479 Hash Workshop applet, 427-432 hash.cpp, classes, 436 HashChain Workshop applet, 459-461 HashDouble applet, 442-451 hashing, 417-425 collisions, 426-427 double, 444-445 linear probing, 427 strings, 471 see also hash tables hierarchical file structures, 303 highArray.cpp, 44-47 holes, see empty cells I G general purpose data structures, 476-480 see also data structures generating quadratic probes, 443-444 Gordon Moore, 477 see also CPU performance; Moore's Law guessing-a-number, 53-54 CuuDuongThanCong.com implementing 2-3-4 trees, 395-405 C++ insertion sorts, 94-97 stacks, 113-116 double-ended lists, 171-173 Person class, 64-68 queue classes, 135-137 queues, 175-178 simple linked lists, 149-157 sorted lists, 186-189 stacks, 166-169 517 infix notations, 322 inheritance, 24 inorder traversal, 317-320 see also traversing Ins button 234Tree Workshop applet, 389 RBTree Workshop applet, 344 insert() member functions, 134 insertFirst() member function, 151-153 inserting 2-3-4 trees (C++), 397 arrays, 31-35 color flips, 361-372 duplicate keys, 36 items 2-3-4 trees, 383 arrays, 39 HashChain Workshop applet, 459 links, 194 new items, 127 priority queues, 139 new links, 147 nodes binary trees, 311-314 red-black trees, 345-346 objects, 298 separate chaining, 467 sorts, 189-192 unordered arrays, 69 insertion function (C++), 185-186 insertion routines, doubleended lists, 174 insertion sorts, 90-91, 189-192, 286-290 36 72316331 index 10/31/02 518 6:57 AM Page 518 inserting sorted lists inserting sorted lists, 184-185 insertsort Workshop applet, 92-93 instances, see objects instantiating objects, 21 interfaces ADTs, 180 classes, 43-48 LowArray, 43-48 internally sorting, 90-91 see also partial sorts invariants, 86 items 2-3-4 trees, 383 C++, 397 arrays, 39 deleting, 35 HashChain Workshop applet, 459-460 linked lists, 146-147 priority queues, 139 Queue Workshop applet, 127-128 searching, 34 stacks, 109 K key values, searching, 148-149 keys defined, 16-17 duplicate, 35-36 HashChain Workshop applet, 460 search, 17 trees, 302 L languages, further reading, 510 last item removed, see LIFO leaf (trees), 302 left child, 303 levels (trees), 302 libraries, 478 LIFO (last item removed), 125 linear probing, 452 C++, 432, 434-438 Hash Workshop applet, 427-432 hashing, 427 linear searches, 52-69 Link class, 150-151 linked lists, 146-147, 478 binary trees, 298 Del button, 149 doubly, 192-202 efficiency, 162 Find button, 148-149 LinkList Workshop applet, 147-149 memory leaks, 162 new links, 147 queues, 170-178 simple, 149-157 stacks, 166-170 LinkList class, 151 LinkList Workshop applet, 147-149, 184-189 linkList.cpp program, 155-157 linkList2.cpp, 157-160 links deleting, 149-160 inserting, 194 TeamLRN CuuDuongThanCong.com linked lists, 147 searching, 148-160 LinkStack, 167-169 lists ADTs, 180 double-ended, 170-174 linked, 146-147, 478 avoiding memory leaks, 162 binary trees, 298 doubly, 192-202 efficiency, 162 implementing stacks, 166-169 LinkList Workshop applet, 147-149 stacks, 170 sorted, 183-189 creating, 149 data, 184-185 inserting, 189-192 unsorted, 149 viewing, 194 load factors, HashChain Workshop applet, 460 logarithms, 61-63 loops nth terms, 208-209 recursion, 223-228 lowArray.cpp, 42-48 M main(), 42-48 main() function, 23, 308 2-3-4 trees, 398 mapping keys, 469 mathematical inductions, recursive functions, 216 maximum values, binary searches, 324-325 36 72316331 index 10/31/02 6:57 AM Page 519 partial sorts median-of-three partitioning, 280-286 member functions C++, 19 classes, 114-115 displayList(), 153-154 find(), 56-57, 160 insert(), 134 insertFirst(), 151-153 object, 21 peek(), 134 remove(), 134, 161 removeFirst(), 153 memory leaks, avoiding, 162 mergesorts, 240-252 Workshop Applet, 246-251 messages, RBTree Workshop applet, 345 minimum values, binary searches, 324-325 modeling, real-world, 14 modifiers, accessing, 24 modifying array sizes, 81-82 modulo operators, separate chaining, 471-473 Moore's Law, 477 moving subtrees, 235, 354 multiple class files, 507 multiway trees, 375 N new button, 111 New button creating queues, 129 priority queues, 140-141 sorting, 80 CuuDuongThanCong.com new features, C++, 25 Node class, 306-307 nodes, 302 2-3-4 trees (C++), 397 binary trees, 326-327 deleting, 314 finding, 308-310 inserting, 311-314 colored, 341 RBTree Workshop applet, 345-346 red-black trees clicking, 343 inserting, 360-374 splits, 384-387 viewing, 390-391 non-random keys, separate chaining, 469-470 nth terms, finding, 208-209 null child, 342 number of steps, 62-63 O O(log N), balanced trees, 340 O(N), 339-340 object-oriented design/software engineering, further reading, 511 object-oriented programming languages, further reading, 510 object-oriented programming, see OOP objects C++, 98, 100 instantiating, 21 519 OOP, 19-21 ordered arrays, 298 sorting, 98 storing, 64-68 OOP (object-oriented programming), 18-21, 23 access modifiers, 24 ADTs, 179 classes, 20 constructors, 23 inheritance, 24 main() function, 23 objects, 19-21 polymorphism, 24 open addressing, 427 efficiency, 451-452 separate chaining, 468 Open command, File menu, 506 opening delimiters, 119-120 Workshop applets, 506 operators, dot, 21 OrdArray class, 57-60 ordered arrays, 51-54 binary trees, 298 C++ code, 55-60 Ordered Workshop applet, 51-54 organization, array.cpp, 40 overviews algorithms, 15 data structures, 14-15 P parent classes, 24 see also base classes partial sorts, 90-91 36 72316331 index 10/31/02 520 6:57 AM Page 520 Partition Workshop applet Partition Workshop applet, 258-259 partition.cpp program, 260-262 partitioning, 258-264 median-of-three, 280-282 paths, trees, 300 Peek button, 112 priority queues, 140-141 peek() member functions, 134 peeking, Queue workshop applet, 128 performance CPU, 477 trees, 339-340 Person class, 64-68 pivot values, 262-272 pointers double-ended lists, 174 linked lists, 146-147 polymorphism, 24 Pop button, 112 pop(), 166 popping, defined, 110 postfix notations, 324 postorder traversal, 317-324 see also traversing trees prefix notations, 323 preorder traversal, 317-324 see also traversing trees primary clustering, 444 prime numbers, double hashing, 451 priority queues, 137-141 C++ code, 141-143 PriorityQ Workshop applet, 138-141 private, 24 probes, 430 linear, 452 quadratic, 442-445 programming object-oriented, 18 recursion, 207-215 tools, 107-109 programming styles, further reading, 512 programs Cardfile, 13 classDataArray, 65-68 classes, 40-43 partition.cpp, 260-262 sample, 507-508 triangle.cpp, 212 public, 24 Push button, 111 push(), 166 puzzles, The Towers of Hanoi, 233-240 Q quadratic probing, 442-445 queues, 14, 107-126, 482 abstract, 108 C++, 132-136 circular, 130-132 classes, 135-137 priority, 137-141, 482 restricted access, 108 Workshop applet, 126-137 wrapping around, 131-132 TeamLRN CuuDuongThanCong.com quicksort, 258-265 troubleshooting, 279-293 quickSort1 Workshop applet, 272-277 quickSort2 Workshop applet, 286 quiz answers, 489-504 R R/B button, RBTree Workshop applet, 345 random keys, separate chaining, 469 ranges, 62-63 RBTree Workshop applet, 344-350 reading, 509 real-world data storage, 12-13 real-world modeling, 14 rear arrows, arrays, 130 records, defined, 16 recQuickSort(), arguments, 266 recursion, 207 binary searches, 223-228 loops, 223-226 mergesort, 240-252 The Towers of Hanoi, 233-240 triangular numbers, 208-215 recursive functions, 215-216 recursive triangle() member functions, 215 36 72316331 index 10/31/02 6:57 AM Page 521 splits, 2-3-4 trees recursion anagrams, 216-218 stacks, 228, 230 red-black trees, 338-341 0(log N), 340 0(N), 339 2-3-4 trees, 405-409 balanced/unbalanced, 338-339 color flips, 361-372 colored nodes, 341 deleting, 373 duplicate keys, 342 efficiency, 374 nodes clicking, 343 inserting , 360-374 RBTree Workshop applet, 343-345 rotations, 351-355 rules, 341, 350 searching, 345 red-red conflicts, 362 relationships, classes, 170 remaining columns, finding, 210-211 remove() member functions, 134, 161 removeFirst() member function, 153 replacing loops, 223-228 restricted access, 108 reversing words, 116-118 right child, 303 ring buffers, see circular queues RoL button, RBTree Workshop applet, 344 root flipping, 363 splits, 385-386 trees, 300 CuuDuongThanCong.com RoR button, RBTree Workshop applet, 345 rotating trees, 347-355 words, 217 rules red-black, 341, 350 violations, 370-372 Run button, 80 running, see executing S sample programs, 507-508 search keys, 17 Search menu commands, Find, 13 searching arrays, 31-35 binary, 51-54, 70 C++ code, 55-60 maximum/minimum values, 324-325 binary trees, 479 duplicate keys, 36 items, 34 2-3-4 trees, 383 2-3-4 trees (C++), 397 arrays, 39 linear, 52-55 linked lists, 298 links, 148-160 red-black trees, 345 separate chaining, 467 selecting pivot values, 266-272 self-referential classes, 146 521 separate chaining, 458-470 simple linked lists, 149-157 see also linked lists simple sorts, comparing, 102 single-stepping, bubble sorts, 81 Size button, 81-82 sizes array, 81-82 HashChain Workshop applet, 461 stacks, 112 small partitions, 286-293 software engineering, 26 sorted lists, 183-189 creating, 149 sorting, 75-79 algorithms, 101 bubble sorts, 86-87 C++ code, 83-86 Workshop applet, 79-83 data structures, 483-484 insertion, 90-97 insertion sorts, 286-290 New button, 80 objects, 98-100 partial, 90-91 quicksort, 265-272 Run button, 80 simple, 102 step button, 81 special purpose data structures, 481-482 see also data structures speed, algorithms, 477 splits, 2-3-4 trees, 384-387 36 72316331 index 10/31/02 522 6:57 AM Page 522 Stack Workshop applet Stack Workshop applet, 111-112 stacks, 14, 170, 482 abstract, 108 C++, 113-116 delimiters, 119-120 efficiency, 123 errors, 116 examples, 118-119 items, 109 linked lists, 166-169 pop(), 166 push(), 166 recursion, 228, 230 size, 112 StackX class member functions, 114-115 Standard Template Library, see STL Start button, RBTree Workshop applet, 343 starting new sorts, 80 Step button, singlestepping, 81 STL (Standard Template Library), 25 stopping pivot values, 262-263 storing data, 12-13 objects, 64-68 trees, 412 string classes (C++), 25-26 strings, hashing, 471 subarrays, 267 subtrees, 302 moving, 235, 354 swapping pivots, 268 swapping pivot values, 262-263 T tables hash, 417-425, 479 HashChain Workshop applet, 461 terminating sample programs, 508 terminology, trees, 300-303 text, RBTree Workshop applet, 345 The Towers of Hanoi, 233-240 tools, 14 design, 180 programming, 107-108 top-down insertion, 360 see also inserting Towers Workshop applet, 234-238 transformations, 407 traversing trees, 302, 317-324 Tree class, 307 Tree234 Workshop applet, 387-392 tree234.cpp, 398, 400, 402-405 trees, 299 2-3-4, 379-382 efficiency, 409-412 implementing, 395-405 inserting items, 383 node splits, 384-387 red-black trees, 405-409 root splits, 385-386 searching items, 383 TeamLRN CuuDuongThanCong.com transformations to red-black trees, 407 Tree234 Workshop applet, 387-392 3-node, 319-320 algebraic expressions, 322 balanced, 375, 479 binary, 297-327 C++, 318-319 child, 302 creating, 305-308 defined, 299-300 duplicate keys, 327 hierarchical file structures, 303 inorder, 318 leaf, 302 maximum/minimum values, 324-325 parent, 301 path, 300 postorder, 322-324 preorder, 322-324 red-black, 338-355 root, 300 terminology, 300-303 traversing, 317-324 unbalanced, 305-306 Workshop applet, 320-322 triangle() function, 211-215 triangle.cpp program, 212 triangular numbers, 208-215 troubleshooting quicksort, 279-293 red-black rules, 342 rule violations, 371-372 stacks, 116 36 72316331 index 10/31/02 6:57 AM Page 523 Zoom button, 234Tree Workshop applet U unbalanced trees, 305-306, 338-339, 349 unordered arrays, 69 unsorted lists, 149 V values key, 148-149 maximum/minimum, 324-325 pivot, 262-263 vector classes (C++), 26 viewing arrays, 40 lists, 194 nodes, 390-391 violations, 371-372 visiting nodes, 302 W-Z words, 216-218 Workshops applets, 505-507 Binary Tree applet, 304-314 Hash applet, 427-432 HashChain applet, 458-461 insertsort applet, 92-93 LinkList applet, 147-149, 184-189 mergesort applet, 246-251 CuuDuongThanCong.com Partition applet, 258-259 PriorityQ, 138-141 quickSort1 applet, 272-277 quickSort2 applet, 286 RBTree applet, 343-350 Stack applet, 111-112 Towers applet, 234-238 Tree234 applet, 387-392 wrapping around queues, 131-132 Zoom button, 234Tree Workshop applet, 389 523 ... tree234.cpp 398 2-3 -4 Trees and Red-Black Trees 405 Transformation from 2-3 -4 to Red-Black 406 Operational Equivalence 406 Efficiency of 2-3 -4 Trees 409... applet shows how they work Hour 20, “Implementing 2-3 -4 Trees,” presents C++ code for a 2-3 -4 tree and discusses the relationship of 2-3 -4 trees to red-black trees Hour 21, “Hash Tables,” introduces... 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 CuuDuongThanCong.com 00 72316331 FM 10/31/02

Ngày đăng: 29/08/2020, 22:40

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN