

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


First Name: _______________ Last Name: _______________
This is a closed book examination.
(b) (5 points) Assume that a Node class is defined as shown below. Also assume that a function void visit(Node *) already exists, which performs some operation on a node (such as printing its value). Using an STL stack, write a function called levelorder , which performs a level order traversal of a binary tree. You need not show code to include the STL stack header file.
class Node{ public: int key; Node *P, *LC, *RC; // P: parent, LC: left child, // RC: right child };
void levelorder(Node *root) {
a. Draw the BST tree that results after the following sequence of operations on a BST tree that is initially empty: insert(9), insert(4), insert(1), insert(7), insert(0), insert(8), insert(3), insert(6), insert(2), insert(2.5), Delete(4), Delete(3), Delete(0).
b. Draw the AVL tree that results after the following sequence of operations on an AVL tree that is initially empty: insert(9), insert(4), insert(1), insert(7), insert(0), insert(8), insert(3), insert(2), insert(1.5), insert(10), insert(-1), insert((3.5), Delete(7).
c. Show the max-heap that results from applying the O(n) heap initialization algorithm that we discussed in class to the following array: 3, 6, 10, 5, 1, 9, 7, 4, 0, 2, 8. Draw the pointer representation.
Av.: Average, Amort.: amortized, WC: Worst case, AvAm.: Average amortized time
(b) (5 points) Consider a simple spam (junk email) filter as described below. We keep track of senders (say, the from field in the email) whose messages should be tagged as spam. Initially, no one is listed as a spammer. Each time the user marks an email as spam, we record that sender as a spammer. Each time we receive an email, if the sender has been recorded earlier as a spammer, then the message is tagged as spam. Suggest a suitable data structure to store the records of spammers. State any reasonable assumptions that you make, and justify your answer.
void BST::Delete(Node *n) { // Delete the node n. Assume n is a valid node.
push pop top/ front
erase push front
push back
pop front
pop back
search
vector x x x
Amort:
sorted vector x^ x^ x^ x^ x^ x
deque x x x x
Amort: Amort: x
stack x x x x x x
queue x x x x x x
BST WC: Av:
x x WC: Av:
x x x x WC: Av: AVL tree x x x x x x
heap x x x x x x
hash table
AvAm: (^) x x Av: x x x x Av:
doubly linked list
x x x