Data structures and algorithms with object oriented design patterns in c++ 2001

1.9K 1.1K 2
Data structures and algorithms with object oriented design patterns in c++ 2001

Đ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

Data Structures and Algorithms with Object-Oriented Design Patterns in C++ Data Structures and Algorithms with Object-Oriented Design Patterns in C++ Data Structures and Algorithms with Object-Oriented Design Patterns in C++ Bruno R Preiss B.A.Sc., M.A.Sc., Ph.D., P.Eng Associate Professor Department of Electrical and Computer Engineering University of Waterloo, Waterloo, Canada ● Colophon ● Dedication ● Preface ● Contents ● Introduction ● Algorithm Analysis ● Asymptotic Notation ● Foundational Data Structures ● Data Types and Abstraction ● Stacks, Queues and Deques ● Ordered Lists and Sorted Lists ● Hashing, Hash Tables and Scatter Tables ● Trees ● Search Trees ● Heaps and Priority Queues ● Sets, Multisets and Partitions ● Dynamic Storage Allocation: The Other Kind of Heap ● Algorithmic Patterns and Problem Solvers ● Sorting Algorithms and Sorters http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus4/html/book.html (1 of 2) [20/10/2001 01:19:31] Data Structures and Algorithms with Object-Oriented Design Patterns in C++ ● Graphs and Graph Algorithms ● C++ and Object-Oriented Programming ● Class Hierarchy Diagrams ● Character Codes ● References ● Index Copyright © 1997 by Bruno R Preiss, P.Eng All rights reserved http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus4/html/book.html (2 of 2) [20/10/2001 01:19:31] Colophon Data Structures and Algorithms with Object-Oriented Design Patterns in C++ Colophon Copyright © 1997 by Bruno R Preiss All rights reserved No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the author This book was prepared with LaTeX and reproduced from camera-ready copy supplied by the author The book is typeset using the Computer Modern fonts designed by Donald E Knuth with various additional glyphs designed by the author and implemented using METAFONT METAFONT is a trademark of Addison Wesley Publishing Company SPARCstation, Solaris, and Java are registered trademarks of Sun Microsystems TeX is a trademark of the American Mathematical Society UNIX is a registered trademark of AT&T Bell Laboratories Copyright © 1997 by Bruno R Preiss, P.Eng All rights reserved http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page1.html [20/10/2001 01:19:33] Dedication Data Structures and Algorithms with Object-Oriented Design Patterns in C++ Dedication To my children, Anna Kristina, Katherine Lila and Alexander Edgar Copyright © 1997 by Bruno R Preiss, P.Eng All rights reserved http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page2.html [20/10/2001 01:19:33] Preface Data Structures and Algorithms with Object-Oriented Design Patterns in C++ Preface This book was motivated by my experience in teaching the course E&CE 250: Algorithms and Data Structures in the Computer Engineering program at the University of Waterloo I have observed that the advent of object-oriented methods and the emergence of object-oriented design patterns has lead to a profound change in the pedagogy of data structures and algorithms The successful application of these techniques gives rise to a kind of cognitive unification: Ideas that are disparate and apparently unrelated seem to come together when the appropriate design patterns and abstractions are used This paradigm shift is both evolutionary and revolutionary On the one hand, the knowledge base grows incrementally as programmers and researchers invent new algorithms and data structures On the other hand, the proper use of object-oriented techniques requires a fundamental change in the way the programs are designed and implemented Programmers who are well schooled in the procedural ways often find the leap to objects to be a difficult one ● Goals ● Approach ● Outline ● Suggested Course Outline ● Online Course Materials Copyright © 1997 by Bruno R Preiss, P.Eng All rights reserved http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page3.html [20/10/2001 01:19:34] Goals Data Structures and Algorithms with Object-Oriented Design Patterns in C++ Goals The primary goal of this book is to promote object-oriented design using C++ and to illustrate the use of the emerging object-oriented design patterns Experienced object-oriented programmers find that certain ways of doing things work best and that these ways occur over and over again The book shows how these patterns are used to create good software designs In particular, the following design patterns are used throughout the text: singleton, container, iterator, adapter and visitor Virtually all of the data structures are presented in the context of a single, unified, polymorphic class hierarchy This framework clearly shows the relationships between data structures and it illustrates how polymorphism and inheritance can be used effectively In addition, algorithmic abstraction is used extensively when presenting classes of algorithms By using algorithmic abstraction, it is possible to describe a generic algorithm without having to worry about the details of a particular concrete realization of that algorithm A secondary goal of the book is to present mathematical tools just in time Analysis techniques and proofs are presented as needed and in the proper context In the past when the topics in this book were taught at the graduate level, an author could rely on students having the needed background in mathematics However, because the book is targeted for second- and third-year students, it is necessary to fill in the background as needed To the extent possible without compromising correctness, the presentation fosters intuitive understanding of the concepts rather than mathematical rigor Copyright © 1997 by Bruno R Preiss, P.Eng All rights reserved http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page4.html [20/10/2001 01:19:34] Approach Data Structures and Algorithms with Object-Oriented Design Patterns in C++ Approach One cannot learn to program just by reading a book It is a skill that must be developed by practice Nevertheless, the best practitioners study the works of others and incorporate their observations into their own practice I firmly believe that after learning the rudiments of program writing, students should be exposed to examples of complex, yet well-designed program artifacts so that they can learn about the designing good software Consequently, this book presents the various data structures and algorithms as complete C++ program fragments All the program fragments presented in this book have been extracted automatically from the source code files of working and tested programs The full functionality of the proposed draft ANSI standard C++ language is used in the examples including templates, exceptions and run-time type information[3] It has been my experience that by developing the proper abstractions, it is possible to present the concepts as fully functional programs without resorting to pseudo-code or to hand-waving Copyright © 1997 by Bruno R Preiss, P.Eng All rights reserved http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page5.html [20/10/2001 01:19:35] Outline Data Structures and Algorithms with Object-Oriented Design Patterns in C++ Outline This book presents material identified in the Computing Curricula 1991 report of the ACM/IEEE-CS Joint Curriculum Task Force[38] The book specifically addresses the following knowledge units: AL1: Basic Data structures, AL2: Abstract Data Types, AL3: Recursive Algorithms, AL4: Complexity Analysis, AL6: Sorting and Searching, and AL8: Problem-Solving Strategies The breadth and depth of coverage is typical of what should appear in the second or third year of an undergraduate program in computer science/computer engineering develops In order to analyze a program, it is necessary to develop a model of the computer Chapter several models and illustrates with examples how these models predict performance Both average-case and worst-case analyses of running time are considered Recursive algorithms are discussed and it is shown how to solve a recurrence using repeated substitution This chapter also reviews arithmetic and geometric series summations, Horner's rule and the properties of harmonic numbers introduces asymptotic (big-oh) notation and shows by comparing with Chapter Chapter results of asymptotic analysis are consistent with models of higher fidelity In addition to chapter also covers other asymptotic notations ( , and that the , this ) and develops the asymptotic properties of polynomials and logarithms Chapter introduces the foundational data structures the array and the linked list Virtually all the data structures in the rest of the book can be implemented using either one of these foundational structures This chapter also covers multi-dimensional arrays and matrices deals with abstraction and data types It presents the recurring design patterns used Chapter throughout the text as well a unifying framework for the data structures presented in the subsequent chapters In particular, all of the data structures are viewed as abstract containers discusses stacks, queues and deques This chapter presents implementations based on both Chapter foundational data structures (arrays and linked lists) Applications for stacks and queues and queues are presented covers ordered lists, but sorted and unsorted In this chapter, a list is viewed as a searchable Chapter container Again several applications of lists are presented introduces hashing and the notion of a hash table This chapter addresses the design of Chapter hashing functions for the various basic data types as well as for the abstract data types described in Chapter Both scatter tables and hash tables are covered in depth and analytical performance results http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page6.html (1 of 3) [20/10/2001 01:19:36] Outline are derived introduces trees and describes their many forms Both depth-first and breadth-first tree Chapter traversals are presented Completely generic traversal algorithms based on the use of the visitor design pattern are presented, thereby illustrating the power of algorithmic abstraction This chapter also shows how trees are used to represent mathematical expressions and illustrates the relationships between traversals and the various expression notations (prefix, infix and postfix) addresses trees as searchable containers Again, the power of algorithmic abstraction is Chapter demonstrated by showing the relationships between simple algorithms and balancing algorithms This chapter also presents average case performance analyses and illustrates the solution of recurrences by telescoping presents several priority queue implementations, including binary heaps, leftist heaps and Chapter binomial queues In particular this chapter illustrates how a more complicated data structure (leftist heap) extends an existing one (tree) Discrete-event simulation is presented as an application of priority queues covers sets and multisets Also covered are partitions and disjoint set algorithms The latter Chapter topic illustrates again the use of algorithmic abstraction Techniques for dynamic storage management are presented in Chapter This is a topic that is not found often in texts of this sort However, the features of C++ which allow the user to redefine the new and delete operators make this topic approachable Chapter surveys a number of algorithm design techniques Included are brute-force and greedy algorithms, backtracking algorithms (including branch-and-bound), divide-and-conquer algorithms and dynamic programming An object-oriented approach based on the notion of an abstract solution space and an abstract solver unifies much of the discussion This chapter also covers briefly random number generators, Monte Carlo methods, and simulated annealing covers the major sorting algorithms in an object-oriented style based on the notion of an Chapter abstract sorter Using the abstract sorter illustrates the relationships between the various classes of sorting algorithm and demonstrates the use of algorithmic abstractions presents an overview of graphs and graph algorithms Both depth-first and Finally, Chapter breadth-first graph traversals are presented Topological sort is viewed as yet another special kind of traversal Generic traversal algorithms based on the visitor design pattern are presented, once more illustrating algorithmic abstraction This chapter also covers various shortest path algorithms and minimum-spanning-tree algorithms At the end of each chapter is a set of exercises and a set of programming projects The exercises are designed to consolidate the concepts presented in the text The programming projects generally require the student to extend the implementation given in the text http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page6.html (2 of 3) [20/10/2001 01:19:36] Outline Copyright © 1997 by Bruno R Preiss, P.Eng All rights reserved http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page6.html (3 of 3) [20/10/2001 01:19:36] Finding Items in a List The find method also does a search of the ordered list However, it uses the isEQ method to compare the items Thus, the find method searches the list for an object which matches its argument The find method returns the object found If no match is found, it returns null The running time of this method In the worst case, the object sought depends on the time required for the comparison operator, is not in the list In this case the running time is For simplicity, we will assume that the comparison takes a constant amount of time Hence, the running time of the method is also O(n), is the number of items in the list where It is important to understand the subtle distinction between the search done by the isMember method and that done by find The isMember method searches for a specific object instance while find simply looks for a matching object Consider the following: Comparable object1 = new Int (57); Comparable object2 = new Int (57); List list = new OrderedListAsArray (1); list.insert (object1); This code fragment creates two Int class object instances, both of which have the value 57 Only the first object, object1, is inserted into the ordered list list Consequently, the method call list.isMember (object1) returns true; whereas the method call list.isMember (object2) returns false On the other hand, if a search is done using the find method like this: Comparable object3 = list.find (object2); the search will be successful! After the call, object3 and object1 refer to the same object Copyright © 1998 by Bruno R Preiss, P.Eng All rights reserved http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/html/page173.html (2 of 2) [20/10/2001 01:47:22] Removing Items from a List Data Structures and Algorithms with Object-Oriented Design Patterns in Java Removing Items from a List Objects are removed from a searchable container using the withdraw method Program defines the withdraw method for the OrderedListAsArray class This method takes a single argument which is a the object to be removed from the container It is the specific object instance which is removed from the container, not simply one which matches (i.e., compares equal to) the argument Program: OrderedListAsArray class withdraw method The withdraw method first needs to find the position of the item to be removed from the list This part is identical to the main loop of the isMember method An exception is thrown if the list is empty, or if the object to be removed is not in the list The number of iterations needed to find an object depends on its position If the object to be removed is found at position i, then the search phase takes O(i) time http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/html/page174.html (1 of 2) [20/10/2001 01:47:24] Removing Items from a List Removing an object from position i of an ordered list which is stored in an array requires that all of the , be moved one position to the left Altogether, objects at positions i+1, i+2, , objects need to be moved Hence, this phase takes time The running time of the withdraw method is the sum of the running times of the two phases, O(i)+ Hence, the total running time is O(n), where is the number of items in the ordered list Care must be taken when using the withdraw method Consider the following: Comparable object1 = new Int (57); Comparable object2 = new Int (57); List list = new OrderedListAsArray (1); list.insert (object1); To remove object1 from the ordered list, we may write list.withdraw (object1); However, the call list.withdraw (object2); will fail because object2 is not actually in the list If for some reason we have lost track of object1, we can always write: list.withdraw (list.find (object2)); which first locates the object in the ordered list (object1) which matches object2 and then deletes that object Copyright © 1998 by Bruno R Preiss, P.Eng All rights reserved http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/html/page174.html (2 of 2) [20/10/2001 01:47:24] Positions of Items in a List Data Structures and Algorithms with Object-Oriented Design Patterns in Java Positions of Items in a List As shown in Program , objects that implement the Cursor interface can be used to access, insert, and delete objects in an ordered list Program defines an inner class called OrderedListAsArray.MyCursor that implements the Cursor interface The idea is that instances of this inner class are used by the OrderedListAsArray class to represent the abstraction of a position in an ordered list Program: OrderedListAsArray.MyCursor class The MyCursor class has a single field, offset, that is used to record an offset in the array of objects http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/html/page175.html (1 of 2) [20/10/2001 01:47:25] Positions of Items in a List A single constructor is provided which simply assigns a given value to the offset field Program also defines the getDatum method of the MyCursor class This method simply returns the item in the array at the position record in the offset field, provided that position is valid The running time of the getDatum method is simply O(1) Copyright © 1998 by Bruno R Preiss, P.Eng All rights reserved http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/html/page175.html (2 of 2) [20/10/2001 01:47:25] Finding the Position of an Item and Accessing by Position Data Structures and Algorithms with Object-Oriented Design Patterns in Java Finding the Position of an Item and Accessing by Position Program defines two more methods of the OrderedListAsArray class, findPosition and get The findPosition method takes as its argument a Comparable object The purpose of this method is to search the ordered list for an item which matches the object, and to return its position in the form of an object that implements the Cursor interface In this case, the result is an instance of the MyCursor inner class Program: OrderedListAsArray class findPosition and get methods The search algorithm used in findPosition is identical to that used in the find method (Program ) The findPosition uses the isEQ method to locate a contained object which is equal to the search target Note that if no match is found, the offset is set to the value count, which is one position to the right of the last item in the ordered list The running time of findPosition is identical http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/html/page176.html (1 of 2) [20/10/2001 01:47:27] Finding the Position of an Item and Accessing by Position to that of find: , where The get method defined in Program takes an int argument and returns the object in the ordered list at the specified position In this case, the position is specified using an integer-valued subscript expression The implementation of this method is trivial it simply indexes into the array Assuming the specified offset is valid, the running time of this method is O(1) Copyright © 1998 by Bruno R Preiss, P.Eng All rights reserved http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/html/page176.html (2 of 2) [20/10/2001 01:47:27] Inserting an Item at an Arbitrary Position Data Structures and Algorithms with Object-Oriented Design Patterns in Java Inserting an Item at an Arbitrary Position Two methods for inserting an item at an arbitrary position in an ordered list are declared in Program insertBefore and insertAfter Both of these take one arguments a Comparable object The effects of these two methods are illustrated in Figure Figure: Inserting an item in an ordered list implemented as an array shows that in both cases a number of items to the right of the insertion point need to be moved Figure over to make room for the item that is being inserted into the ordered list In the case of insertBefore, items to the right including the item at the point of insertion are moved; for insertAfter, only items to the right of the point of insertion are moved, and the new item is inserted in the array location following the insertion point gives the implementation of the insertAfter method for the Program OrderedListAsArray.MyCursor class The code for the insertBefore method is identical except for one line as explained below http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/html/page177.html (1 of 3) [20/10/2001 01:47:29] Inserting an Item at an Arbitrary Position Program: OrderedListAsArray.MyCursor class insertAfter method The insertAfter method takes one arguments a Comparable object The method begins by performing some simple tests to ensure that the position is valid and that there is room left in the array to the insertion On line 19 the array index where the new item will ultimately be stored is computed For insertAfter the index is as shown in Program In the case of insertBefore, the value required is simply offset The loop on lines 21-22 moves items over and then object being inserted is put in the array on line 23 http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/html/page177.html (2 of 3) [20/10/2001 01:47:29] Inserting an Item at an Arbitrary Position If we assume that no exceptions are thrown, the running time of insertAfter is dominated by the loop which moves list items In the worst case, all the items in the array need to be moved Thus, the running time of both the insertAfter and insertBefore method is O(n), where Copyright © 1998 by Bruno R Preiss, P.Eng All rights reserved http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/html/page177.html (3 of 3) [20/10/2001 01:47:29] Removing Arbitrary Items by Position Data Structures and Algorithms with Object-Oriented Design Patterns in Java Removing Arbitrary Items by Position The final method of the OrderedListAsArray.MyCursor class that we will consider is the withdraw method The desired effect of this method is to remove from the ordered list the item at the position specified by the cursor shows the way in which to delete an item from an ordered list which implemented with an Figure array All of the items remaining in the list to the right of the deleted item need to be shifted to the left in the array by one position Figure: Withdrawing an item from an ordered list implemented as an array gives the implementation of the withdraw method After checking the validity of the Program position, all of the items following the item to be withdraw are moved one position to the left in the array http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/html/page178.html (1 of 3) [20/10/2001 01:47:31] Removing Arbitrary Items by Position Program: OrderedListAsArray.MyCursor class withdraw method The running time of the withdraw method depends on the position in the array of the item being deleted and on the number of items in the ordered lists In the worst case, the item to be deleted is in the first position In this case, the work required to move the remaining items left is O(n), where http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/html/page178.html (2 of 3) [20/10/2001 01:47:31] Removing Arbitrary Items by Position Copyright © 1998 by Bruno R Preiss, P.Eng All rights reserved http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/html/page178.html (3 of 3) [20/10/2001 01:47:31] Linked-List Implementation Data Structures and Algorithms with Object-Oriented Design Patterns in Java Linked-List Implementation introduces the This section presents a linked-list implementation of ordered lists Program OrderedListAsLinkedList class The OrderedListAsLinkedList class extends the AbstractSearchableContainer class introduced in Program OrderedList interface defined in Program and it implements the Program: OrderedListAsLinkedList fields ● Fields ● Inserting and Accessing Items in a List ● Finding Items in a List ● Removing Items from a List ● Positions of Items in a List ● Finding the Position of an Item and Accessing by Position ● Inserting an Item at an Arbitrary Position ● Removing Arbitrary Items by Position Copyright © 1998 by Bruno R Preiss, P.Eng All rights reserved http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/html/page179.html (1 of 2) [20/10/2001 01:47:32] Linked-List Implementation http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/html/page179.html (2 of 2) [20/10/2001 01:47:33] ... [20/10 /2001 01:19:43] Object Hierarchies and Design Patterns Data Structures and Algorithms with Object- Oriented Design Patterns in C++ Object Hierarchies and Design Patterns There is more to object- oriented. .. [20/10 /2001 01:19:41] Introduction Data Structures and Algorithms with Object- Oriented Design Patterns in C++ Introduction ● What This Book Is About ● Object- Oriented Design ● Object Hierarchies and. .. http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page23.html [20/10 /2001 01:19:47] Pointers Data Structures and Algorithms with Object- Oriented Design Patterns in C++ Pointers Mastering the use of pointers is essential when programming in C++ The key

Ngày đăng: 25/03/2019, 16:04

Từ khóa liên quan

Mục lục

  • www.pads.uwaterloo.ca

    • Data Structures and Algorithms with Object-Oriented Design Patterns in C++

    • Colophon

    • Dedication

    • Preface

    • Goals

    • Approach

    • Outline

    • Suggested Course Outline

    • Online Course Materials

    • Contents

    • Introduction

    • What This Book Is About

    • Object-Oriented Design

    • Abstraction

    • Encapsulation

    • Object Hierarchies and Design Patterns

    • Containers

    • Iterators

    • Visitors

    • Adapters

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

Tài liệu liên quan