1、数据结构与算法分析C版答案 Modified by JEEP on December 26th, 2020.数据结构与算法分析C版答案Data Structures and Algorithm 习题答案Preface ii 1 Data Structures and Algorithms 1 2 Mathematical Preliminaries 5 3 Algorithm Analysis 17 4 Lists, Stacks, and Queues 23 5 Binary Trees 32 6 General Trees 40 7 Internal Sorting 46 8 File P
2、rocessing and External Sorting 54 9Searching 58 10 Indexing 64 11 Graphs 69 12 Lists and Arrays Revisited 76 13 Advanced Tree Structures 82 i ii Contents 14 Analysis Techniques 88 15 Limits to Computation 94 Preface Contained herein are the solutions to all exercises from the textbook A Practical In
3、troduction to Data Structures and Algorithm Analysis, 2nd edition. For most of the problems requiring an algorithm I have given actual code. In a few cases I have presented pseudocode. Please be aware that the code presented in this manual has not actually been compiled and tested. While I believe t
4、he algorithms to be essentially correct, there may be errors in syntax as well as semantics. Most importantly, these solutions provide a guide to the instructor as to the intended answer, rather than usable programs. 1 Data Structures and Algorithms Instructors note: Unlike the other chapters, many
5、of the questions in this chapter are not really suitable for graded work. The questions are mainly intended to get students thinking about data structures issues. This question does not have a specific right answer, provided the student keeps to the spirit of the question. Students may have trouble
6、with the concept of “operations.” This exercise asks the student to expand on their concept of an integer representation. A good answer is described by Project , where a singly-linked list is suggested. The most straightforward implementation stores each digit in its own list node, with digits store
7、d in reverse order. Addition and multiplication are implemented by what amounts to grade-school arithmetic. For addition, simply march down in parallel through the two lists representing the operands, at each digit appending to a new list the appropriate partial sum and bringing forward a carry bit
8、as necessary. For multiplication, combine the addition function with a new function that multiplies a single digit by an integer. Exponentiation can be done either by repeated multiplication (not really practical) or by the traditional (log n)-time algorithm based on the binary representation of the
9、 exponent. Discovering this faster algorithm will be beyond the reach of most students, so should not be required. A sample ADT for character strings might look as follows (with the normal interpretation of the function names assumed). Chap. 1 Data Structures and Algorithms Some In C+, this is 1 for
10、 s1s2; 0 for s1=s2; int strcmp(String s1, String s2) Ones compliment stores the binary representation of positive numbers, and stores the binary representation of a negative number with the bits inverted. Twos compliment is the same, except that a negative number has its bits inverted and then one i
11、s added (for reasons of efficiency in hardware implementation). This representation is the physical implementation of an ADT defined by the normal arithmetic operations, declarations, and other support given by the programming language for integers. An ADT for two-dimensional arrays might look as fo
12、llows. Matrix add(Matrix M1, Matrix M2); Matrix multiply(Matrix M1, Matrix M2); Matrix transpose(Matrix M1); void setvalue(Matrix M1, int row, int col, int val); int getvalue(Matrix M1, int row, int col); List getrow(Matrix M1, int row); One implementation for the sparse matrix is described in Secti
13、on Another implementation is a hash table whose search key is a concatenation of the matrix coordinates. Every problem certainly does not have an algorithm. As discussed in Chapter 15, there are a number of reasons why this might be the case. Some problems dont have a sufficiently clear definition.
14、Some problems, such as the halting problem, are non-computable. For some problems, such as one typically studied by artificial intelligence researchers, we simply dont know a solution. We must assume that by “algorithm” we mean something composed of steps are of a nature that they can be performed b
15、y a computer. If so, than any algorithm can be expressed in C+. In particular, if an algorithm can be expressed in any other computer programming language, then it can be expressed in C+, since all (sufficiently general) computer programming languages compute the same set of functions. The primitive
16、 operations are (1) adding new words to the dictionary and (2) searching the dictionary for a given word. Typically, dictionary access involves some sort of pre-processing of the word to arrive at the “root” of the word. A twenty page document (single spaced) is likely to contain about 20,000 words.
17、 A user may be willing to wait a few seconds between individual “hits” of mis-spelled words, or perhaps up to a minute for the whole document to be processed. This means that a check for an individual word can take about 10-20 ms. Users will typically insert individual words into the dictionary inte
18、ractively, so this process can take a couple of seconds. Thus, search must be much more efficient than insertion. The user should be able to find a city based on a variety of attributes (name, location, perhaps characteristics such as population size). The user should also be able to insert and dele
19、te cities. These are the fundamental operations of any database system: search, insertion and deletion. A reasonable database has a time constraint that will satisfy the patience of a typical user. For an insert, delete, or exact match query, a few seconds is satisfactory. If the database is meant t
20、o support range queries and mass deletions, the entire operation may be allowed to take longer, perhaps on the order of a minute. However, the time spent to process individual cities within the range must be appropriately reduced. In practice, the data representation will need to be such that it acc
21、ommodates efficient processing to meet these time constraints. In particular, it may be necessary to support operations that process range queries efficiently by processing all cities in the range as a batch, rather than as a series of operations on individual cities. Students at this level are like
22、ly already familiar with binary search. Thus, they should typically respond with sequential search and binary search. Binary search should be described as better since it typically needs to make fewer comparisons (and thus is likely to be much faster). The answer to this question is discussed in Cha
23、pter 8. Typical measures of cost will be number of comparisons and number of swaps. Tests should include running timings on sorted, reverse sorted, and random lists of various sizes. Chap. 1 Data Structures and Algorithms The first part is easy with the hint, but the second part is rather difficult
24、to do without a stack. a) bool checkstring(string S) int count = 0; for (int i=0; ilength(S); i+) if (Si = () count+; if (Si = ) if (count = 0) return FALSE; count-; if (count = 0) return TRUE; else return FALSE; b) int checkstring(String Str) Stack S; int count = 0; for (int i=0; i 0. It is symmetric since xy = yx. It is transitive since any two members of the given class satisfy the relationship. 5 Chap. 2 Mathematical Preliminaries (d) This is not an equivalance relation sinc
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1