Академический Документы
Профессиональный Документы
Культура Документы
com
Please read the following instructions carefully before attempting any of the
questions:
The data items listed below are to be used to answer the following parts to this question: [30
pts]
23 46 79 21 55 33 44 57 56
a) Create a binary search tree for the data items drawing a diagram to show the final tree
after all items have been inserted. (5 points)
b) Write the contents of the tree using:
1. inorder traversal (5 points)
2. preorder traversal (5 points)
3. postorder traversal (5 points)
c) Create an AVL tree for the data items. Show each stage of the tree as it gets built along
with the single or double rotations (when needed). (10 points)
A B output(A) output(B)
B A enqueue(A) output(B) output(dequeue())
Suppose your input were ABC. Which of the six permutations of ABC can you generate using
enqueue(data), dequeue(), and output(data)? Show the operations that yield the
permutations that are possible. Explain why any impossible permutations are impossible (it's
not enough to show you've tried lots of combinations of operations). To get you started, you
can generate:
ABC output(A) output(B) output(C) Show how to produce the other 5 permutations ACB,
BAC, BCA, CAB, CBA given the input ABC.
Question No. 3 Marks : 5
You can insert as many stack.pop()'s as you like in the above sequence of stack.push's to get
a desired output. Which of the following cannot be an output?
1 123456
2 325416
3 342561
4 342615
5 342165
Which traversal gives a decreasing order of elements in a heap where the max element is
stored at the top? [5 pts]
1 pre-order
2 in-order
3 post-order
4 level-order
5 none of the above
Let heap stored in an array as A = [3, 8, 4, 9, 11, 5, 6, 10, 12, 13]. Assume that the index of
the first array cell is 1 and that this is also the root of the heap (i.e., element 3). In other
words, the root of the heap contains the minimum element. What is the result of inserting 7
into this heap? [10 pts]
1 [3, 7, 4, 9, 8, 5, 6, 10, 12, 13, 11]
2 [3, 4, 7, 9, 8, 5, 6, 10, 12, 13, 11]
3 [3, 7, 4, 9, 8, 5, 6, 10, 12, 11, 13]
4 [3, 4, 7, 9, 8, 5, 6, 10, 12, 11, 13]
5 [3, 8, 7, 9, 4, 5, 6, 10, 12, 11, 13]
For each part of the problem, name and justify one data structure that addresses the
problem. Your answer should not be more than 3 lines. You may choose from the following
list of data structures: [12 pts]
Stack Queue Binary search tree AVL tree Heap Hash table
(a) The data structure is initially empty. We then insert the values 2, 10, 12, 8, 6 and 4, in
that order. Now, the only element we can remove is 12. (3 points)
(b) The data structure is initially empty. We insert the values 2, 4, 6, 8, 10, 1 and 7, in that
order. If we want to remove an element, our only choice is 2 (3 points)
(c) The data structure initially contains n elements. We then insert elements 7, 14, 27, 68,
and 3. We may now find any element, in average case, in constant time (i.e., O(1)) time
(3 points)
(d) The data structure initially contains n elements. We then insert elements 2, 7, 5, 13, 11, 3
and 1, in that order. We may insert any element, in worst case, in time proportional to log
n (i.e., O(log n)) time (3 points)
Suppose you are given a pointer "thisnode" that points to some node in a singly linked list
(NULL terminated), as well as a stack which can hold pointers to list nodes. If x points to a
node in the list, the member "x->next" holds a pointer to the node following x in the list. In 20
words or less, describe what the following pseudo code does: [5 pts]
temp = thisnode->next
while (temp is not NULL)
{
push temp onto the stack
temp = temp->next
}
temp = thisnode
while(stack is not empty)
{
temp->next = pop off stack
temp = temp->next
}
temp->next = NULL
http://vujannat.ning.com
BEST SITE TO HELP STUDENTS
FINALTERM EXAMINATION
FALL 2006 Marks: 75
CS301 - DATA STRUCTURES (Session - 1 ) Time: 120min
StudentID/LoginID: ______________________________
Draw AVL Tree by following digits 15, 4, 13, 6, 17, 2, 11 and also perform necessary rotation,
while showing all the intermediate trees being created in the process. In each stage, the AVL
transformation should be conducted at a discrepancy that is farthest from the root.
Some operations are given. Show the output of the given operations step by step in form of
array.
Enqueue(13);
Enqueue(35);
Enqueue(11);
Enqueue(39);
Enqueue(9);
N = RemoveMin();
Enqueue( 51);
N = RemoveMin();
Enqueue(15);
Frequency Table
A) Create a Huffman tree to determine the binary codes for each character. (10)
B) Fill the codes into the table above. (2.5)
C) Encode the following sequence ABCDE. (2.5)
Following array is given which represents a min-heap. Insert 4 in the following array and
convert it into a min-heap again. Show process steps by drawing heap trees.
Create a hash table using hash table of size 10 i.e. (0-9). Insert the following values in the hash table
79, 76, 75, 56, 53, 47, 48, 63, 90 and 59.
If there is any collision then insert a node in front of collision node to put the value in table e.g.
► merge sort
► quick sort
► heap sort
Hash tables are very good if there are many insertions and deletions.
► True
► False
► True
► False
Question No: 10 ( Marks: 2 ) - Please choose one
When there is a collision, we try to find some other place in our array. This approach is called
► open addressing
► closed hashing
► merge sort
► quick sort
► heap sort
StudentID/LoginID: ______________________________
The new operation in C++ for dynamically allocating memory returns a pointer to an object it has
just created.
► True
► False
► True
► False
► True
► False
An unbalanced tree is one whose root has many more left descendents than right descendants.
► True
► False
► False
Write a function in C++ that will swap the second and third node in a singly linked list (having 5
nodes) by adjusting only the pointers (and not the data). You can use Node class and List class
methods (such as getNext, setNext, next, get) without writing them. You can assume that the Node
class and List class exists, i.e., do not write the code for these classes. The simple declaration of
Node class and List class is as follow,
class Node
{
public:
int get() { return object; };
void set(int object) { this->object = object; };
private:
int object;
Node * nextNode;
};
};
Push the characters ‘c’, ‘d’, ‘m’, ‘a’, ‘b’ into the stack in the given order. Pop two elements from
the stack one at a time. Then push two characters ‘f’, and ‘g’. Now pop all the characters. What is
the result?
Convert the infix expression 2+(9-3 *2) to postfix. Show the trace of the algorithm, i.e., the
stack, the infix expression and postfix expression using the following table pattern.
(a) Starting from the root node A, perform an In-order traversal of the binary tree below and write
the letters in the nodes that will result during the visitations.
(b) Write the nodes if a Pre-order traversal is performed starting with node A.
B C
D E
F G
H
Page 1 of 3
WWW.vujannat.ning.com
http://vujannat.ning.com
Largest Online Community of VU Students
Please read the following instructions carefully before attempting any of the questions:
1. This examination is closed book, closed notes, closed neighbors; any one found cheating will
get no grade.
2. Attempt all questions. Marks are written adjacent to each question.
3. Do not ask any questions about the contents of this examination from anyone.
a. If you think that there is something wrong with any of the questions, attempt it to the
best of your understanding.
b. If you believe that some essential piece of information is missing, make an
appropriate assumption and use it to solve the problem.
c. Wriite all steps, missing steps may lead to deduction of marks.
4. You are allowed to use any development environment like Dev C++ etc.
**WARNING: Please note that Virtual University takes serious note of unfair
means. Anyone found involved in cheating will get an `F` grade in this course.
(a) Starting from the root node A, perform a pre-order traversal of the binary tree below and write the
letters in the nodes that will result during the visitations.
(b) Write the nodes if an in-order traversal is performed starting with node A.
Consider the following problem: Given N elements in a data structure, find the middle element in
sorted order. This is also called the median. Note the elements are not necessarily in sorted order
unless the data structure forces them to be. For example, if the numbers are:
34 76 87 12 8 96 83
Then the middle element in sorted order is 76. If the elements had been sorted, 76 would be in the
middle.
For each of the following possible data structures assume the N elements are already in the data
structure. For each data structure, describe briefly in one or two lines an efficient algorithm to find the
middle element in sorted order (do not write code).
1. an unsorted array
2. a sorted linked list – (here the elements are already sorted)
3. a min heap
4. a balanced binary search tree
5. a hash table of size M
Using modulo 11 as the hash function "h(x) = x mod 11", show the contents of the hash table
(indexed by 0..10) after the following values are stored in order: 3,14,25,4, 37.
Show (pictorially) the result after each insertion.
class Employee {
public:
// Constructor functions
Employee();
Employee(const string Name, const int Years, const float Salary,
const bool Promote = True);
string GetName () const;
void GetAge (int &Age) const;
float GetSalary() const;
void Print() const;
void SetName(const string Name);
void SetAge(const int Age);
void SetSalary(const float Pay);
void SetPromotable(const bool Promote);
private:
bool getPromotability() const;
string employeeName;
int age;
float salary;
bool promotable;
};
// Functions
float ComputeAverageSalary (const Employee Workers[], const int NumWorkers)
{
float Sum = 0.0;
for (int i = 0; i < NumWorkers; i++)
{
// CODE FRAGMENT 1
}
return (Sum / NumWorkers);
};
// Main program
int main( )
{
// INITIALIZATION CODE
// CODE FRAGMENT 2
return 0;
}
a) Given these definitions above, determine which of the following are valid object creation
examples? (Circle Valid or Invalid)
b) Assume the Employee objects Amy and Bob have been properly created and initialized. Which of
the following are valid object usage examples? (Circle Valid or Invalid)
c) Assume the Employee objects Bob and Amy have been properly created and initialized. Which of
the following are valid object usage examples? (Circle Valid or Invalid)
f) In CODE FRAGMENT 2, how would you call ComputeAverageSalary() given an initialized array of
30 Employee objects called Staff.
(a) Show the result of inserting (in order) 10,8,6,2,4,1,7 into an initially empty BST. Draw the diagram
of resultant BST
(b) Give an order in which the elements should arrive to have the worst case (largest) height, as well
as the best case (minimum) height.
(d) Give a linear time algorithm to print all the nodes in a arbitrary tree in level order. That is, all
nodes at depth 0 are printed first (the root) followed by nodes of depth 1, then depth 2 and so on.
Build an efficient Huffman tree using the algorithm we discussed in class for the following:
Show all steps in building the tree. The counts for each character have already been calculated for
you. Also note the blank is represented by an underscore ( - ).
Page 3 of 3
www.vujannat.ning.com
Instructions
Please read the following instructions carefully before attempting any question:
1. The duration of this examination is 150 Mins.
2. This examination is closed book, closed notes, closed neighbors; any one found
cheating will get no grade.
3. Unless stated otherwise, all questions carry a single mark.
4. Do not ask any questions about the contents of this examination from anyone.
a. If you think that there is something wrong with any of the questions,
attempt it to the best of your understanding.
b. If you believe that some essential piece of information is missing, make an
appropriate assumption and use it to solve the problem.
5. You are allowed to use any development environment like Dev C++ etc.
o True
o False
Question No. 2 Marks : 02
In binary search tree (BST) every node has two or zero node.
o True
o False
Question No. 3 Marks : 02
o True
o False
Question No. 5 Marks : 02
o True
o False
Question No. 6 Marks : 15
This question concerns trees containing character data. When two characters need
to be compared, use normal alphabetic comparison, so that for example 'a' < 'g'.
[15 pts]
a) Suppose the characters 'f', 'a', 'r','t', 'z', 'h', 'p', 'e', 'u', 'o' are stored in a
Binary Search Tree (BST). draw a BST that contains these characters.
b) What will be the resulting tree after deleting the 'z', 't', 'h' in the given
sequence.
c) Draw a BST that is as short as possible and contains the characters 'g', 'v',
'q', 'm', 'b'.
Insert 55, 35, 57, 25, 37, 56, 41, 39 elements into AVL tree that is initially empty;
perform necessary rotation during insertion of elements. [10 pts]
Consider the following class definition for a binary tree of integers. [10
pts]
class binTree {
public:
btnode* root; // root of the bintree
btnode* current; // current node in the bintree
binTree();
bool isEmpty();
int CountInterior(); //count the number of
interior nodes
};
class btnode {
friend binTree;
int value;
binTree left; // left subtree
binTree right; // right subtree
public:
btnode(int value); // Constructor
bool isLeaf(); // True if this node is a leaf;
false otherwise
};
Complete the C++ code that examine whether a tree is complete tree or not.
(A binary tree is strictly binary tree or balance tree or complete tree if its
every node has two children nodes (i.e. left node and right node) or zero
child.)
bool completeTree(BinaryNode * rootNode)
{
Frequency Table
A) Create a Huffman tree to determine the binary codes for each character.
B) Fill the codes into the table above.
a. Remove the smallest element from the following array which represents a
min-heap. [15 pts]
b. Draw a min-heap tree of the elements 67, 98, 115, 23, 45.
WWW.vujannat.ning.Com
Connecting VU Students
Given the string "go go gophers". How Huffman coding is used to compress this
String?
Frequency Table
character frequency Huffman Codes
g ----------- -----------
o ----------- -----------
p ----------- -----------
h ----------- -----------
e ----------- -----------
r ----------- -----------
s ----------- -----------
Space ----------- -----------
A) Calculate Frequency of each character from given string (“go go gopher”) and fill
into the table
given above.
B) Create a Huffman tree from frequencies calculated in part ‘A’ of this question.
C) Fill the Huffman codes into the table above.
15
9 21
6 11 16 31
Hint: In step by step sorting you have to show passes for sorting, according to algorithm
technique provided to you. There are 6 passes involved in sorting array ‘BS’. You have to
show only two passes.
When inner loop complete its execution at once that is called one pass.
typedef struct bt_s { int key; struct bt_s *left, *right; } bt_t;
if(*current == T)
return current;
1 2 3 4 5 6 7 8 9 10 11 12
97 76 61 42 54 59 31 23 17 44 49
The constructor of the Hashtable class initializes data members and creates the
hashtable.
True
False
Draw an AVL Tree by following digits 1, 5, 6, 4, 18, 24 and also perform necessary rotation,
while showing all the intermediate trees being created in the process.
WWW.vujannat.ning.com
http://vujannat.ning.com
Largest Online Community of VU Students
MIDTERM EXAMINATION
Total Marks:86
SEMESTER FALL 2003
CS301-DATA STRUCTURE Duration: 60min
Instructions
Please read the following instructions carefully before attempting any question:
a. Only x and y
b. Only x and z
c. Only y and z
d. None of three the functions.
e. All three functions.
The Bag ADT is like the List ADT. The Bag ADT does not store items in any particular
order and it allows duplicates. Suppose that the Bag class is efficiently implemented with a fixed array
with a capacity of 4000. Insert appends the new item at the end of the array. Choose the best
description of b’s member variables size (count of items in the bag) and data (the array that holds
the actual items) after we execute these statements:
Bag b;
b.insert(5);
b.insert(4);
b.insert(6);
What will be the values of b.size and b.data after the statements?
a. b.size is 3, b.data[0] is 4, b.data[1] is 5, b.data[2] is 6
b. b.size is 3, b.data[0] is 5, b.data[1] is 4, b.data[2] is 6
c. b.size is 3, b.data[0] is 6, b.data[1] is 4, b.data[2] is 5
d. b.size is 3, b.data[0] is 6, b.data[1] is 5, b.data[2] is 4
In the linked list implementation of the stack class, where does the push member function
place the new entry on the linked list?
a. At the head
b. At the tail
c. After all other entries that are greater than the new entry.
d. After all other entries that are smaller than the new entry.
a. Neither changes
b. Only front pointer changes.
c. Only rear pointer changes.
d. Both change.
I have implemented the queue with a linked list, keeping track of a front pointer and a rear
pointer. Which of these pointers will change during an insertion into an EMPTY queue?
a. Neither changes
b. Only front pointer changes.
c. Only rear pointer changes.
d. Both change.
In a single function declaration, what is the maximum number of statements that may be
recursive calls?
a. 1
b. 2
c. n (where n is the argument)
d. There is no fixed maximum
class Throttle
{
public:
Throttle( ); C
Throttle(int size); C
void shut_off( );
void shift(int amount);
double flow( ) const; X
bool is_on( ) const;X
...
Question No: 14 Marks: 5
I am going to execute this code with THREE pushes and ONE pop:
Stack s;
s.push(1);
s.push(2);
s.push(3);
cout << s.pop( );
Suppose that the stack s is represented by a fixed-sized array. Draw the state of the private
member variables “data” and “top” of “s” after the above code:
Answer/Solution
0 1 2 3 4 5 6 7 8 9
1 2 Top 1
int counter( )
// Precondition:
// There is a line of input waiting to be read from cin.
// Postcondition:
// A line of input has been read from cin, up to but not
// including the newline character. The return value of
// the function is the number of times that the LAST
// character of the line appeared somewhere in this line.
// EXAMPLE
// Input: ABBXDXXZX The value returned by counter would
// be 4 for this input since there are 4 X’s in
// the input line.
{
int answer = 0;
Queue q;
Answer/Solution
int counter()
{
char a[100];
int i=0;
int answer=0;
Queue q;
cin.getline(a,98,'\n');
for(i=0;i<strlen(a);i++)
{
q.enqueue(a[i]);
}
i--;
while(!q.isEmpty())
{
if(a[i]==q.dequeue())
{
answer++;
}
}
return answer;
}
Suppose that s is represented by a singly linked linked list. Draw the linked list and the state of the
private member variables of s after the above code:
front_ptr
rear_ptr
Answer/Solution
2 3
rear_prt
Front_prt
{
public:
Node *next;
Node *prev;
int data;
};
class CList
{
public:
void insertHead(int);
void insertTail(int);
void removeHead();
void removeTail();
bool isEmpty();
bool find(int);
private:
Node *head;
Node *tail;
};
A. write the body of the member function insertHead which inserts a new element at the head
of the list.
void Clist::insertHead( int x )
{
B. write the body of the member function removeTail which removes the element at the tail of
the list.
void Clist::removeTail( int x )
{
Answer/Solution
void CList::insertHead(int x)
{
Node *newNode=new Node();
newNode->data=x;
newNode->next=NULL;
newNode->prev=NULL;
if(isEmpty())
head=tail=newNode;
else
{
newNode->next=head;
newNode->prev=NULL;
head->prev=newNode;
head=newNode;
}
) AB- (
AB-
+ AB- +
C AB-C +
/ AB-C +/
Question No: 19 Marks: 13
Here is a small binary tree:
MIDTERM EXAMINATION
Total Marks:86
SEMESTER FALL 2003
CS301-DATA STRUCTURE Duration: 60min
Instructions
Please read the following instructions carefully before attempting any question:
Is it possible for a member function of a class to activate another member function of the same class?
a. No.
b. Yes, but only public member functions.
c. Yes, but only private member functions.
d. Yes, both public and private member functions can be activated within another member function.
class quiz
{
public:
quiz( );
int f( );
int g( ) const;
private:
double score;
};
Which functions can carry out an assignment score=1.0; to the private ember variable score?
a. Both f and g can carry out the assignment.
b. f can carry out the assignment, but not g.
c. g can carry out the assignment, but not f.
d. Neither f nor g can carry out the assignment
In C++, when allocating an array of objects, what constructor is used to initialize all of the objects in the
array?
a. The automatic copy constructor.
b. The constructor specified at the declaration.
c. The default constructor.
d. None of the above.
a. 3
b. 4
c. 7
d. 8
e. Some other number
Suppose T is a binary tree with 14 nodes. What is the minimum possible depth of T?
a. 0
b. 3
c. 4
d. 5
Q4: Which value is in a node at level 2 and is the left child of a node at level 1? (1.5 Pt)
Q5: Which value is in a node at level 2 and is the right child of a node at level 1? (1.5 Pt)
Answer/Solution
0 1 2 3 4 5 6 7 8 9
1 2 Top 1
Question No: 15 Marks: 10
Complete the body of this function. Use a Queue of characters to store the input line as
it is being read.
int counter( )
// Precondition:
// There is a line of input waiting to be read from cin.
// Postcondition:
// A line of input has been read from cin, up to but not
// including the newline character. The return value of
// the function is the number of times that the LAST
// character of the line appeared somewhere in this line.
// EXAMPLE
// Input: ABBXDXXZX The value returned by counter would
// be 4 for this input since there are 4 X’s in
// the input line.
{
int answer = 0;
Queue q;
Answer/Solution
int counter()
{
char a[100];
int i=0;
int answer=0;
Queue q;
cin.getline(a,98,'\n');
for(i=0;i<strlen(a);i++)
{
q.enqueue(a[i]);
}
i--;
while(!q.isEmpty())
{
if(a[i]==q.dequeue())
{
answer++;
}
}
return answer;
}
Queue s;
s.insert(1);
s.insert(2);
s.insert(3);
cout << s.remove();
Suppose that s is represented by a singly linked linked list. Draw the linked list and the state of the
private member variables of s after the above code:
front_ptr
rear_ptr
Answer/Solution
2 3
rear_prt
Front_prt
Answer/Solution
Answer/Solution
MIDTERM EXAMINATION
Total Marks:86
SEMESTER FALL 2003
CS301-DATA STRUCTURE Duration: 60min
Instructions
Please read the following instructions carefully before attempting any question:
class MyClass
{
public:
...
void Func();
private:
int n;
};
what notation does the body of Func use to assign the value 3 to n?
(a) n = 3;
(b) MyClass.n = 3;
(c) MyClass::n = 3;
(d) someObject.n = 3;
(d) It can’t be done–n is private.
Suppose that the class declaration of SomeClass includes the following function prototype.
bool LessThan( SomeClass anotherObject );
Which of the following tests in the client code correctly compares two class objects alpha and beta?
Bag b;
b.insert(5);
b.insert(4);
b.insert(6);
What will be the values of b.size and b.data after the statements?
In the linked list implementation of the stack class, where does the push member function
place the new entry on the linked list?
(a) At the head
(b) At the tail
(c) After all other entries that are greater than the new entry.
(d) After all other entries that are smaller than the new entry.
Given a stack of n items, how many POP and PUSH operations need to be performed to
remove the item at its bottom?
(a) 0 POP operation and 0 PUSH operation
(b) 1 POP operation and 1 PUSH operation
(c) n POP operations and n PUSH operations
(d) n POP operations and n-1 PUSH operations
(e) Unknown
In the linked list implementation of the queue class, where does the insert member
function place the new entry on the linked list?
(a) At the head
(b) At the tail
(c) After all other entries that are greater than the new entry.
(d) After all other entries that are smaller than the new entry.
I have implemented the queue with a linked list, keeping track of a front pointer and a rear
pointer. Which of these pointers will change during an insertion into an EMPTY queue?
(a) Neither changes
(b) Only front pointer changes.
(c) Only rear pointer changes.
(d) Both change.
For public part of the Throttle declaration below, mark each function member header as
follows:
• Mark C for any constructor;
• mark X for any function that is forbidden from changing the throttles data fields.
class Throttle
{
public:
Throttle( );
Throttle(int size);
void shut_off( );
void shift(int amount);
double flow( ) const;
bool is_on( ) const;
...
Answer/Solution
class Throttle
{
public:
Throttle( ); C
Throttle(int size); C
void shut_off( );
void shift(int amount);
double flow( ) const; X
bool is_on( ) const; X
...
head -->
Answer/Solution
head 2 1
int counter()
{
char a[100];
int i=0;
int answer=0;
Queue q;
cin.getline(a,98,'\n');
for(i=0;i<strlen(a);i++)
{
q.enqueue(a[i]);
}
i--;
while(!q.isEmpty())
{
if(a[i]==q.dequeue())
{
answer++;
}
}
return answer;
. I am going to execute this code with THREE inserts (enqueue) and ONE remove ( dequeue ):
Queue s;
s.insert(1);
s.insert(2);
s.insert(3);
cout << s.remove( );
Suppose that queue s is represented by a circular array. Draw the state of the private member
variables “data” and “front” of s after the above code:
Answer/Solution
0 1 2 3 4 5 6 7 8 9
2 3 Front 1
{
public:
Node *next;
Node *prev;
int data;
};
class CList
{
public:
void insertHead(int);
void insertTail(int);
void removeHead();
void removeTail();
bool isEmpty();
bool find(int);
private:
Node *head;
Node *tail;
};
A. write the body of the member function insertHead which inserts a new element at the head
of the list.
void Clist::insertHead( int x )
{
B. write the body of the member function removeTail which removes the element at the tail of
the list.
void Clist::removeTail( int x )
{
Answer/Solution
void CList::insertHead(int x)
{
Node *newNode=new Node();
newNode->data=x;
newNode->next=NULL;
newNode->prev=NULL;
if(isEmpty())
head=tail=newNode;
else
{ newNode->next=head;
newNode->prev=NULL;
head->prev=newNode;
head=newNode;
}
}
Trace the running of the infix to postfix conversion algorithm on the infix expression
A * (B − C)/D
A*(B–C)/D
Symbol Postfix Stack
A A
* A *
( A *(
B AB *(
- AB *(-
C ABC *(-
) ABC- *(
ABC- *
/ ABC-* /
D ABC-*D
ABC-*D/
Answer/Solution
Please read the following instructions carefully before attempting any of the
questions:
1 Syntax error
2 Logical error
3 Problem with computer
4 Compiler error
5 Linker error
Question No. 2 Marks : 8
Write a program that asks the user to enter two numbers, obtains the two numbers from the
user, and prints the sum, product, difference, quotient and modulus of the two numbers.
In a structure we have
A computer program:
What is a pointer? What is call by value and call by reference? Briefly explain with examples.
1 open ()
2 read ()
3 get ()
4 seekg ()
5 seekp ()
The Super Cheap Computer Company sells its product, the Super-Cheap-PC, for Rs.490.00.
In addition, they sell disk drives for Rs.200.00, printers for Rs.75.00 and software packages
for Rs.50.00 each. If the customer's total bill would be above Rs.850.00 then the customer
gets a 15% discount. Write a program that prompts the salesperson to enter quantity of each
item the customer wants to purchase and then outputs a bill. The complete interactive I/O
should appear as below:
Please enter the following info:
Number of PCs:
Number of Disk Drives:
Number of Printers:
Number of Software Packages:
Also write the bill details (cost of each Item and total bill) in a text file called
"BillDetail.txt".
Write a function which displays first 100 ints, starting from 1, using for loop?
WWW.vujannat.ning.COM
Connecting VU Students
MIDTERM EXAMINATION
FALL 2006 Marks: 40
CS301 - DATA STRUCTURES (Session - 3 ) Time: 60min
StudentID/LoginID: ______________________________
► True
► False
► True
► False
► True
► False
An unbalanced tree is one whose root has many more left descendents than right descendants.
► True
► False
Question No: 5 ( Marks: 2 ) - Please choose one
► True
► False
Write a function in C++ that will swap the second and third node in a singly linked list (having 5
nodes) by adjusting only the pointers (and not the data). You can use Node class and List class
methods (such as getNext, setNext, next, get) without writing them. You can assume that the Node
class and List class exists, i.e., do not write the code for these classes. The simple declaration of
Node class and List class is as follow,
class Node
{
public:
int get() { return object; };
void set(int object) { this->object = object; };
private:
int object;
Node * nextNode;
};
};
Push the characters ‘c’, ‘d’, ‘m’, ‘a’, ‘b’ into the stack in the given order. Pop two elements from
the stack one at a time. Then push two characters ‘f’, and ‘g’. Now pop all the characters. What is
the result?
Convert the infix expression 2+(9-3 *2) to postfix. Show the trace of the algorithm, i.e., the
stack, the infix expression and postfix expression using the following table pattern.
(a) Starting from the root node A, perform an In-order traversal of the binary tree below and write
the letters in the nodes that will result during the visitations.
(b) Write the nodes if a Pre-order traversal is performed starting with node A.
A
B C
D E
F G
H
WWW.vujannat.ning.com
http://vujannat.ning.com
Largest Online Community of VU Students
Virtual University
Government of Pakistan
Midterm Examination Spring 2003
StudentID/LoginID
Name
PVC Name/Code
Date April 26, 2003
Instructions
Please read the following instructions carefully before attempting any question:
CS301-Data Structure
Question 1: [1 pt]
Here is the start of a C++ class declaration:
class foo
{
public:
void x(foo f);
void y(const foo f);
void z(foo f) const;
...
Which of the three member functions can alter the PRIVATE member variables of the foo object
that activates the function?
A. Only x can alter the private member variables of the object that activates the function.
B. Only y can alter the private member variables of the object that activates the function.
C. Only z can alter the private member variables of the object that activates the function.
D. Two of the functions can alter the private member variables of the object that activates the
function.
E. All of the functions can alter the private member variables of the object that activates the
function.
Question 2: [1 pt]
In C++, when allocating an array of objects, what constructor is used to initialize all of the objects in
the array?
Question 3: [1 pt]
The list abstract data type (ADT) is used to work with ordered or unordered sequence of items such
as numbers or strings. What of the following implementation of list ADT is best to answer questions
such as "What is the item at position n?"
A. Lists implemented with an array.
B. Doubly-linked lists.
C. Singly-linked lists.
D. Doubly-linked or singly-linked lists are equally best
Question 4: [1 pt]
Which of the following stack operations could result in stack underflow?
A. is_empty
B. pop
C. push
D. Two or more of the above answers
Question 5: [1 pt]
One difference between a queue and a stack is:
Question 6: [1 pt]
What is the maximum depth of recursive calls a function may make?
A. 1
B. 2
C. n (where n is the argument)
D. There is no fixed maximum
Question 7: [1 pt]
Consider this function declaration:
void quiz(int i)
{
if (i > 1)
{
quiz(i / 2);
quiz(i / 2);
}
cout << "*";
}
Question 8: [1 pt]
Suppose T is a binary tree with 14 nodes. What is the minimum possible depth of T?
A. 0
B. 3
C. 4
D. 5
Question 9: [6 pts]
The nodes of a binary tree have data 1, 2, 3, 4. The in-order traversal of the tree yields 2,1,4,3. The
postorder traversal is 2, 4, 3, 1. The root of the tree is at level 0.
Q4: Which value is in a node at level 2 and is the left child of a node at level 1? (1.5 Pt)
Q5: Which value is in a node at level 2 and is the right child of a node at level 1? (1.5 Pt)
class intList
{
public:
// ...
// Remove any duplicate elements from the sorted list
void removeDuplicates();
private:
Node* head; // points to the first Node in the list
// ...
};
Give an implementation of the function removeDuplicates which removes any duplicate elements
from the sorted list, so, for example {1, 2, 3, 3, 4, 4} would be reduced to {1, 2, 3, 4}.
(A+B)−C/D
symbol postfix string operator stack
WWW.vujannat.ning.com
http://vujannat.ning.com
Largest Online Community of VU Students
Virtual University
Government of Pakistan
Midterm Examination Spring 2003
StudentID/LoginID
Name
PVC Name/Code
Date April 26, 2003
Instructions
Please read the following instructions carefully before attempting any
question:
CS301-Data Structure
Question 1: [1 pt]
In C++, when should you use a const reference parameter?
Question 2: [1 pt]
In C++, in which location in memory do dynamic variables (allocated with new) reside?
Question 3: [1 pt]
“Entries in a stack are Ordered". What is the meaning of this statement?
Question 4: [1 pt]
Which of the following applications may use a stack?
Question 5: [1 pt]
If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then removed one at a time,
in what order will they be removed?
A. ABCD
B. ABDC
C. DCAB
D. DCBA
Question 6: [1 pt]
When the compiler compiles your program, how is a recursive call treated differently than a non-
recursive function call?
Question 7: [1 pt]
When a function call is executed, which information is not saved in the call stack?
Question 8: [1 pt]
Select the one FALSE statement about binary trees:
Question 9: [5 pts]
Consider the following binary search tree.
5
/
2
/ \
1 4
/
3
The value 5 is removed from the tree. The following questions refer to BST that remains after 5 has
been removed. The root is at level 0.
544 496
540 8
536 540
532 2
528 508
524 7
520
516
512 0
508 6
A+(B−C)/D
Postfix
Symbol string Operator stack
WWW.vujannat.ning.com
http://vujannat.ning.com
Largest Online Community of VU Students
H
MIDTERM EXAMINATION
Total Marks:75
SEMESTER SPRING 2004
CS301-DATA STRUCTURE Duration: 60min
Instructions
Please read the following instructions carefully before attempting any question:
Answer:
6.2+5/3
6.2+1 (As 5 & 3 are integers so result will be in integer, fraction portion will be discard)
7.2
Question No: 2 Marks: 5
Answer:
Input op1 op2 value stack
8 8
6 6
8
4 4
6
8
+ 6 4 10 10
8
3 3
10
8
* 10 3 30 30
8
+ 8 30 38 38
5 5
38
+ 38 5 43 43
6+ (5 - 3 ) * 8
Answer: 6 5 3 - 8 * +
class Node
{
public:
Node(const string& s, Node* ptr)
: info(s), next(ptr)
{}
private:
string info;
Node* next;
};
Write a function that changes every ’t’ that occurs as the first letter of a word to a ’b’. No other letters
should change. For example, ("tin", "tile", "ant", "saint", "tot") should be changed to ("bin", "bile",
"ant", "saint", "bot")
Answer:
b) Add nodes containing “black” and “panda” so that the tree remains a search tree. Add “black”
first. Draw the nodes attached to the tree diagram above.
c) Draw a search tree in which “teddy” is at the root of the tree, and the root’s left child is “polar”
(include all other nodes from the tree in the diagram above, these other nodes can occur in any
order in the tree you draw.)
Answer
Draw a box and pointer diagram (i.e., a linked list) of what list points to after executing the following code
fragment:
string* name;
Stack<string*> s;
Node* list = NULL;
s.push(new string("Naveed"));
s.push(new string("Mahmood"));
s.push(new string("Mohsin"));
s.push(new string("Aijaz"));
s.push(new string("Pervaiz"));
while (!s.empty())
{
name = s.pop(name);
list = new Node(name, list);
}
Answer:
Answer:
4
( ::: )
[ ::: ]
{::: }
In a properly formed program, these characters will be properly nested and matched. To determine
whether this condition holds for a particular program, you can ignore all the other characters and look
simply at the pattern formed by the parentheses, brackets, and braces. In a legal configuration, all the
operators match up correctly, as shown in the following example:
{ x = ( s = v[ 1 ] + 2 ); y = 4 * ( v [ v.size() - 1 ] + x ); }
bool isBalanced(string s)
that takes a string s with all characters except the bracketing operators removed. For example, for the
program statements
{ x = ( s = v [ 1 ] + 2 ); y = 4 * ( v [ v.size() - 1 ] + x ); }
The method should return true if the bracketing operators in s are balanced, which means they are
correctly nested and aligned, otherwise it should return false. You must either use a Stack or recursion in
your solution. Assume you have the following helper functions.
boolean isBalanced(String s)
{
Answer:
bool is Balanced(string s)
{
char ch, stch;
int i =0;
while(s[i]) // untill the end of the string
{
ch = s[i]; // pick the character
if( boolisOpener(ch); // if it is opener then push on the stack
st.push(ch);
else if( boolisCloser(ch) // if it is closer then pop the last char. from stack
{ stch = st.pop();
MIDTERM EXAMINATION
Total Marks:75
SEMESTER SPRING 2004
CS301-DATA STRUCTURE Duration: 60min
Instructions
Please read the following instructions carefully before attempting any question:
Answer : 7
Write function circularcount that returns the number of nodes in a circularly-linked list like the one
illustrated below.
Answer
and listB as
Answer:
}
if (templistA==NULL)
{
While(templistB!=NULL)
{
Temp=new Node(templistB->info,temp);
templistB=templistB->next;
}
return temp;
}
else if (listB==NULL)
{
While(templistA!=NULL)
{
Temp=new Node(templistA->info,temp);
templistA=templistA->next;
}
return temp;
}
}
Answer:
Input op1 op2 value stack
7 7
5 5
7
3 3
5
7
+ 5 3 8 8
7
* 7 8 56 56
5 5
56
2 2
5
56
- 5 2 3
56
8 8
3
56
9 9
8
3
56
* 8 9 72 72
3
56
+ 3 72 75 75
56
+ 56 75 131 131
Answer: 13 4 10 3 - * +
Suppose that a Stack class is implemented and will be used to implement a Queue class. The only data
members in the private section of the Queue class are an integer representing the number of elements in
the queue and a stack for storing queue elements.
private:
Stack<Etype> myStack;
int mySize;
The member function for Enqueue is shown below (recall that both stack and queue classes are
tem- plated).
Answer:
Answer:
a) black, garbanzo,pinto
b) black, garbanzo, chickpea, pinto, navy, lima
c)
lima
/ \
chickpea navy
/ \ \
black garbanzo pinto
\ \ \
butter kidney soy
/
red
**WARNING: Please note that Virtual University takes serious note of unfair means.
Anyone found involved in cheating will get an `F` grade in this course.
Four statements about lists and stacks are given below. Three of them are correct.
Which one is incorrect?
Root Node
Leaf node
All
None of these
Four statements about trees are given below. Three of them are correct. Which one is
INCORRECT?
What is the output of the following tester program for MyQueue and MyStack?
(10)
MyQueue q;
MyStack s;
q = new MyQueue();
s = new MyStack();
s.push(5);
s.push(6);
s.push(7);
Cout<<s.pop());
q.enqueue(s.pop());
q.enqueue(5);
q.enqueue(6);
cout<<q.dequeue();
s.push(q.dequeue());
cout<<s.pop();
cout<<s.pop();
Answer :
Draw a Binary Search Tree (BST) for following Integers given: 37, 30, 15, 45, 32,
91, 40,18 (10)
Convert the infix expression ((A+b)*D ^ (E- F) ) to postfix. Show the trace of the
algorithm. (10)
Answer:
Final Postfix:
Here is a code of link list which saves the sentence in the link list such that each node
consist of part of sentence (string) e.g. “I am taking examination” then there will be four
nodes and 1st node will consist of the string “I” and second node will consist of string “am”
third will consist of “taking” ans so on.
//---------------------------------------------------------------------------------
struct Node
{
string store;
Node * next;
Node (const string& word, Node * nptr)
: store(word),
next(nptr) { }
};
//---------------------------------------------------------------------------------
class LinkedList
{
public:
LinkedList();
~LinkedList();
int size() const;
void add(Node *) ;
void printList() ;
void removeNode(string );
void replace(string, string );
private:
Node * myHead; // header node
Node * myTail; // last node in list, header if list is empty
int mySize; // # nodes in linked list
};
//---------------------------------------------------------------------------------
You are required to write code for the method replace(string, string). This method
will take two arguments and both will be of type string.
First argument will be the string which will be searched through the link list to
replace and second argument will be the string which it will be replace in the link list.
Incase if method failed to find the string it will display the message “String not
found”
Here is an example
Suppose the link list consists of sentence “I have taken the examination tomorrow”
We need to replace the string “tomorrow” with the string “yesterday” so we call the
method like this
replace(“tomorrow”, “yesterday”);
Redundant.
Recursive.
Self-referential.
Bidirectional.
WWW.vujannat.ning.com
http://vujannat.ning.com
Largest Online Community of VU Students
MIDTERM EXAMINATION
SPRING 2007 Marks: 50
CS301 - DATA STRUCTURES (Session - 1 ) Time: 90min
Q1. (10)
Describe the tree in data structure also describe Binary tree with example?
Q2.(5)
Consider the following infix expression.
3*2^5-1
Converts the above expression into postfix and show the step by step process of conversion.
Q3.(2)
Queue operations are very similar to stack operations. Which of the following definitions is not suitable
for both applications?
3) Delete( )- remove the most recently inserted item from the application.
4) Clear( ) – clear the data structure.
Q4.(2)
The following are statements related to queues.
(i) The last item to be added to a queue is the first item to be removed
(ii) A queue is a structure in which both ends are not used
(iii) The last element hasn’t to wait until all elements preceding it on the queue are removed
(iv)A queue is said to be a last-in-first-out list or LIFO data structure
Which of the above is/are related to normal queues?
i) None of above
ii) 2 and 4 only
iii) 1,2 and 4only
iv) 3 and 2 only.
Q5.(5)
Consider the following infix expression:
(( A+B) * C – (D-E)) ^(F + G)
Converts the above expression into prefix and show the step by step process of conversion.
Q6. (2) Commomly simulation models are
1) a&b
2) (c)Timeless simulation
3) (b) Event based simulation
4) (a)time –bases simulation
Q7.(2) Which of the following is a stack application in the real world?
i) Printing jobs from the spooler directory
ii) Walk in criteria, when one gets into the food line and picks up a tray
iii) Walk in criteria, when one gets into the food line
iv) Creating a directory structure in Dos
Q8. (2)
Consider the following tree.
A
B C D E
F G H I J
L M K
Q9. (10)
Describes the list implementation with following methods?
Add method
Find method
Remove method
Q10. 6)
Consider the linked list given bellow.
2 10 12 13
31 31 29 25 25 20 20
If the above series of operation is performed, what is final position of top and contents of the top
respectively?
Q12. (2)
Consider the following (iv) statements.
Which one of the following is correct in respect of the above statements regarding the Binary trees?
1) ii, iii and iv only
2) ii and iii only
3) I, ii and iii only
4) Iii only
WWW.vujannat.ning.com
http://vujannat.ning.com
Largest Online Community of VU Students
MIDTERM EXAMINATION
SPRING 2007 Marks: 50
CS301 - DATA STRUCTURES (Session - 4 ) Time: 90min
StudentID/LoginID: ______________________________
Please read the following instructions carefully before attempting any question:
► highest to highest
► highest to lowest
► lowest to lowest
► Lowest to highest
► right node
► left node
► both nodes
► If three nodes lie in a straight line, a double rotation is needed to restore the balance.
► If three nodes lie in a straight line, a single rotation is needed to restore the balance.
► If three nodes lie in a dog-leg pattern (that is, there is a bend in the path), you need
to perform a single rotation to restore the balance.
► If three nodes lie in a dog-leg pattern (that is, there is a bend in the path), you need
to perform a single rotation twice to restore the balance.
► A tree consists of left and right sub-trees and value of the right child node is higher with
respect
the root and less than left child.
► A tree consists of left and right sub-trees and value of the right child node is smaller with
respect the root and higher than left child.
A tree with such a property that items in the left sub-tree are smaller than the root and
items in the right sub-tree are larger than the root is called a
► AVL tree
► BST
► Graph
► Circular queue
Question No: 7 ( Marks: 2 ) - Please choose one
If one fixed the violation property of the above AVL tree, what would be the resulting tree?
► a
► b
► c
► d
Consider the following expression tree representation. Give the step by step inorder traversing
Convert following infix expression to postfix and show the conversion process.
A ↑ B * C – D + E/F
Question No: 10 ( Marks: 5 )
What is cost of a search & describe it with example in tree data structure?
WWW.vujannat.ning.com
http://vujannat.ning.com
Largest Online Community of VU Students
Virtual University
Government of Pakistan
Summer Session Examination 2003
StudentID/LoginID
Name
PVC Name/Code
Date August 16, 2003
Instructions
CS301-Data Structure
1. Can two classes contain member functions with the same name? [2
pts]
A. No.
B. Yes, but only if the two classes have the same name.
C. Yes, but only if the main program does not declare both kinds.
D. Yes, this is always allowed.
2. What is the best C++ statement to use when a program must choose between
several alternatives that are controlled by the value of a single variable?
[2 pts]
A. do-while statement
B. for statement
C. if-else statement
D. switch statement
E. while statement
int *p;
int i;
int k;
i = 42;
k = i;
p = &i;
After these statements, which of the following statements will change the value of i
to 75?
A. k = 75;
B. *k = 75;
C. p = 75;
D. *p = 75;
E. Two or more of the answers will change i to 75.
5. Here is an infix expression: 4+3*(6*3-12). Suppose that we are using the usual
stack algorithm to convert the expression from infix to postfix notation.
What is the maximum number of symbols that will appear on the stack AT ONE
TIME during the conversion of this expression?
[5 pts]
A. 1
B. 2
C. 3
D. 4
E. 5
6. I have implemented the queue with a circular array, keeping track of first,
last, and count (the number of items in the array). Suppose first is zero, and
last is CAPACITY-1. What can you tell me about count?
[5 pts]
void test_b(int n)
{
if (n>0)
test_b(n-2);
cout << n << " ";
}
A. 0 2 4
B. 0 2
C. 2 4
D. 4 2
E. 4 2 0
class Node {
public:
int data;
Node* next;
Node(int d, Node *n)
{
data = d; next = n;
}
void setData( int data ) { this->data = data;}
int getData() { return data; }
void setNext( Node* ptr ){ this->next = ptr; }
Node* getNext() { return next; }
};
class intList
{
public:
//...
// return the product of the data values in nodes
of the // list.
int product();
// return the number of times the given "value"
occurs as // data in the
int occurs( int value );
// ....
private:
Node* head; // points to the first Node in the
list
Node* current;// a cursor pointer
};
A. Write the code for the member function product which returns the product of
the data values stored in the nodes of the list.
B. Provide the code for the member function occurs which counts the number of
times the given value occurs in the list. For example, if the 6 nodes in the list contain
the number 42, occurs will return 6.
9. I am going to execute this code with THREE pushes and ONE pop: [5 pts]
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
cout << s.pop( );
Suppose that s is represented by a linked list. Draw the state of the private member
variables of s after the above code:
_______
head_ptr | |
|_______|
10. Write a recursive function to meet the following specification.
[10 pts]
Virtual University
Government of Pakistan
Summer Session Examination 2003
StudentID/LoginID
Name
PVC Name/Code
Date August 16, 2003
Instructions
Please read the following instructions carefully before attempting any question:
1. The duration of this examination is 60 Mins.
2. This examination is closed book, closed notes, closed neighbors; any one found
cheating will get no grade.
3. Unless stated otherwise, all questions carry a single mark.
4. Do not ask any questions about the contents of this examination from anyone.
a. If you think that there is something wrong with any of the questions, attempt it
to the best of your understanding.
b. If you believe that some essential piece of information is missing, make an
appropriate assumption and use it to solve the problem.
5. Most, but not all, of the examination consists of multiple-choice questions. Choose only
one choice as your answer.
a. If you believe that two (or more) of the choices are the correct ones for a
particular question, choose the best one.
b. On the other hand, if you believe that all of the choices provided for a particular
question are the wrong ones, select the one that appears to you as being the
least wrong.
7. You are allowed to use any development environment like Dev C++ etc.
CS301-Data Structure
1. Is it possible for a member function of a class to activate another member function of the same
class? [2 pts]
A. No.
B. Yes, but only public member functions.
C. Yes, but only private member functions.
D. Yes, both public and private member functions can be activated within another member
function.
class quiz
{
public:
quiz( );
int f( );
int g( ) const;
private:
double score;
};
Which functions can carry out an assignment score=1.0; to the private ember variable score?
A. serc
B. carpets
C. steprac
D. ccaarrppeettss
5. Suppose we have a circular array implementation of the queue class, with ten items in the queue
stored at data[2] through data[11]. The CAPACITY is 42, i.e., the array has been declared to be
of size 42. Where does the push member function place the new entry in the array? [2 pts]
A. data[1]
B. data[2]
C. data[11]
D. data[12]
6. I have implemented the queue with a circular array. If data is a circular array of CAPACITY
elements, and last is an index into that array, what is the formula for the index after last?
[2 pts]
A. (last % 1) + CAPACITY
B. last % (1 + CAPACITY)
C. (last + 1) % CAPACITY
D. last + (1 % CAPACITY)
void test_a(int n)
{
cout << n << " ";
if (n>0)
test_a(n-2);
}
What is printed by the call test_a(4)?
A. 0 2 4
B. 0 2
C. 2 4
D. 4 2
E. 4 2 0
8. A partial declaration for a singly-linked integer list class is as follows: [10 pts]
class Node {
public:
int data;
Node* next;
Node(int d, Node *n)
{
data = d; next = n;
}
void setData( int data ) { this->data = data;}
int getData() { return data; }
void setNext( Node* ptr ){ this->next = ptr; }
Node* getNext() { return next; }
};
class intList
{
public:
// ...
// Insert a new node at the tail of the list with
//value ’data’ stored new node.
void tailInsert( int data );
// return the sum of the data values in nodes of the
//list.
int sum();
// ....
private:
Node* head; // points to the first Node in the list
Node* current;// a cursor pointer
};
A. Write the code for the member function tailInsert which inserts a new node at the end (tail)
of the list. The new node stores the integer ’data’. Your code must take care of the case when the
list is empty.
B. Write the code for the member function sum which returns the sum of the data values stored in
the nodes of the list.
9. Consider the usual algorithm to convert an infix expression to a postfix expression. Suppose that
you have read 10 input characters during a conversion and that the stack now contains these
symbols: [10 pts]
| |
| + |
| ( |
bottom |_*_|
Now, suppose that you read and process the 11th symbol of the input. Draw the stack for the case
where the 11th symbol is:
A. A number:
B. A left parenthesis:
C. A right parenthesis:
D. A minus sign:
E. A division sign:
14
/ \
2 11
/ \ / \
1 3 10 30
/ /
7 40
A. An in-order traversal:
B. A pre-order traversal:
C. A post-order traversal:
http://vujannat.ning.com
BEST SITE TO HELP STUDENTS
MIDTERM EXAMINATION
SUMMER 2007 Marks: 50
CS301 - DATA STRUCTURES (Session - 1 ) Time: 120min
StudentID/LoginID: ______________________________
Please read the following instructions carefully before attempting any question:
1. Mobile Phones are strictly prohibited in the examination center.
2. Dev C++ or any other software is not allowed.
3. The duration of this examination is 90 Mins.
4. This examination is closed book, closed notes, closed neighbors; any one found
cheating will get no grade.
5. Unless stated otherwise, all questions carry a single mark.
6. Do not ask any questions about the contents of this examination from anyone.
a. If you think that there is something wrong with any of the questions, attempt it
to the best of your understanding.
b. If you believe that some essential piece of information is missing, make an
appropriate assumption and use it to solve the problem.
7. Most, but not all, of the examination consists of multiple-choice questions. Choose
only one choice as your answer.
a. If you believe that two (or more) of the choices are the correct ones for a
particular question, choose the best one.
On the other hand, if you believe that all of the choices provided for a particular question are
the wrong ones, select the one that appears to you as being the least wrong.
When we say the order of a tree is M, we mean that every non-leaf node must have M
subtrees.
► True
► False
The time complexity of an ordered list of inserting/deleting a data item to/from the list is
O(length_of_list*length_of_list)
► True
► False
An unbalanced tree is one whose root has many more left descendents than right descendents
► True
► False
Stack and queue data structures are needed to convert the infix notations to post fix
notations.
► True
► False
A sequential search of the elements is faster than the binary search of an ordered set of
elements in an array.
► True
► False
Convert the infix expression (A - B) * C + D to postfix. Show the trace of the algorithm, i.e.,
the stack, the infix expression and postfix expression.
Given two sorted lists, L 1 and L 2 , Write the following routine to compute L 1 ∩ L 2 using only
the basic list operations.
List intersection(List list1, List list2){
// Write the complete code to compute L 1 ∩ L 2 here.
}
Consider a binary search tree (BST) that is initially empty. Draw the tree that will result if
the following numbers are inserted in the order given: 35, 28, 13, 43, 30, 89, 38, 16, 12, 40
(a) Starting from the root node, perform an In-order traversal of the binary tree below and
write the letters in the nodes that will result during the visitations.
(b) Write the nodes if a Pre-order traversal is performed starting with node O.
M H
N S
F I