















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; Professor: Earls; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Spring 2011;
Typology: Exams
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















Final Examination
3 hours permitted
Print your name, netID, and lab section day/time neatly in the space provided below; print your name at the upper right corner of every page.
Name: NetID: Lab Section (Day/Time):
Problem Points Score Grader 1 16 2 40 3 64 4 15 5 15 6 15 7 15 Total 180
(a) You have a hash table class declared as follows: class HashTable { private: Array<pair<int, int> > table; public: // public function declarations }; The Array will be indexed from 0 through table.Size() - 1. The first value in each pair holds 0 if the cell is empty, 1 if the cell is valid, and -1 if the cell was deleted. The second value in each pair holds the key value in this cell (if the cell is a valid cell). Write the assignment operator for this class. (It’s okay to just write the code that would appear in the .cpp file; that is, you can just write the definition and assume the declaration occured in the “public function declarations” above section.
(a) You have an red-black tree composed of n nodes that each have three pointers. Each node points not only to its two children, but to its parent as well. (The root’s parent pointer, points to NULL, since the root does not have a parent. You have a pointer to the node in the red-black tree containing the minimum value in the tree. You want to travel from the minimum node to the root of the tree, printing each value you encounter along the way, and then travel from the root to the node containing the AVL tree’s maximum value, again printing each value you encounter along the way. Express the order of growth of the worst-case running time of this procedure, in Big-O notation, and explain convincingly why your answer is correct.
(b) The algorithm known as heapsort was briefly mentioned in lecture once. The algorithm sorts n integers by putting them all into a heap, and then calling DeleteMin repeatedly, thus pulling the integers out of the heap, in order from lowest to highest. Express the order of growth of the worst-case running time of heapsort, in Big-O notation, and explain convincingly why your answer is correct.
(d) You have an ordinary trie, holding n different words, all of which are no longer than L characters (counting the null character at the end of a word). You want to perform F find operations on the trie. Express the order of growth of the worst-case running time of this procedure (that procedure being, “perform F find operations”), in Big-O notation, and explain convincingly why your answer is correct.
(a) Given a singly-linked list, and a pointer current to a node in the singly-linked list, explain convincingly how you can implement InsertBefore in constant time.
(b) Imagine you have a sparse array with R rows and C columns, implemented by a “list of arrays”. That is, there’s a list, and for every row that has at least one non-default value, this list has a node for that row, containing the index of the row, and an array of every possible column in that row. If every row in the sparse array has at least one value, do we save any memory with this approach? Explain why or why not.
(d) Explain convincingly that for a red-black removal, case 2a works. That is, when the node or “NULL” which you’ve labelled “x” has a black sibling, and of that sibling’s two children, the one furthest from “x” is black and the one closest to “x” is red, explain what is done in that case, and why, and explain why we still have a legal red-black tree after this case.
(e) We looked at a depth-first-search-based algorithm for finding the topological sort of a graph. Explain what this algorithm was and why it worked.
(g) Consider a hash table where h(x) = x mod 11 and h 2 (x) = 7 − x mod 7. Insert the values 56, 27, 38, 25, 78, 50, and 45, in that order, into the hash table. Use double hashing to resolve collisions.
(h) If you were to insert the words cot, car, carpet, cow, and coworker into a Patricia tree, not counting the leaves, how many nodes would there be? Explain your answer.
You have the following ListNode class:
class ListNode { public: int element; ListNode* next; ListNode* prev; };
and a doubly-linked list made up of such nodes, with a ListNode pointer head to the first node and with the first node’s prev and the last node’s next equalling NULL. We will assume it is publicly accessible, rather than nested in a class, for this problem. Write a function ReverseParts which has two parameter and returns nothing. The first parameter will be a reference to a ListNode pointer. This pointer will point to the head node of a doubly-linked list (and thus would be NULL if the list were empty). This list will have the prev of the first node and the next of the last node both pointing to NULL. The second parameter will be a positive integer, which we will call n. This function should re- verse every set of n values. For example, if the list had been 5->3->1->10-14->22->67->4->NULL and n were 3 , then the resultant list should be 1->3->5->22->14->10->4->67->NULL (the set of two values at the end of the list likewise have their order reversed). You can assume that n will be less than or equal to the list size. Whatever linked list this results in, the head parameter should be pointing to the first node of that list when you are done.
void ReverseParts(ListNode*& head) { // your code goes here
Given the following class:
class TreeNode { public: int element; TreeNode* left; TreeNode* right; };
You have a tree built from the above node type. This tree is NOT a Binary Search tree, though it is a binary tree, by definition. You want a function Greater that takes two parameters. The first will be a pointer to a TreeNode. The second should be an integer value. The function should return an array of all values greater than the parameter integer. The Array
Array
(Verifier, continued)
(Complete Subgraph, continued)
You have a de la Briandais tree that stores strings made of lowercase letters. Assume that it does NOT use the Patricia tree optimization, and that it is composed of individual nodes of the following list node type:
class DeLaNode { public: char letter; DeLaNode* subtree; DeLaNode* nextInList; };
To indicate leaves, we just set the null-character cell in that level’s array, to point to leaf (which we’ll assume is a variable we’ve declared elsewhere). You want to write a method numPrefixes that has three parameters. The first is a pointer to a DeLaNode. The second is a positive integer n. The third is an integer holding the depth of this node. You want to return the number of unique prefixes of exactly length n that are represented in the trie. For example, if you had already inserted “car”, “carpet”, and “cotton”, then you have two unique prefixes of length three – namely, “car”, and “cot”.
int numPrefixes(DeLaNode* ptr, int n, int level) { // your code goes here