Presentation is loading. Please wait.

Presentation is loading. Please wait.

Solution. 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.

Similar presentations


Presentation on theme: "Solution. 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."— Presentation transcript:

1 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 void Queue ::Push(const &x) { if (rear == front && lastOp == “INSERT”) Resize(); rear = (rear + 1) % capacity; queue[rear] = x; lastOp = “INSERT”; };

4 Solution template void Queue ::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 1. Please implement the class Queue by using stacks. 2. 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 1. 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 2. 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(i 2 ) 平均的時間複雜度為

9 Problem 3 Given the following maze: 1.Trace out the action of Program 3.16 on the maze and find a path through the maze 2.Rewrite Program 3.16 so that it can find out the shortcut entrance exit

10 Solution 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()) { Let p=(x, y) be a point popped from stack; 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; end for }

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 * * 5 NextStackOutput noneemptynone 6Empty6 //6 2/6 2 **6 2 / 3*6 2 / / 3 * / 3 * 4 *+*6 2 / 3 * 4 5+ *6 2 / 3 * / 3 * 4 5 * +

15 Solution 6 2 / 3 * 4 5 * + OperationsPostfix Notation 3 = 6 / 2 9 = 3 * 3 20 = 4 * 5 29 = * 4 5 * *

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; } S.Push(e[i]); } else {//e[i] == ‘(’ while (S is not empty) { y = S.Pop(); if (y != ‘)‘) Output.Push(y); else break; } Pop out all the elements in S and push them into the stack Output; while (!Output.IsEmpty()) print out Output.Pop(); }


Download ppt "Solution. 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."

Similar presentations


Ads by Google