Download presentation
Presentation is loading. Please wait.
1
Homework - Chapter 03 Solution
2
Problem 1 Rewrite function Push and Pop (Program 3.10 and 3.12) using an additional variable lastOp as discussed on Page 146. The queue should now be able to use all positions to hold up elements. The complexity of each of your functions should be O(1) (exclusive of the time taken to double queue capacity when needed).
3
Solution Suppose lastOp is of string type. template <class T> void Queue<T>::Push(const &x) { if (rear == front && lastOp == “INSERT”) Resize(); rear = (rear + 1) % capacity; queue[rear] = x; lastOp = “INSERT”; };
4
Solution template <class T> void Queue<T>::Pop() { if (rear == front && lastOp == “DELETE”) //Empty throw “Queue is empty. Cannot delete.” front= (front + 1) % capacity; queue[front].~T(); lastOp = “DELETE”; };
5
Problem 2 Please implement the class Queue by using stacks.
Suppose that you start with an empty stack. After a sequence of n inqueues (insertion) and dequeues (deletion) operations, what is the average time complexity for each operation. please reason your answer.
6
Solution class Queue { public: Queue(); ~Queue();
void Push(data value); data Pop(); private: Stack S1, S2; };
7
Solution Queue::Queue() { Initialize S1 and S2; } Queue::~Queue() deallocate S1 and S2; void Queue::Push(data value) { S1.Push(value); } data Queue::Pop() if (S1.IsEmpty()) throw exception; while (!S1.IsEmpty()) S2.Push(S1.Pop()); data delNode = S2.Pop(); while (!S2.IsEmpty()) S1.Push(S2.Pop()); return delNode;
8
Solution Insertion的時間複雜度: O(1). Deletion的時間複雜度: O(S), 假設S代表queue當中的個數。
考慮n次的新增與刪除,其中我們令新增執行了i次,刪除總共有n-i次。由於刪除的時間複雜度和queue當中的個數有關,就worst case而言,我們可以連續做完i次都新增後,再連續作n-i次刪除。 時間複雜度為 O(i) + (O(i)+O(i-1)+…+O(i-(n-i)) < O(i)+O(i)+O(i-1)+….+O(1) = O(i2) 平均的時間複雜度為
9
Problem 3 Given the following maze:
1 2 3 4 5 6 7 8 9 entrance exit Trace out the action of Program 3.16 on the maze and find a path through the maze Rewrite Program 3.16 so that it can find out the shortcut.
10
Solution The path is (1,1) →(2,2) →(2,3) →(3,3) →
4 5 6 7 8 9 The path is (1,1) →(2,2) →(2,3) →(3,3) → (4,4) →(3,5) →(2,5) →(1,6) →(2,7) → (3,6) →(4,6) →(5,5) →(6,6) →(7,7) → (8,8) →(8,9)
11
Solution void Path(const in m, const int p) { ….
//if a path is found, the points are stored in the stack. //Then, invoke a function to print out the shortcut. ShortCut(m, p, maze, stack); }
12
Shortcut(m, p, maze, stack) { //step 是一個2D的陣列,step[i][j]記錄一個整數 //用來代表(i,j)會被原路徑中會第幾個點被拜訪到 int step[m][p]; //用來標記路徑 int mark[m][p]; int length = 1; //用來記錄目前路徑走到第幾點 Stack path, result; Initialize step[i][j] with a large number (MAX) for each (i, j); Initialize mark[i][j] with 0 for each (i, j); while (!stack.IsEmpty()) { Let p=(x, y) be a point popped from stack; mark[x][y] = 1; path.Push(p); } previous = path.Pop(); //The first point result.Push(previous); while (!path.IsEmpty()) if (step[x][y] < length) then //表示有更快的方法可以走到p for (i = length; i>step[x][y]; i--) { //pop掉之前走過的 del = result.Pop(); for each t =(a, b) where t denotes any neighbor of del do if step[a][b] equals to i then Restore step[a][b] to MAX; end for length = step[x][y]; end if result.Push(p); //走到p for each q = (m, n), where q is neighbor of p do //將p的每個鄰居以現有的長度(length)來標記 if (q is not previous and mark[m][n] equals to 1 and step[m][n] >= length) step[m][n] = length;
13
Problem 4 Given an infix expression
6 / 2 * * 5 Use tables like Section and Figure 3.16 to describe how you convert it into postfix notation and how you evaluate its value. Write an algorithm to convert an infix expression into prefix notation.
14
Solution 6 / 2 * 3 + 4 * 5 Next Stack Output none empty 6 Empty / 2
6 2 * 6 2 / 3 6 2 / 3 + 6 2 / 3 * 4 6 2 / 3 * 4 +* 5 + * 6 2 / 3 * 4 5 6 2 / 3 * 4 5 * +
15
Solution 6 2 / 3 * 4 5 * + Operations Postfix Notation 3 = 6 / 2
3 3 * 4 5 * + 9 4 5 * + 9 = 3 * 3 20 = 4 * 5 9 20 + 29 = 29
16
void InfixToPrefix (expression e)
{ Stack S; Stack Output; Reverse the expression e; //for example, 2+4*3 becomes 3*4+2 for (i=0; e[i] is not the end of string; i++) { if (e[i] is an operand) Output.Push(e[i]; else if (e[i] is ‘)’) S.Push(e[i]); else if (e[i] is not ‘(’) { while (S is not empty) { y = S.Pop(); if (y is not ‘(‘ and the priority of y > the priority of e[i]) Output.Push(y); else { S.Push(y); break; } else { //e[i] == ‘(’ if (y != ‘)‘) else Pop out all the elements in S and push them into the stack Output; while (!Output.IsEmpty()) print out Output.Pop();
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.