1. (30 points) In this problem, and this problem only, please choose among the following items and show your answers by the item labels.
[A] \(O(1)\) [B] \(O(n)\) [C] \(O(\log n)\) [D] \(O(n\log n)\)
The vector data structure is basically a flexible-size array, i.e., the number of elements inside the vector can be unlimited. The vector data structure can be implemented with either the usual fixed-length array or a singly linked list.
The vector can be implemented by a fixed-length array with an auxiliary length information without wasting any other space. Then, accessing an element within the vector by index is trivial. But in the worst case, when an element is inserted into the vector, a new array needs to be created and all the contents in the original array need to be moved; similarly, when an element is removed from the vector, the creating and moving steps need to be performed, too.
The vector can also be implemented by a singly linked list without wasting any other space. Then, inserting an element into or removing an element from the vector by index does not need moving any contents.
Assume there are \(n\) elements in a vector \(V\), what is the asymptotic worst-case running time for each operation under different implementations?
| array | linked list | |
|---|---|---|
| Access(V,i) | (1) | (2) |
| Insert(V,x,i) | (3) | (4) |
| Delete(V,i) | (5) | (6) |
Here \(Access(V,i)\) returns the element at the \(i\)-th position in \(V\), \(Insert(V,x,i)\) inserts an element \(x\) into the \(i\)-th position in \(V\), and \(Delete(V,i)\) deletes an element at the \(i\)-th position in \(V\). Both \(Insert\) and \(Delete\) will change the size of \(V\).
Besides the useful singly linked list, doubly linked list is a more convenient data structure, since a node can have two links point to both its successor and predecessor. Consider a list \(L\) that consists of \(n\) objects with one root pointer and one tail pointer pointing to its first and last object. What is the asymptotic worst-case running time for each dynamic-set operation listed in the following table?
| singly linked list | doubly linked list | |
|---|---|---|
| Search(L,k) | (7) | (8) |
| Remove(L,p) | (9) | (10) |
Here \(Search(L,k)\) returns the object whose key equals \(k\) in \(L\), and \(Remove(L,x)\) removes a node pointed by pointer \(p\) from \(L\).
(1) array 用 index 直接算位址 → [A]
(2) linked list 要從 head 一個一個走到第 \(i\) 個 → [B]
(3) array insert:要建新 array、把 \(n\) 個元素搬過去 → [B]
(4) linked list insert 不用搬資料,但要先走到第 \(i\) 個位置(最壞 \(n\) 步) → [B]
(5) array delete:同樣要重建、搬動 → [B]
(6) linked list delete:先走到第 \(i\) 個位置 → [B]
(7) singly linked list 依 key 找,最壞整串走完 → [B]
(8) doubly linked list 也只能一個一個比 → [B]
(9) \(Remove(L,p)\),singly:要改 \(p\) 前一個節點的 next,但沒有 prev 指標,得從 root 走到 \(p\) 前面找 predecessor → [B]
(10) doubly:用 \(p\)'s prev、next 直接接起來,tail/root 另外更新 → [A]
| (1) | (2) | (3) | (4) | (5) | (6) | (7) | (8) | (9) | (10) |
|---|---|---|---|---|---|---|---|---|---|
| A | B | B | B | B | B | B | B | B | A |
(1) A (2) B (3) B (4) B (5) B (6) B (7) B (8) B (9) B (10) A