Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Private Data - Data Structures - Exam, Exams of Data Structures and Algorithms

Main points of this exam paper are: Private Data, Public Interfaces, Alphabetize, Bubble Sort, Alphabetical Order, Postorder, Quicksort

Typology: Exams

2012/2013

Uploaded on 04/07/2013

sethuraman_h34rt
sethuraman_h34rt 🇮🇳

4.3

(8)

159 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures, Sample Test Questions for the
Material after Test 2, with Answers
1. Recall the public interfaces of classes List and ListIterator:
typedef int ListItemType;
class ListIterator{
public:
const ListItemType& operator*(); // dereference the iterator
ListIterator operator++(); // prefix ++
bool operator==(const ListIterator& rhs) const;
bool operator!=(const ListIterator& rhs) const;
friend class List;
/* private members here ... */
};
class List{
public:
List(); List(const List& aList); // constructors
~List(); // destructor
bool isEmpty() const;
bool getLength() const;
ListIterator insert(ListIterator iter, ListItemType newItem);
// inserts item at spot before iter, returns an iterator
// to the newly inserted item
void retrieve(ListIterator iter, ListItemType& dataItem) const;
// retrieves the item pointed to by iter, places it in
// dataItem
ListIterator remove(ListIterator iter);
// removes the item pointed to by iter from the list and
// returns an iterator to the next item
ListIterator begin() const;
// iterator to first item of list
ListIterator end() const;
// iterator value to test whether an iterator has reached
// the end of the list
/* private members here ... */
};
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Private Data - Data Structures - Exam and more Exams Data Structures and Algorithms in PDF only on Docsity!

Data Structures, Sample Test Questions for the

Material after Test 2, with Answers

  1. Recall the public interfaces of classes List and ListIterator:

