CS 351 – DATA STRUCTURES II EXAM #2 Practice This question

advertisement

CS 351 – DATA STRUCTURES II

EXAM #2 Practice

1.

This question concerns AVL-Trees: a.

(15 pts) Insert the following key values into an AVL-tree. Please redraw the tree after each rotation.

500, 300, 100, 150, 120, 250 b.

(10 pts) Delete 120 and then 100 from your result from part a. This should require a rotation; if it does not, delete other values until a rotation is needed.

2.

(10 pts) Suppose I have a class named “bstClass”, which implements a binary search tree. Suppose further, it has public methods “void insertValue(int newValue)”, “void deleteValue(int oldValue)”, “int searchValue(int oldValue)” and “void printTree()”. Write the code to implement a hash table with 100 buckets in which each bucket is a binary search tree. Give the private data members, a constructor (if necessary), and “void addToTable(int newHashVal)”. Use the standard MOD hash function.

3.

Recall that a binary min heap can be represented as an array. a.

(15 pts) Insert the following key values into a binary min heap using only the array implementation.

Show the array after each insertion.

100, 300, 200, 50, 210, 17, 40 b. (10 pts) Now show the array after deleting the three smallest values. Again, show the result after each delete.

4.

The beginning of the definition of a linked list class is given below. Assuming there are methods to add new elements, etc. : a.

(7 pts) Write a method for this class to add up all the dvalue’s in the linked list ITERATIVELY, i.e. use a while loop. b.

(8 pts) Write a method for this class to add up all the dvalue’s in the linked list RECURSIVELY, i.e. do not use any explicit looping construct. class linkedList { private: int dvalue; linkedList *next;

}

5.

Suppose I have a hash table with 10 buckets. a.

(10 pts) Given the numbers below, which of the following hash functions is best? Be sure to explain your answer. For all, use linear probing as the method of resolving conflicts. Here are the hash functions: i.

H1(X) = X MOD 10 ii.

H1(X) = (X DIV 10) MOD 10 iii.

H1(X) = (X DIV 100) MOD 10

Here are the numbers:

100, 592, 600, 455, 170, 330 b.

(7 pts) Show the final hash table after the insertions are done using your preferred solution from 5a. c.

(8 pts) Now delete 100 and then 600 from your hash table from part b. Be sure to show the table after each value is deleted.

Download