DB101 Fall2008 Midterm
DB101 Fall2008 Midterm
Name
Section 1
1. (3 marks) A book store maintains a list of up to 20 requests for any book that is out of stock. The
information maintained for each request consists of the ISBN number of the book (10
characters), title of the book (up to 50 characters), and the number of requests received for the
book. Write suitable structure definition(s) to capture the above information. You may use the C
programming language syntax for this.
2. (3 marks) Compare arrays and linked lists on the basis of: (a) storage requirements (b) access
time.
4. (3 marks) State the properties that a collection of nodes and edges should satisfy in order for it to
be considered a tree.
5. (3 marks) Consider a linear list of nodes v(1) to v(10). Can this be considered to be a tree?
If so, what will be root node and which node(s) will be the leaf node(s)?
6. (3 marks) Consider a tree that consists of a set of nodes v(i) for i=1 to N where v(i)is
the parent of v(i+1) for i = 1 to N-1. Draw the tree corresponding to this description for
N=6.
9. (3 marks) Define the “heap property” that every node in a heap should satisfy.
10. (3 marks) Describe how hash tables can be implemented using an array of pointers.
Section 2
1. The next three questions are related to each other
a. (5 marks) Explain the output of the following algorithm with TWO examples:
ALGORITHM FSTRING ( INPUT_STRING, OUTPUT_STRING )
Begin
PUSH every character from INPUT_STRING into a stack S1
POP from stack S1 every character and store into OUTPUT_STRING
End
b. (5 marks) Explain the output of the following algorithm with TWO examples:
ALGORITHM TSTRING ( INPUT_STRING, OUTPUT_STRING )
Begin
FSTRING ( INPUT_STRING, TEMP_STRING )
FSTRING ( TEMP_STRING, OUTPUT_STRING )
End
Page 1 of 2
Roll Number
Name
c. (10 marks) Regular expressions are patterns that use special characters to represent
one or more characters. One such character is the star (*). Star represents zero or
more occurrences of the previous character. For example, the pattern ab*c*d matches
acd, abcd, abbccd, abbbbccccd, and so on.
Write an algorithm to generate strings based on the above rule. For this algorithm,
INPUT_PATTERN is the input regular expression pattern (like ab*c*d).
REPEAT_COUNT is the number of time the previous character should be repeated.
OUTPUT_STRING is the expanded string where every * is replaced by
REPEAT_COUNT occurrences of the previous character.
ALGORITHM GEN_STR ( INPUT_PATTERN, REPEAT_COUNT, OUTPUT_STRING )
Use a combination of a stack (call it S), FSTRING, and TSTRING in the algorithm.
2. (10 marks) Stacks generally impose a finite size. A variation of the standard stack called
LEAKY_STACK can be created in which instead of throwing an error when the stack is full, it
dismisses the item that is at the bottom of the stack and pushes the new item into the stack.
That is, the PUSH operation will never give an overflow error.
Write an algorithm for the LEAKY_PUSH operation into the LEAKY_STACK represented by S
using an array implementation of stacks. Assume array index range to be 1..N where N is array
size.
ALGORITHM LEAKY_PUSH ( S, ELEMENT )
a. (5 marks) Draw adjacency matrix representation for the graph given below:
v1 v2
v4 v3
a. (5 marks) Build a heap H by inserting elements in the given order: 35, 23, 45, 64, 36, 54,
24, 78. Show the heap structure after inserting each element.
b. (5 marks) Delete element from the heap H in the given order: 78, 45, 24. Show the
heap structure after deleting each element.
5. (10 marks) Compare splay trees with normal linked lists. Justify why splay trees are better than
linked lists for certain operations. (Hint: Recall my Moodle posting on this topic)
Page 2 of 2