Data Structures and Problem Solving with C++ Second Edition Instructors Resource Manual Mark Allen Weiss Florida International University Addison-Wesley Publishing Company, Inc. Menlo Park, California • Reading, Massachusetts • New York • Don Mills, Ontario Wokingham, U.K. • Amsterdam • Bonn • Paris • Milan • Madrid • Sydney Singapore • Tokyo • Seoul • Tapei • Mexico City • San Juan, Puerto Rico Chapter 1 1.1 1.2 1.3 2.2 2.3 3.2 3.3 Objects and Classes 7 Key Concepts and How To Teach Them 7 2.1.1 The class Construct 7 2.1.2 Public and Private Sections 8 2.1.3 Interface vs. Implementation 8 2.1.4 Constructors, Destructors, Copy Constructor, and Copy Assignment operator= 8 2.1.5 const Member Functions 9 2.1.6 this Pointer 9 2.1.7 Operator Overloading 9 2.1.8 Type Conversions 10 2.1.9 I/O (Friends), and More on Binary Operators 10 2.1.10 static Class Members 10 2.1.11 enum, and Constant Class Members 10 2.1.12 string Class 10 Solutions To Exercises 11 Exam Questions 13 Chapter 3 3.1 1 Key Concepts and How To Teach Them 1 1.1.1 Arrays 1 1.1.2 Strings 1 1.1.3 Pointers 2 1.1.4 Dynamic Allocation 2 1.1.5 Reference Variables and Parameter Passing Mechanisms 2 1.1.6 Structures 2 Solutions To Exercises 3 Exam Questions 4 Chapter 2 2.1 Pointers, Arrays, and Structures Templates 19 Key Concepts and How To Teach Them 19 3.1.1 The Concept of a Template 19 3.1.2 Function Templates 19 3.1.3 Class Templates 20 3.1.4 vector Class 20 3.1.5 Fancy Stuff 20 3.1.6 Bugs 20 Solutions To Exercises 21 Exam Questions 22 Chapter 4 4.1 4.2 4.3 5.2 5.3 6.2 6.3 Algorithm Analysis 33 The Standard Template Library Key Concepts and How To Teach Them 43 Solutions To Exercises 44 Exam Questions 46 Chapter 8 8.1 31 Key Concepts and How To Teach Them 33 6.1.1 What is Algorithm Analysis 33 6.1.2 Some Examples 33 6.1.3 The Maximum Contiguous Subsequence Sum Problem 33 6.1.4 Official Big-oh Rules 34 6.1.5 Logarithms 34 Solutions To Exercises 34 Exam Questions 37 Chapter 7 7.1 7.2 7.3 Design Patterns Key Concepts and How To Teach Them 31 5.1.1 Must-Teach Patterns 31 Solutions To Exercises 31 Exam Questions 32 Chapter 6 6.1 25 Key Concepts and How To Teach Them 25 4.1.1 The Concept of Inheritance and Polymorphism 25 4.1.2 Public, Private, and Protected Members and Inheritance 26 4.1.3 Static vs. Dynamic Binding 26 4.1.4 Default Constructor, Copy Constructor, and Copy Assignment Operator 26 4.1.5 Abstract Classes 27 4.1.6 Tricky Details 27 Solutions To Exercises 27 Exam Questions 28 Chapter 5 5.1 Inheritance Recursion 49 Key Concepts and How To Teach Them 49 8.1.1 What is Recursion? 49 8.1.2 Proof by Induction 49 8.1.3 Basic Recursion 50 43 8.2 8.3 8.1.4 Numerical Applications 50 8.1.5 Divide and Conquer 50 8.1.6 Dynamic Programming and Backtracking 50 Solutions To Exercises 51 Exam Questions 54 Chapter 9 9.1 9.2 9.3 10.2 10.3 11.2 11.3 67 Fun and Games 71 Key Concepts and How To Teach Them 71 11.1.1 Word Search Puzzle 71 11.1.2 Tic-tac-toe 71 Solutions To Exercises 72 Exam Questions 73 Chapter 12 12.1 Randomization Key Concepts and How To Teach Them 67 10.1.1 Linear Congruential Generators 67 10.1.2 Permutation Generation 67 10.1.3 Randomized Algorithms 68 Solutions To Exercises 68 Exam Questions 69 Chapter 11 11.1 59 Key Concepts and How To Teach Them 59 9.1.1 Motivation for Sorting 59 9.1.2 Insertion Sort Analysis 60 9.1.3 Shellsort 60 9.1.4 Mergesort 60 9.1.5 Quicksort 60 9.1.6 Selection 60 9.1.7 Lower Bound for Sorting 60 9.1.8 Indirect sorting 61 Solutions To Exercises 61 Exam Questions 64 Chapter 10 10.1 Sorting Stacks and Compilers 75 Key Concepts and How To Teach Them 75 12.1.1 Balanced Symbol Checker 75 12.1.2 Infix to Postfix Conversion 75 12.2 12.3 Solutions To Exercises 76 Exam Questions 79 Chapter 13 13.1 13.2 13.3 14.2 14.3 15.2 15.3 16.2 16.3 85 Graphs and Paths 89 Key Concepts and How To Teach Them 89 15.1.1 Definitions and Implementation 89 15.1.2 Unweighted Shortest Paths 90 15.1.3 Positive Weighted Shortest Paths 90 15.1.4 Negative Weighted Shortest Paths 90 15.1.5 Acyclic Graphs 90 Solutions To Exercises 91 Exam Questions 93 Chapter 16 16.1 Simulation Key Concepts and How To Teach Them 85 14.1.1 Josephus Problem 85 14.1.2 Discrete-event Simulation 85 Solutions To Exercises 86 Exam Questions 87 Chapter 15 15.1 81 Key Concepts and How To Teach Them 81 13.1.1 Huffman’s Algorithm 81 13.1.2 Cross-reference Generator 81 Solutions To Exercises 82 Exam Questions 83 Chapter 14 14.1 Utilities Stacks and Queues 97 Key Concepts and How To Teach Them 97 16.1.1 Array-based Stack 97 16.1.2 Array-based Queue 97 16.1.3 Linked list-based Stack 97 16.1.4 Linked list-based Queue 98 16.1.5 Double-ended Queue 98 Solutions To Exercises 98 Exam Questions 100 Chapter 17 17.1 17.2 17.3 18.2 18.3 19.2 19.3 20.2 20.3 109 Binary Search Trees 117 Key Concepts and How To Teach Them 117 19.1.1 The Basic Binary Search Tree 117 19.1.2 Order Statistics 118 19.1.3 AVL Trees, Red-Black Trees, and AA-trees 118 19.1.4 STL set and map 118 19.1.5 B-trees 118 Solutions To Exercises 118 Exam Questions 123 Chapter 20 20.1 Trees Key Concepts and How To Teach Them 109 18.1.1 General Trees and Recursion 109 18.1.2 Binary Trees and Recursion 109 18.1.3 Tree Traversal 110 Solutions To Exercises 110 Exam Questions 113 Chapter 19 19.1 103 Key Concepts and How To Teach Them 103 17.1.1 Basic Ideas: Header Nodes and Iterator Classes 103 17.1.2 Implementation Details 103 17.1.3 Doubly Linked Lists and Circular Linked Lists 104 17.1.4 Sorted Linked Lists 104 17.1.5 The STL list Implementation 104 Solutions To Exercises 104 Exam Questions 106 Chapter 18 18.1 Linked Lists Hash Tables 127 Key Concepts and How To Teach Them 127 20.1.1 Basic Ideas 127 20.1.2 Hash Function 127 20.1.3 Linear Probing 127 20.1.4 Quadratic Probing 128 20.1.5 Other Implementations 128 Solutions To Exercises 128 Exam Questions 131 Chapter 21 21.1 21.2 21.3 Key Concepts and How To Teach Them 135 21.1.1 Binary Heap and Heapsort 135 Solutions To Exercises 136 Exam Questions 139 Chapter 22 22.1 22.2 22.3 143 Merging Priority Queues 147 Key Concepts and How To Teach Them 147 Solutions To Exercises 147 Exam Questions 149 Chapter 24 24.1 24.2 24.3 Splay Trees Key Concepts and How To Teach Them 143 Solutions To Exercises 143 Exam Questions 144 Chapter 23 23.1 23.2 23.3 A Priority Queue: The Binary Heap The Disjoint Set Class 151 Key Concepts and How To Teach Them 151 Solutions To Exercises 151 Exam Questions 154 APPENDIX 157 Appendix A Sample Syllabi 159 Appendix B Sample Assignments 163 135 Preface This Instructor’s Resource Manual provides additional material for instructors to use in conjunction with Data Structures, and Problem Solving with C++, second edition. Each chapter in the text has a corresponding chapter in this manual that consists of: • A section on the important concepts and how to teach them • Solutions to many of the In Short and In Theory questions, as well as some comments for some of the In Practice questions • Multiple choice questions I have attempted to avoid redundancy. As a result, common errors, which are already listed in the text for each chapter, are not repeated. You should be sure to review these errors with your students. Also, I have not repeated material already stated in the book’s preface. A minimal set of multiple choice questions is provided. It is easy to generate additional questions from both these multiple choice questions (for example, replace inorder with postorder, and you have a different question in Chapter 18) and from some of the in chapter exercises. It is also a simple matter to design true/ false questions or fill in questions based on what is provided. My own preference is to give three types of questions on an exam: long answer (write a code fragment...), simulation (show the data structure after the following operations...), and questions. Of course if you have very large sections, grading this might be too time consuming. As mentioned in the textbook, the source code is available online. I have not included any additional directly compilable code in this supplement. Appendix A provides two syllabii: One for the separation approach, and one for the traditional approach. Eight assignments are described in Appendix B. Many many more are sugested as Programming Projects throughout the text. E-mail Please send comments and error reports to [email protected]. Eventually, my home page http://www.fiu.edu/~weiss will maintain an updated error list and additional notes. Chapter 1 Pointers, Arrays, and Structures 1.1 Key Concepts and How To Teach Them This chapter introduces several concepts: • • • • • • basic arrays (first-class arrays, using vector) basic strings (using string) pointers dynamic allocation reference variables and parameter passing mechanisms structures Depending on the students’ background, some or even all of this chapter could be skipped, but I recommend at least a quick review of the chapter in all circumstances. Students who have not had C or C++ should go through the entire chapter slowly. It is easy for students to think they understand the material in this chapter based on an earlier course, and my experience is that they do not. 1.1.1 Arrays Modern C++ arrays use the standard vector class and avoids the C-style array. I like to explain the idea of using a library class, so as to lead in to Chapter 2. Remind students that array indexing starts at 0 and goes to size()-1. Off-byone errors are very common; make sure the students are aware of these possibilities, and that the standard vector is not bounds checked (we write a better one in Chapter 3). Explain parameter passing. Finally discuss the push_back idea. I have found that push_back is easy for students to understand. 1.1.2 Strings There is not much to do here; avoid C-style strings. You may prefer to do strings before arrays. 1.1.3 Pointers Draw the usual memory pictures and emphasize that a pointer object is an object that stores a memory address. Go through as many examples as you can to distinguish between accessing a pointer and dereferencing it. Note: The NULL constant is defined in several header files, including stdlib.h. 1.1.4 Dynamic Allocation This is here to avoid forward references in the text. You may prefer to delay this material until linked lists are discussed. Or you can preview the idea of new and delete, and explain the problems of stale pointers and double-deletion. Explain the term memory leak. 1.1.5 Reference Variables and Parameter Passing Mechanisms The key topic here is the distinction between call-by-value, call-by-reference, and call-by-constant reference. Emphasize over and over again, that there are really three forms of parameter passing and that the const is not merely window dressing. Here are my rules: • Call by value: used for IN parameters for built-in types • Call by constant reference: used for IN parameters for class types • Call by reference: used for IN OUT parameters Many students insists on passing int objects by constant reference; this is wrong! It induces the overhead of pointer indirection when the reference is accessed inside the function. Many students get confused about passing pointers. When a pointer object is passed, the value of the object is an address. Passing a pointer by reference means that where the pointer points at could change. This is useful for resizing dynamically allocated C-style arrays and also in binary tree updates. 1.1.6 Structures This is a quickie opener to the class discussion. A C-style structure achieves the grouping of data, but does not provide for information hiding or encapsulation of functionality. Even so, some issues become evident and are worth discussing: 1. Structures should be passed either by reference or constant reference. 2. Deep vs. shallow copy. 3. Quick introduction to the linked list, C-style. 1.2 Solutions To Exercises In Short 1.1 1.2 1.3 1.4 1.5 1.6 Pointers can be declared and initialized, they can be assigned to point at an object, they can be dereferenced, they can be involved in arithmetic. The address-of operator can be applied to a pointer in the same manner as any other object. (a) Yes; (b) Both have the same value as A; (c) *ptrPtr=&b; (d) No; these objects have different types. (a) Yes as long as x is an object. (b) No, because x might not be a pointer. (a) the address where a is stored; (b) the value of a (5); (c) 1; (d) this is a type mismatch, and if accepted by the compiler is most likely 0; (e) the address where ptr is stored; (f) illegal, because a is not a pointer; (g) the value of a (5); (h) the value of a (5). (a) a is a member of type int in struct S and b is a member of type pointer to S in struct S; (b) z is of type S; (c) x is of type pointer to S; (d) y is of type array of 10 S; (e) u is of type array of 10 pointer to S; (f) x>a is of type int; (g) x->b is of type pointer to S; (h) z.b is of type pointer to S; (i) z.a is of type int; (j) *z.a is illegal because z.a is not a pointer type; (k) (*z).a is illegal because z is not a pointer type; (l) (this question should not be here) x->b-z.b is a subtraction of pointers and is thus of type int; (m) y->a is illegal; (n) y[1] is of type S; (o) y[1].a is of type int; (p) y[1].b is of type pointer to S; (q) u[2] is of type pointer to S; (r) *u[2] is of type S; (s) u[2]->a is of type int; (t) u[2]->b is of type pointer to S; (u) u[10] is of type pointer to S but is past the declared bounds of u; (v) &z is of type pointer to S; (w) &x is of type pointer to pointer to S; (x) u is of type array of pointer to S; (y) y is of type array of S. The picture below reflects a, b, and c after the declarations. The statement b=5 changes a to 5 and then c=2 changes a to 2. b a = 3 c 1.7 1.8 1.3 1.1. This is perfectly legal. However if the const is moved from the second declaration to the first, then the declaration and initialization of b would be illegal. /* begins a comment. Exam Questions For the declarations below, which statement is illegal? int *ptr1; int *ptr2; int a = 5; a. b. c. d. e. 1.2. ptr1 = ptr2; ptr1 = a; ptr1 = &a; *ptr1 = *ptr2; *ptr1 = a; For the declarations below, which expression is true if ptr1 and ptr2 point at the same object? int *ptr1; int *ptr2; a. b. c. d. e. 1.3. ptr1 == ptr2 *ptr1 == *ptr2 *(ptr1 == *ptr2) &ptr1 == &ptr2 None of the above For the declaration below, what is the type of *a? const int *a; a. b. c. d. e. int const int int * const int * none of the above 1.4. Call by value Call by reference Call by constant reference All of the above None of the above A shallow copy refers to a. b. c. d. e. 1.7. A local array is deleted. A dynamically allocated object is deleted. A dynamically allocated object is no longer referenced. Two pointers point at the same object. A dynamically allocated object is deleted twice. Which of the following parameter passing mechanisms can be used to alter a parameter? a. b. c. d. e. 1.6. A memory leak occurs when: a. b. c. d. e. 1.5. the copying of small objects the copying of pointers the copying of objects that are being pointed at the copying of basic types, such as integers call by value Exogenous data is a. a small member of a structure b. a large member of a structure c. an object that is not part of the structure, but is pointed at by the structure d. a global variable e. the entire structure 1.8. If f is a member of structure S, and p is of type pointer to S, then which expression must be legal? a. b. c. d. e. p.f p->f *p.f s.f More than one of the above 1.9. What is the result of the following? int x = 5; int & ref = x; ref++; a. b. c. d. e. 1.10. It increments x It increments ref It increments *ref It increments &ref It is illegal What is the result of the following? int x = 5; int *ptr = &x; int * & ref = ptr; *ref++; a. b. c. d. e. It increments ptr It increments ref It increments x It increments &ref It is illegal Answers to Exam Questions 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. B A B C B B C B A A
© Copyright 2024