














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Unknown 1989;
Typology: Exams
1 / 22
This page cannot be seen from the preview
Don't miss anything!















Final Examination
3 hours permitted
Name: NetID: Lab Section (Day/Time):
Problem Points Score Grader 1 60 2 40 3 20 4 20 5 20 6 20 Total 180
(a) Explain how the middle node of a singly-linked list can be removed in constant time, given a pointer to that node.
(b) In a “perfect” skiplist, we expect to never have to traverse forward more than one node on any level. Why is this?
(e) How do we “mark a vertex known” in the heap implementation of Dijkstra’s Algorithm
(f) Given a graph that is both undirected, and connected (i.e. given any two vertices, there is a path between them in the graph), can breadth-first-search ever produce a spanning forest instead of a spanning tree? Justify your answer.
(g) Consider a hash table where h(x) = x mod 11 and h 2 (x) = 5 − x mod 5. Insert the values 80, 58, 30, 5, 21, and 27, in that order, into the hash table. Use double hashing to resolve collisions. We have provided the relevant values of the two hash functions, in the table below.
(h) How many “array nodes” (as opposed to leaves containing info records for our keys, rather than arrays) would there be in a Patricia Tree containing the words cart, car, carthage, cardio, and cartoon?
(j) For the given graph, run Prim’s algorithm, indicating in the table below the distances at each vertex at the end of each step (dv), and whether or not the vertex has been marked known yet at the end of each step (kv).
V dv kv dv kv dv kv dv kv dv kv dv kv dv kv dv kv A ∞ 0 B 0 0 C ∞ 0 D ∞ 0 E ∞ 0 F ∞ 0 G ∞ 0
(a) You have a pointer head to a doubly-linked list of n nodes, made up of nodes of the ListNode class on page 19. You need to do three things: (1) explain what the following code does, (2) tell us what the the order of growth of the worst-case running time of the following code is (in big-O notation), and (3) explain your answer to (2) in enough detail to be convincing. // you are given the pointer ‘‘head’’ to a list of size n ListNode* temp = NULL; ListNode* latestHead = NULL; while (head != NULL) { ListNode* toEnd = head; while (toEnd->next != NULL) toEnd = toEnd->next; if (toEnd->prev != NULL) toEnd->prev->next = NULL; else head = NULL; toEnd->prev = temp; if (temp != NULL) temp->next = toEnd; else latestHead = toEnd; temp = toEnd; }
(c) Suppose you want to add a vertex to graph of V vertices and E edges. Assume that this graph is implemented using an adjacency matrix of exactly the right size needed for the graph. Express (using big-O notation) the order of growth of the worst-case running time of this operation, in terms of V and E. Explain your answer in enough detail to be convincing.
(d) Explain why the time to lookup a value in a trie does not depend on how many values there are in the trie.
(Range Removal, continued)
You have the use of the Array, ListNode, and TreeNode classes seen on page 19 , as well as a standard Queue class with a no-argument constructor that initializes the Queue to be empty. You want to write a function levels that has one parameter, a pointer to TreeNode, which points to the root of a binary tree made of TreeNode objects. The function should return an Array object indexed from 0 to maxDepth, where maxDepth is the depth of the deepest leaf in the tree. For every index i between 0 and maxDepth, inclusive, the array cell at index i should point to a linked list containing every node of depth i in the binary tree, in order from left-to-right. For example, cell 0 of the array would point to a list containing the tree’s root node, cell 1 of the array would point to a list containing the root’s left and right children, in that order, and so on. You are also allowed to use one standard Queue, which has a no-argument constructor that initializes the Queue to be empty.
Array<ListNode> levels(TreeNode ptr) // your code goes here
You have the following classes:
class EdgeNode { class VertexRecord { public: public: int target; // index of target vertex int distance; int weight; // weight of edge int known; EdgeNode* next; // next node in list EdgeNode* edgePtr; }; };
We can implement graphs using the adjacency list implementation, by having a variable theGraph of type Array
void dijkstra(Array
(Dijkstra’s Algorithm, continued)
(Optimizing Tries, continued)
These nodes and classes are used on the exam:
template
void setBounds(theLo, theHigh); // resizes array to have lower bound // theLow and upper bound theHigh; any // values in that index range already, // remain in the resized array
int size(); // returns the number of values
Etype& operator[](int index) // returns the cell at parameter index // there are other member functions but // they are not used on this exam };
template <typename Etype1, typename Etype2> class pair { public: Etype1 first; Etype2 second; };
class ListNode { public: int element; ListNode* next; ListNode* prev; ListNode(int elem) {element = elem; next = NULL; prev = NULL;} };
class TreeNode { public: int element; TreeNode* left; TreeNode* right; TreeNode* parent; TreeNode(int elem) {element = elem; left = NULL; right = NULL; parent = NULL; } };