Data structures and algorithms in c++ by drozdek, adam

818 4 0
Data structures and algorithms in c++ by drozdek, adam

Đ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

This is an electronic version of the print textbook. Due to electronic rights restrictions, some third party content may be suppressed. Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. The publisher reserves the right to remove content from this title at any time if subsequent rights restrictions require it. For valuable information on pricing, previous editions, changes to current editions, and alternate formats, please visit www.cengage.comhighered to search by ISBN, author, title, or keyword for materials in your areas of interest.

Data Structures and Algorithms in C++ Fourth Edition Adam Drozdek Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it This is an electronic version of the print textbook Due to electronic rights restrictions, some third party content may be suppressed Editorial review has deemed that any suppressed content does not materially affect the overall learning experience The publisher reserves the right to remove content from this title at any time if subsequent rights restrictions require it For valuable information on pricing, previous editions, changes to current editions, and alternate formats, please visit www.cengage.com/highered to search by ISBN#, author, title, or keyword for materials in your areas of interest Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it Data Structures and Algorithms in C++, Fourth Edition by Adam Drozdek Executive Editor: Marie Lee Senior Product Manager: Alyssa Pratt Associate Product Manager: Stephanie Lorenz © 2013 Cengage Learning ALL RIGHTS RESERVED No part of this work covered by the copyright herein may be reproduced, transmitted, stored or used in any form or by any means graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web distribution, information networks, or information storage and retrieval systems, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the publisher For product information and technology assistance, contact us at Cengage Learning Customer & Sales Support, www.cengage.com/support Content Project Manager: Matthew Hutchinson For permission to use material from this text or product, submit all requests online at www.cengage.com/permissions Further permissions questions can be emailed to permissionrequest@cengage.com Art Director: Cheryl Pearl Print Buyer: Julio Esperas Compositor: PreMediaGlobal Proofreader: Andrea Schein Library of Congress Control Number: 2012942244 Indexer: Sharon Hilgenberg ISBN-13: 978-1-133-60842-4 ISBN-10: 1-133-60842-6 Cengage Learning 20 Channel Center Street Boston, MA 02210 USA Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil and Japan Locate your local office at: www.cengage.com/global Cengage Learning products are represented in Canada by Nelson Education, Ltd To learn more about Cengage Learning, visit www.cengage.com Purchase any of our products at your local college store or at our preferred online store www.cengagebrain.com Some of the product names and company names used in this book have been used for identification purposes only and may be trademarks or registered trademarks of their respective manufacturers and sellers Any fictional data related to persons or companies or URLs used throughout this book is intended for instructional purposes only At the time this book was printed, any such data was fictional and not belonging to any real persons or companies Cengage Learning reserves the right to revise this publication and make changes from time to time in its content without notice The programs in this book are for instructional purposes only They have been tested with care, but are not guaranteed for any particular intent beyond educational purposes The author and the publisher not offer any warranties or representations, nor they accept any liabilities with respect to the programs Printed in the United States of America 18 17 16 15 14 13 12 Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it To my daughters, Justyna and Kasia Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it Contents  Object-Oriented Programming 1.1 1.2 1.3 1.4 1.5 1.6 1.7 Using C++ Abstract Data Types   1 Encapsulation   1 Inheritance   6 Pointers   9 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 Pointers and Arrays   12 Pointers and Copy Constructors   14 Pointers and Destructors   16 Pointers and Reference Variables   17 Pointers to Functions   20 Polymorphism   21 C++ and Object-Oriented Programming   23 The Standard Template Library   24 1.7.1 1.7.2 1.7.3 1.7.4 Containers   24 Iterators   25 Algorithms   25 Function Objects   26 1.8 1.9 1.10 Vectors in the Standard Template Library   28 Data Structures and Object-Oriented Programming   35 Case Study: Random Access File   35 1.11 Exercises   46 1.12 Programming Assignments   48 Bibliography   50 Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it vi    ■    C o n t e n t s  Complexity Analysis 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 Computational and Asymptotic Complexity   51 Big-O Notation   52 Properties of Big-O Notation   54 Ω and Θ Notations   56 Possible Problems   57 Examples of Complexities   57 Finding Asymptotic Complexity: Examples   59 The Best, Average, and Worst Cases   61 Amortized Complexity   64 NP-Completeness   68 2.11 Exercises   71 Bibliography   74  Linked Lists 3.1 51 75 Singly Linked Lists   75 3.1.1 Insertion   81 3.1.2 Deletion   83 3.1.3 Search   89 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 Doubly Linked Lists   90 Circular Lists   94 Skip Lists   96 Self-Organizing Lists   101 Sparse Tables   106 Lists in the Standard Template Library   109 Concluding Remarks   113 Case Study: A Library   114 3.10 Exercises   125 3.11 Programming Assignments   127 Bibliography   130 Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it C o n t e n t s     ■    vii  Stacks and Queues 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 Stacks   131 Queues   139 Priority Queues   148 Stacks in the Standard Template Library   149 Queues in the Standard Template Library   149 Priority Queues in the Standard Template Library   151 Deques in the Standard Template Library   153 Case Study: Exiting a Maze   158 4.9 Exercises   165 4.10 Programming Assignments   166 Bibliography   168  Recursion 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.10 5.11 Recursive Definitions   169 Function Calls and Recursion Implementation   172 Anatomy of a Recursive Call   174 Tail Recursion   177 Nontail Recursion   178 Indirect Recursion   184 Nested Recursion   186 Excessive Recursion   186 Backtracking   190 Concluding Remarks   197 Case Study: A Recursive Descent Interpreter   198 5.12 Exercises   207 5.13 Programming Assignments   210 Bibliography   213  Binary Trees 6.1 6.2 6.3 131 169 214 Trees, Binary Trees, and Binary Search Trees   214 Implementing Binary Trees   219 Searching a Binary Search Tree   222 Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it viii    ■    C o n t e n t s 6.4 6.5 6.6 6.7 6.8 6.9 6.10 6.11 6.12 Tree Traversal   224 6.4.1 Breadth-First Traversal   225 6.4.2 Depth-First Traversal   226 6.4.3 Stackless Depth-First Traversal   233 Insertion   240 Deletion   243 6.6.1 Deletion by Merging   244 6.6.2 Deletion by Copying   246 Balancing a Tree   250 6.7.1 The DSW Algorithm   253 6.7.2 AVL Trees   256 Self-Adjusting Trees   261 6.8.1 Self-Restructuring Trees   262 6.8.2 Splaying   263 Heaps   268 6.9.1 Heaps as Priority Queues   270 6.9.2 Organizing Arrays as Heaps   271 Treaps   276 k-d Trees   280 Polish Notation and Expression Trees   286 6.12.1 Operations on Expression Trees   287 6.13 Case Study: Computing Word Frequencies   290 6.14 Exercises   298 6.15 Programming Assignments   302 Bibliography   306  Multiway Trees 7.1 309 The Family of B-Trees   310 7.1.1 B-Trees   311 7.1.2 B*-Trees   321 7.1.3 B+-Trees   323 7.1.4 Prefix B+-Trees   326 7.1.5 K‑d B-trees   327 7.1.6 Bit-Trees   334 7.1.7 R-Trees   336 7.1.8 2–4 Trees   337 7.1.9 Sets and Multisets in the Standard Template Library   353 7.1.10 Maps and Multimaps in the Standard Template Library   359 Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it 786    ■    I n d e x problems decomposing into subproblems, 170 satisfiability, 69–70 programming languages Polish notation, 287 recursive definitions, 171 specification of valid language elements, 171 programs, compilation, 198 compiled, 51 interpreted, 51 using information stored in secondary storage, 311 program() user, 658–659 prolific region, 653 Prolog, 626 tail recursion, 178 properties and big-O notation, 54–56 protected inheritance, protected keyword, pSet1 set, 357 pSet2 set, 357 pseudograph, 391–392 pseudokeys, 571 public inheritance, public keyword, push_back() function, 29, 31, 111, 155–157 push_front() function, 32, 111, 154–156 push() function, 131, 149–151, 231–232 putQueen() function, 191, 193–194 changes on run-time stack for, 195 placing four queens, 196–197 put() statement, 180 p variable, 75, 77 Q quadratic algorithms, 57, 59 quadratic functions, 553–555 quadratic probing, 554–556 queue container, 24–25, 150 queue() function, 150 queueHeads array, 525 queues acrosticIndicator() algorithm, 140 adding and removing elements, 139 array implementation, 140–143 Bank One example implementation code, 146–147 breadth-first traversal, 225, 397 checking if empty, 139 clearing, 139 doubly linked list implementation, 141, 143–144 enqueue and dequeue operations, 139–140 FIFO (first in/first out) structure, 139 hierarchy, 214 priority queues, 148 simulations, 144–146 STL (Standard Template Library), 149–150 toBeChecked list, 403 used separately, 404 queues[] array, 525 queueTails[] array, 525 queuing theory, 144 quick operations, 65 quickSearch() algorithm, 693–695 quicksort, 512–513, 518 quicksort() algorithm, 513–515 quicksort2() algorithm, 518 R radix sort, 521–526 radixsort() algorithm, 521–526 radix transformation, 551 rand() function, 566 random access files defining record format, 36–45 directly accessing records, 36 finding records, 36–45 fixed-length records, 36 inserting records, 36–45 modifying records, 36–45 randomized tree, 277 ranges two-dimensional array, 282 rbegin() function, 24, 29, 111, 155, 354, 361 read barrier, 647, 649 readCommittees() function, 469, 471 readFromFile() function, 44–45 readKey() function, 45 real-time systems and iteration, 197–198 receive() function, 184 records defining format, 36–45 direct access, 36 finding, 36–45 fixed-length, 36 inserting, 36–45 modifying, 36–45 outputting in readable form, 45 recurrence equation, 256–257, 634 recursion, 625 anchors, 185 backtracking, 191–197 converting into iterative version, 197 excessive recursion, 186–190 function calls, 172–174 functions, 226 indirect recursion, 184–185 logical simplicity, 186 nested recursion, 186 nontail recursion, 178–184 printing input line in reverse order, 178–179 program readability, 177 properly handling, 174 readability, 186 repetition of calculations, 187–188 run-time stack, 186 self-documentation, 177 simplifying coding, 177 slowing down execution time, 186–187 speed, 198 tail recursion, 177–178 von Koch snowflakes, 182–184 recursive calls anatomy of, 174–177 at end of function implementation, 177–178 Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it I n d e x     ■    787 end-of-line character, 179 reverse() function, 178–179 tracing, 175 recursive definitions, 169–172 recursive descent, 199–200 recursive descent interpreter, 198–206, 288 recursive functions, 174–177 Fibonacci numbers, 187–190 iterative versions, 180 run-time stack, 233 recursive traversals and run-time stack, 240 red-black trees, 339 maps, 359 sets implementing as, 357–358 redistributed keys, 319 red pointers, 339 reduction algorithm, 69, 465–466 reference types, 18–19 reference variables, 17–19 regular expression matching, 707–711 regular expressions, 674 defining, 707 NDFA (nondeterministic finite automation), 707–711 regularExpr() function, 709–710 rehashing cuckoo hashing, 568–570 flexibility to hashing, 571 global impact on hash table, 571 hash tables, 568–570 reinterpret_cast operator, 45 relational databases, 24 run-length encoding, 607 remove() function, 111, 154 rend() function, 24, 29, 111, 155, 354, 361 reserve() algorithm, 630–631 reserveFib() algorithm, 635–636 reserve() function, 29, 32, 112, 154 resize() function, 29, 32, 111–112, 155 resultStack stack, 136 returned values, 175 reverse() algorithm, 33 reverse() function, 111, 178–179 rightDiagonal[] array, 194 right side occurrence rule, 686 root, 214 B-tree of order m, 312 moving node to, 262–263 root[] array, 410 root pointers, 637 roots[] array, 658 root set, 637 rotateRight() function, 253 r reduction function, 69 R+-trees, 337 R-trees, 336–337 run-length encoding, 606–607 fax images, 607 Huffman coding with, 610–621 relational databases, 607 text files, 607 runs, 606–607, 611 run-time stack, 643 activation records, 173, 195 behavior, 228 changes, 194, 228–229 data necessary to resume execution, 638 dynamic allocation, 172 mark-and-sweep garbage collection, 637 overflow, 637 pending calls on, 176 recursive functions, 233 recursive traversals, 240 storing many things on, 186 tracking all calls on, 175 S Sager, Thomas, 566 salary data member, 44, 45 satisfiability problem, 69–70 saturation degree, 461 scan pointer, 646 scatter table, 558 SCC (strongly connected components), 418–419 Schorr and Waite algorithm, 642 science and trees, 214 search() function, 222, 244, 282–283 searching best case, 223 binary search trees, 222–224, 276 bit-trees, 335–336 B-trees, 314 Cichelli’s algorithm, 564–565 comparisons performed, 223 complexity, 223 compressed trie, 371 for elements in lists, 103–104 files, 35–45 linked lists, 114, 216 R-trees, 337 singly linked lists, 89 trees, 216, 250 vh-trees, 339–342 worst case for binary tree, 223 searching algorithms, 674 searching and replacing text, 674 search() procedure, 564 secondary clusters, 555–556 seek time, 310 selection sort, 495–497 selectionsort() algorithm, 495–497 self-adjusting trees moving up elements used more often, 262 self-restructuring trees, 262–263 splaying, 263–268 self-organizing lists, 101–106 self-referential objects, 76 self-restructuring trees, 262–263 semispaces, 644 semisplaying, 267, 291 separate chaining, 558–559 separator keys, 326 sequence set, 323 sequential coloring, 460 sequentialColoringAlgorithm() algorithm, 460–461 sequential-fit methods, 626–627 sequential search key comparison, 548 service[] array, 146 set class, 25, 359 set container, 24, 353–356 set() function, 355 set-related algorithms, 358–359 sets implemented as red-black tree, 357–358 inserting number, 356–357 Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it 788    ■    I n d e x insertion operation, 353 ordering, 357–358 set-related algorithms, 358–359 solving union-find problem, 409–411 symmetric difference, 439–440 set_union() algorithm, 358 shakersort, 508 Shannon, Claude E., 591 Shell, Donald L., 505 Shell sort, 505–508 shiftAnd() algorithm, 726 shift-and algorithm, 697–700 shift folding, 549–550 shortest paths all-to-all shortest path problem, 405–408 deque, 403–404 Dijkstra’s algorithm, 399–401 FordAlgorithm() algorithm, 402–403 labelCorrectingAlgorithm() algorithm, 403–404 label-correcting algorithms, 402–403 label-correcting methods, 399 label-setting methods, 398–399 small label first method, 405 threshold algorithm, 405 short ordered lists, 148 sibling property, 602 simple graphs, 391 simpleIterativeReverse() function, 179–180 simple language interpreter, 202–206 simple prefix B+-tree, 326 Simula, simulations, queues, 144–146 sine, 185 single-node list, 87 single variables, single vertex, 439 singly linked lists accessing nodes, 75–76 access node, 81 action in case of exception, 85 deleting nodes, 83–89 implementation, 78–81 incorporating nodes, 77 initializing nodes, 77 inserting nodes, 81–83 integers, 78–81 linking nodes, 76 manipulating, 81 nodes, 77, 81–82, 90 only one node to remove, 86–87 pointers to trees, 593 searching, 89 storing information, 76 sink, 421, 423 size() function, 24, 29, 31, 111, 149–151, 155, 355, 361 SkipList class, 101 SkipListNode class, 101 skip lists efficiency, 101 implementation, 98–100 inserting and deleting nodes, 97, 100–101 levels of nodes, 96, 97 nonsequential search, 96–101 small label first method, 405 Smalltalk, 23, 626 sort() algorithm, 33–34, 494 sort() function, 111, 154 sorting alphabetically, 491 arrays, 251 ASCII (American Standard Code for Information Interchange) code, 491 big-O notation, 492 comparing elements, 501–505 criteria of efficiency, 491–492 decision trees, 501–505 EBCDIC (Extended Binary Coded Decimal Interchange Code), 491 efficient sorting algorithms, 505–528 elementary sorting algorithms, 492–501 estimating speed, 501 integers, 521–527 number of comparisons, 491–492 number of movements, 491–492 numbers, 491 partial sorting functions, 528–531 speed, 503, 505 STL (Standard Template Library), 528–532 subarrays, 505 vectors, 33–34 sorting algorithms, 33 binary trees, 501–502 bubble sort, 497–500 comb sort, 500–501 complexity, 492 counting sort, 527–528 efficiency, 501, 505–528 elementary, 492–501 heap sort, 508–512 insertion sort, 492–495 mergesort, 518–521 quicksort, 512–518 radix sort, 521–526 selection sort, 495–497 Shell sort, 505–508 stable, 532 source, 423 entropy, 590 sourceList adjacency list, 469 space, 51, 57–59 spanning trees and graphs, 411–415 sparse tables, 106–109 spatial data, 336–337 specialized files, 602 spell checker dictionary, 373 implementation using tries, 376–383 splaying efficiency of accessing node, 265–267 focusing on elements, 267 heterogeneous configuration, 263–265, 267 homogeneous configuration, 263–265, 266 rotations, 265–267 splaying() algorithm, 264 splice() function, 111, 154 splitting buckets, 572–576 leaves, 315–316, 327–329 nodes, 322, 327–329, 343 2-4 trees, 343–345 Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it I n d e x     ■    789 square brackets [] delimiters, 132 srand() function, 555 SSN data member, 44, 45 SSN map, 363–364 stable marriage problem, 442 stableMatching() algorithm, 444–445 stable matching problem, 442–445 stable_sort() function, 532 Stack class, 164 stack container, 24, 25, 149 stacked-node-checking algorithm, 642 stack frames, 172 stack() function, 149 stackless depth-first traversal threaded trees, 233–236 traversal through tree transformation, 236–240 stack pointer, 175 stacks adding large numbers, 134–136 arrays, 638 capacity of, 138 checking if empty, 131 clearing, 131 depth-first algorithms, 397 exiting maze, 159 explicit handling, 180 hierarchy, 214 incorporating in list being processed, 638 limited size, 638 linked lists, 136–138, 638 matching delimiters in program, 132–133 overflow, 638–639, 642 as part of tree, 234 predicted size, 138 push and pop operations, 131–132, 138 putting element on top of, 131 recursion, 172 retrieving data in reverse order, 132 returning top element to, 131 run-time stack duties, 180 STL (Standard Template Library), 149 storing coordinated of positions of cells, 160 storing pointers to nodes, 642 taking top element from, 131 threaded trees, 236–237 vector implementation, 136–137 stack variable, 180 Standard Template Library See STL (Standard Template Library) Standish, Thomas, 556 star (*) indirection operator, static binding, 22 Stearns, Richard E., 51 stem, 448 Stepanov, Alexander, 24 st global stack object, 180 STL (Standard Template Library), 24 algorithms, 25–26 containers, 24–25 deques (double-ended queue), 153–158 function objects, 26–27 generic stack class, 149 implementing list as generic doubly linked list, 109–110 iterators, 25 lists, 109–113 maps, 359–364 multimaps, 359–364 multisets, 353–359 priority queues, 151–153 queues, 149–150 sets, 353–359 sorting, 528–532 stacks, 149 vectors, 28–34 stop-and-copy algorithm, 644 stop-and-copy technique, 646–648 store() function, 184 Stout, Quentin F., 253 straightforward algorithms for exact string matching, 674–677 strcpy() function, 14 string matching, 674 approximate, 719–729 exact, 674–718 with k errors, 726–729 stringology, 674 strings aligning, 719 alphabetical order, 218 comparisons, 491 converting to numbers, 45 copying, 14 descendant, 723 differences between, 719–720 functions, 14 length, 14 longest common subsequence, 723 longest common substring, 729–737 predecessor, 723 stored in numerical data members, 44–45 string similarity, 720 strlen() function, 14, 180 strongDFS() algorithm, 419–420 strongly connected components See SCC (strongly connected components) student array, 108 Student class, 45 students array, 106–109 studentsInClasses array, 107–108 subarrays, 251 bounds, 512 dividing, 512–513 merging, 519 sorting, 505 subclasses, subgraphs, 392, 415–416, 462–463 subscript operator([]), 361 substitution and string similarity, 720 subtrees and heaps, 275 successor data member, 234 successor threads, 242 suffix arrays, 717–718 suffix links, 714 suffix trees exact string matching, 711–717 nodes implicit, 713 obtained from suffix trie, 713 suffix links, 714 transforming suffix tries to, 716 Ukkonen algorithm, 714 suffix tries, exact string matching, 711–717 sum() function, 26–27 Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it 790    ■    I n d e x Sunday, Daniel, 693 Sunday algorithms, 693–695 superclass, super-symbols, 611–612 swap() function, 5, 24, 29, 112, 155, 355, 359, 496–497 sweep() algorithm, 643 sweeper pointer, 651 SWmarking() algorithm, 638–639 symbols, 599, 601–602 symmetric binary B-tree, 337 symmetric difference, 439–440 system of distinct representatives theorem, 468–479 T tables clusters and linear probing, 552–553 directory or records, 571 equivalents for symbols, 599 index-inverted, 106 leaving deleted keys in, 562 maps, 359–364 overloaded deleted records, 562 size, 563 sparse tables, 106–109 transforming keys into index, 548 unsuccessful searches, 556 tail, 86 tailBlock field, 156–157 tail data member, 81, 153 tail field, 156 tail() function, 177–178 tail list, 646–647 tail member, 93–94 tail path, 638 tail permanent pointer, 94 tail pointer member, 83 tail recursion, 177–178, 198 tangent, 185 Tarjan, Robert, 395 Tarjan’s algorithm, 419–420 tasks, dependencies between, 421 independent, 459–460 represented by vertices, 459 T& back() function, 28 T class, 36 template classes, 24 templates and list processing, 89 term() function, 201, 710 terminal nodes, 214 text, searching and replacing, 674 text files run-length encoding, 607 searching, 35–45 T& front() function, 29 Θ notations, 56–57, 62 Thompson, Ken, 707 Thompson algorithm, 710–711 threadedInorder() function, 234 threaded trees, 233 inorder traversals, 234 nodes, 240 overloading existing pointers, 234 postorder traversals, 236 preorder traversals, 236 stacks, 236–237 threads incorporating in nodes, 234 successor, 242 3-colorability problem, 463–465 3-satisfiability problem, 70, 463 threshold algorithm, 405 throw clause, 85 time, 51 complexities, 57–59 time-consuming operations, 65 tmp object, 45 tmp pointer variable, 88 tmp record, 36 tmp variable, 86, 89, 494 toBeChecked list, 403–404 toBeChecked set, 399–401 topEl() function, 131 top() function, 149, 151 topological sort, 421–422 topologicalSort() algorithm, 421 topologicalSorting() algorithm, 422 top variable, 180 tospace, 646 tractable algorithms, 69 traditional languages function calls, train algorithm, 655 transpose method, 102–104, 106 traveling salesman problem See TSP (traveling salesman problem) traversal algorithms and trees, 395 traversal through tree transformation, 236–240 traversing binary trees, 240 treaps, 276–280 trees arcs, 214 average number of visits in, 366 average path length, 223 balancing, 250–260 binary, 216–217 binary search, 217 building phase, 255 Cartesian tree, 277 complete binary, 217 degenerate, 237 with deliberate number of children, 216 destroying original structure, 237 disjointed, 214 edges, 396 elements occurring once, 223 empty, 214 exact string matching, 711–717 examples, 215 heap property, 509 height, 214 Hungarian tree, 441 IPL (internal path length), 223 leaves, 214, 218 maps, 359–361 multiway tree of order m, 309 m-way tree, 309 nodes, 214 nonthreaded binary, 240 ordered binary, 217 ordered by keys, 359 orderly, 216 paths, 214 pointers, 234 random insertions and deletions, 249 randomized tree, 277 recursively defining, 214 redundant nodes, 223 relations of hierarchical type, 391 representing hierarchical structure of domain, 250 Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it I n d e x     ■    791 right unbalanced, 249 roots, 214, 217, 592 science and, 214 searching, 216, 250 self-adjusting, 261–268 self-restructuring, 262–263 singly linked list of pointers, 593 stack as part of, 234 storing multiple copies of same values, 217–218 successor threads, 242 threaded, 233–236 transforming linked lists into, 216 traversal algorithms, 395 traversal through transformation, 236–240 turned into linked list, 517 two trees chosen to create, 592–593 unsymmetrical, or lopsided, 250 visiting each node exactly one time, 224–240 weighted path length, 593 tree traversals breadth-first traversal, 225 depth-first traversal, 226–233 order nodes are visited, 224–225 stackless depth-first traversal, 233–240 visiting each node only one time, 224–240 T& return type, 32 trie a tergo, 368 trieInsert() algorithm, 367, 374 trieNodes[] array, 371 tries average number of visits in, 366 bit version, 371 changing how words are tested, 368 composed of words, 364, 366 compressing, 370–371 failure, 364 fixed length nodes, 373 height, 366 inserting key, 367 key-characters organization, 364 leaves, 366, 374 linked lists, 373 longest identical prefix length, 367 nodes, 367–368, 372, 374 one character used in comparisons, 366 order keys inserted, 367 pointers, 366 printing contents, 374 pseudoflexible arrays, 374 signal for key insertion, 364 size, 373–374 skewed by words, 367 space required by, 367–369 special character (#) in each node, 366 speed of, 366 spell checker implementation, 376–383 storing dictionary words, 373 storing entire words, 366 2-4 tree implementation, 367 vertical rectangles, 364 try-catch statement, 85 try() function, 563 TS() algorithm, 421 TSize modulo, 549, 551–553, 556, 566 TSP (traveling salesman problem), 466 minimum Hamiltonian cycle, 455–459 minimum spanning tree, 456–457 triangle inequality, 456 Turing machine, 70 two-argument function object, 33 2-connected graphs, 415 2D edit table, 721 two-dimensional arrays, 108 two-dimensional character array, 158–159 two-pointer algorithm for heap compaction, 643 2-3-4 tree, 337 2-4 trees, 337–353 deletion by copying, 347 flag flipping, 344–345 nodes, 337, 339, 345–346 parent-children links, 339 removing nodes, 347–348 splitting, 343–345 transforming into binary tree form, 339 type declaration, U Ukkonen algorithm and suffix trees, 714 UkkonenSuffixTree() algorithm, 717, 729 UkkonenSuffixTrie() algorithm, 714–716 uncolored degrees, 461–462 underflow, 319, 324 undirected graphs connectivity, 415–418 detecting cycles, 408 vertex cover, 465–466 union-find problem, 409–411 union() function, 359, 410–411, 413, 415 unique elements, 353 unique() function, 112, 154 universal coding scheme, 607 universal hash functions, 551 UNIX agrep (approximate grep) command, 726 compact, 610 compress, 610 diff command, 725 fgrep command, 707 pack, 610 unlabeled vertices, 471 unordered lists and priority queues, 148 unsuccessful searches, 556 updateBalanceFactors() algorithm, 258–259 update() function, 731 updateTail() algorithm, 650 upper_bound() function, 355, 361 user-defined data types, 112 user functions as friends, 23–24 user-maintained stack, 233 V value_comp() function, 355, 361 Variable objects, 534 variables, address, 9, 20 attributes, auxiliary, declaring, Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it 792    ■    I n d e x deleting, 13–14 evaluation of, 199 indirectly accessing values, initializing, 44 pointers, 9–21 value of, 199 vector class, 25, 28–29, 31 vector container, 24, 149, 151 vector() function, 29 vectors, 28–34 accessing elements, 32 adding new element, 31–32, 65 algorithms, 32–33 allocating large amount of cells, 65 available cells, 32 changing capacity, 32 member functions, 30–31 memory allocation, 28, 32 overflow, 65 parameters values, 31 resizing, 32 sorting, 33–34 vertex cover problem, 465–466 vertical-horizontal trees, 339 vertical pointers, 339 vertices, 391 adjacency list, 471 adjacent, 392 articulation points, 415–416 assigning colors to, 460 degree, 392 Hamiltonian cycle, 453–454 incident with, 392 independent, 459 isolated, 392 minimum nonoverlapping sets, 459 partitioning into series of levels, 566 in same set, 409–411 saturation degrees, 461, 462 shortest path, 398–408 sink, 423 sorting high and low degrees, 460–462 source, 423 tasks represented by, 459 uncolored degrees, 461–462 unlabeled, 471 vertices[] array, 469, 471 vertices vector, 471 vh-property, 345 VHTreeInsert() algorithm, 345–346 vh-tree property, 345, 348 vh-trees, 339 AVL trees, 352 binary trees, 339 deletion by copying, 347 flag flipping, 344–345 inserting sequence of numbers, 346–347 nodes, 343, 345–348 odd height, 340–342 properties, 339 searching, 339–342 splitting nodes, 343–345 successors, 347–348 virtual function members, 22–23 virtual hashing, 571 virtual modifier, visit() function, 231–232, 239–240 von Koch, Helge, 181 von Koch snowflakes, 181 recursion, 182–184 von Neumann, John, 518 W WagnerFischer() algorithm, 721–722 WagnerFisherPrint() algorithm, 722–723 Warren, Bette L., 253 Warshall, Stephen, 405 weighted buddy system, 636 weighted graphs, 392 weighted path length, 593 WFIalgorithm() algorithm, 406 while loops, 151, 180, 231, 237–238, 254–255, 513, 516 iterations, 400–402, 639 Williams, John, 273, 509 Wilson, Paul, 657 Witzgall, C., 403 word frequencies, 290–297 worst-fit algorithm, 626–627 write barrier, 652–653 write() function, 44 writeLegibly() function, 45 writeToFile() function, 44–45 WuManber() algorithm, 727–729 Y year data member, 44–45 yes/no marker, 560 Yuasa, Taiichi, 650 Yuasa’s algorithm, 650–653 Z Ziv-Lempel algorithm, 607–610 LZ77, 607–608 universal data compression code, 607 Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it Copyright 2012 Cengage Learning All Rights Reserved May not be copied, scanned, or duplicated, in whole or in part Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s) Editorial review has deemed that any suppressed content does not materially affect the overall learning experience Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it

Ngày đăng: 14/12/2023, 19:23

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

Tài liệu liên quan