typedef int ListItemType; class ListIterator{ public: const ListItemType& operator(); // dereference the iterator ListIterator operator++(); // prefix ++ bool operator==(const ListIterator& rhs) const; bool operator!=(const ListIterator& rhs) const; friend class List; / private members here ... / }; class List{ public: List(); List(const List& aList); // constructors ~List(); // destructor bool isEmpty() const; bool getLength() const; ListIterator insert(ListIterator iter, ListItemType newItem); // inserts item at spot before iter, returns an iterator // to the newly inserted item void retrieve(ListIterator iter, ListItemType& dataItem) const; // retrieves the item pointed to by iter, places it in // dataItem ListIterator remove(ListIterator iter); // removes the item pointed to by iter from the list and // returns an iterator to the next item ListIterator begin() const; // iterator to first item of list ListIterator end() const; // iterator value to test whether an iterator has reached // the end of the list / private members here ... */ };

Implement the following problems using iterators. Your code should work in a non-member function of the class List. In other words, you may not access the private data of the class List.

(a) Write a few lines of C++ code which use iterators to print out all the elements of the list aList. (b) Write a few lines of C++ code which use iterators to change all the elements of the list aList by adding 1 to each of them (so an initial list of 3,6,7,9,0 will become 4,7,8,10,1). (c) Write a few lines of C++ code which use iterators to delete the even integers from the list aList (so an initial list of 3,6,7,9,0 will become 3,7,9).

  1. Consider the following list of words:

apple, tree, car, dog, yellow, frog, gun, harp

(a) Alphabetize the above list using an insertion sort. Show your work. (b) Alphabetize the above list using a bubble sort. Show your work. How many complete passes are necessary for the bubble sort to ensure the list is sorted? (c) Alphabetize the above list using a merge sort. Show your work. (d) Consider an initially empty binary search tree (BST). Place each of the above words into the BST in the order given above. (Use alphabetical order to make your comparisons.) Draw the completed binary search tree.

  1. Consider the following binary tree:

G /
D T / /
B M X / \ / \
A C I K Y
P

(a) What is the result of a postorder traversal of the above tree? (b) What is the result of an inorder traversal of the above tree? (c) Is the above tree a binary search tree? Why or why not?

  1. Consider the following list of integers:

which prints out all the elements of the tree via a preorder traversal. You should also implement any auxiliary functions needed to make preorderTraverse work.

Answers

  1. Recall the public interfaces of classes List and ListIterator:

typedef int ListItemType; class ListIterator{ public: const ListItemType& operator(); // dereference the iterator ListIterator operator++(); // prefix ++ bool operator==(const ListIterator& rhs) const; bool operator!=(const ListIterator& rhs) const; friend class List; / private members here ... / }; class List{ public: List(); List(const List& aList); // constructors ~List(); // destructor bool isEmpty() const; bool getLength() const; ListIterator insert(ListIterator iter, ListItemType newItem); // inserts item at spot before iter, returns an iterator // to the newly inserted item void retrieve(ListIterator iter, ListItemType& dataItem) const; // retrieves the item pointed to by iter, places it in // dataItem ListIterator remove(ListIterator iter); // removes the item pointed to by iter from the list and // returns an iterator to the next item ListIterator begin() const; // iterator to first item of list ListIterator end() const; // iterator value to test whether an iterator has reached // the end of the list / private members here ... */ };

3rd pass: apple, car, dog, tree, yellow, frog, gun, harp 4th pass: apple, car, dog, tree, yellow, frog, gun, harp 5th pass: apple, car, dog, frog, tree, yellow, gun, harp 6th pass: apple, car, dog, frog, gun, tree, yellow, harp 7th pass: apple, car, dog, frog, gun, harp, tree, yellow

(b) Alphabetize the above list using a bubble sort. Show your work. How many complete passes are necessary for the bubble sort to ensure the list is sorted? Solution: original: apple, tree, car, dog, yellow, frog, gun, harp 1st pass: apple, tree, car, dog, yellow, frog, gun, harp apple, car, tree, dog, yellow, frog, gun, harp apple, car, dog, tree, yellow, frog, gun, harp apple, car, dog, tree, yellow, frog, gun, harp apple, car, dog, tree, frog, yellow, gun, harp apple, car, dog, tree, frog, gun, yellow, harp apple, car, dog, tree, frog, gun, harp, yellow (After 1st full pass, we know ”yellow” is in the correct place.) 2nd pass: apple, car, dog, tree, frog, gun, harp, yellow apple, car, dog, tree, frog, gun, harp, yellow apple, car, dog, tree, frog, gun, harp, yellow apple, car, dog, frog, tree, gun, harp, yellow apple, car, dog, frog, gun, tree, harp, yellow apple, car, dog, frog, gun, harp, tree, yellow (After 2nd full pass, we know ”tree, yellow” in correct place.) The third pass, (from “apple” to “harp” only) will verify that the list is already sorted, and so bubble sort can exit after 3 full passes. (c) Alphabetize the above list using a merge sort. Show your work. apple, tree, car, dog, yellow, frog, gun, harp /
apple, tree, car, dog yellow, frog, gun, harp / \ /
apple, tree car, dog yellow, frog gun, harp / \ / \ / \ /
apple tree car dog yellow frog gun harp \ / \ / \ / \ / apple, tree car, dog frog, yellow gun, harp \ / \ / apple, car, dog, tree frog, gun, harp, yellow \ / apple, car, dog, frog, gun, harp, tree, yellow

(d) Consider an initially empty binary search tree (BST). Place each of the above words into the BST in the order given above. (Use alphabetical order to make your comparisons.) Draw the completed binary search tree. Solution: apple
tree /
car yellow
dog
frog
gun
harp

  1. Consider the following binary tree:

G /
D T / /
B M X / \ / \
A C I K Y
P

(a) What is the result of a postorder traversal of the above tree? Solution: ACBDIPKMYXTG (b) What is the result of an inorder traversal of the above tree? Solution: ABCDGIMKPTXY (c) Is the above tree a binary search tree? Why or why not? Solution: No. The inorder traversal is not in alphabetical order (since the M comes before the K).

  1. Consider the following list of integers:

5, 54, 125, 105, 25, 104, 20, 100, 50, 159

(a) Slightly modify the integers above so that it is appropriate to perform a radix sort on the above list.

  1. Perform a quicksort on the following list of integers. Show your work. Make sure you specify what happens with the pivot at each step.

0, 15, 7, 27, 4, 5

Solution: P means pivot, and the swaps are indicated beneath.

0 15 7 27 4 5 (P)

The first pivot is 0: all the other elements are greater than the pivot, so the list is unchanged in the first pass. But we now know that the pivot 0 is in the correct place: the left subarray is empty, and so is already sorted (base case), now the next step is to sort the right subarray 15 7 27 4 5:

(P)

Here are the swaps for the pivot 15 :

0 15 7 27 4 5 (P) -swap-/ 0 15 7 4 27 5 (P) -swap-/ 0 15 7 4 5 27 (P)

Now swap the pivot 15:

0 15 7 4 5 27 ---swap---/ 0 5 7 4 15 27 (P)

Now the left subarray is 5 7 4 (recall 0 was already done as a previous pivot). The right subarray is 27 , which we know is already sorted since it has only one element. So to sort the left subarray,

0 5 7 4 15 27 (P)

Now we must swap

0 5 7 4 15 27 (P) -swap-/ 0 5 4 7 15 27 (P)

Now swap the pivot.

0 5 4 7 15 27 -swap-/ 0 4 5 7 15 27 (P)

At this point, the left subarray 4 and right subarray 7 both have only one element, so we know they’re sorted. Quicksort is finished.

  1. Recall the private data of classes TreeNode and Tree:

typedef string TreeItemType; class TreeNode{ private: TreeItemType item; TreeNode* leftChildPtr; TreeNode* rightChildPtr; /* function members here... / }; class Tree{ private: TreeNode root; /* function members here...*/ };

(a) Write the pseudocode of a preorder traversal of a binary tree. Solution: preorder(tree node){ if (tree node is not empty){ // not base case visit node preorder (left child of node) preorder (right child of node) } } (b) Write the C++ function definition for a Tree member function void Tree::preorderTraverse() const; which prints out all the elements of the tree via a preorder traversal. You should also implement any auxiliary functions needed to make preorderTraverse work. Solution: void Tree::preorder(TreeNode* treePtr) const{ if (treePtr != NULL){ cout << treePtr->item << endl; preorder(treePtr->leftChildPtr); preorder(treePtr->rightChildPtr);