You are on page 1of 14

p. 1 of 7, ds_xs18f&a.

docx, nassar
1) [6 pts: 2+2+2] Consider a double linked list, with nodes having 3 fields each: a data field and
two pointer fields: next and prev, as shown. There are two pointers A and B pointing to two
nodes, as shown in the diagram. There are many ways to refer to the 3rd node in terms of
pointers A and B. One (B -> next) is written for you. Write 3 more.

B -> next

…………………………………………………………………………………………………………

…………………………………………………………………………………………………………

…………………………………………………………………………………………………………

2) [4 pts] Insert values from the set {6, 22, 9, 14, 13, 1, 8} into the tree below so that it becomes a
BST.

p. 2 of 7, ds_xs18f&a.docx, nassar
3) [10 pts] Sketch the BST which gives the following sequence when traversed post-order:

1, 5, 4, 3, 7, 9, 8, 6, 12, 13, 15, 14, 17, 16, 11

Hint: After you sketch the BST, traverse it post-order and make sure you get the above sequence.

p. 3 of 7, ds_xs18f&a.docx, nassar
4) [10 pts] An array-based circular queue of size 4 is implemented and operates as in the PPT
presentation and code you have received. The initial sketch of the queue is shown below. The
following operations are carried out in sequence: Enque 3, Enque 4, Enque 2, Deque, Enque 5,
Enque 8, Deque, Deque, Enque 6, Enque 7, Enque 9, Deque, Enque 2, Enque 1, Deque. Sketch
the queue after each operation, showing clearly the current contents and pointer (H and T)
positions.

p. 4 of 7, ds_xs18f&a.docx, nassar
5) [10 pts = 5 + 5] Consider these 9 student records.
ID Name GPA
39872 Fawzia 2.9
40370 Maha 2.2
68345 Samir 2.3
68328 Sherif 2.8
62881 Mohamed 3.4
47891 Ali 3.6
45866 Fatima 3.2
49757 Ahmed 3.7
62987 Shady 3.7
Using the hash function h(ID) = ID mod 9, insert the above records in the provided hash table shown.
a) using open addressing collision resolution.
Hash Table
Index ID Name GPA
0
1
2
3
4
5
6
7
8

b) using closed addressing collision resolution. Denote a NULL pointer by “x” and a non-NULL
pointer by “●”, and connect the linked (chained) records by a horizontal arrow →.
Hash Table Overflow area
Index ID Name GPA Ptr
0
1
2
3
4
5
6
7
8

You may use the following space to show your address (index) calculations:

p. 5 of 7, ds_xs18f&a.docx, nassar
6) [10 pts = 3 +7] Consider the following heap.

17

13 11

10 12 9 8

7 1 4 2 3 6
5

a) House the heap in the following 16-element array, starting at index 1.

Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Value ///////

b) Sketch the heap after deleting the root, sketching also all the shapes between the start shape and
the end one.

p. 6 of 7, ds_xs18f&a.docx, nassar
7) [10 pts: 6+2+2] You are given the following infix expression, where conventional precedence
rules apply:
a – b + c * d ↑ ((e + f) / (g – h)) * m / n ↑ k

a. Sketch its expression tree.

b. Write down its prefix equivalent

c. Write down its postfix equivalent

p. 7 of 7, ds_xs18f&a.docx, nassar
Model Answer

p. 8 of 7, ds_xs18f&a.docx, nassar
1) [6 pts: 2+2+2] Consider a double linked list, with nodes having 3 fields each: a data field and
two pointer fields: next and prev, as shown. There are two pointers A and B pointing to two
nodes, as shown in the diagram. There are many ways to refer to the 3rd node in terms of
pointers A and B. One (B -> next) is written for you. Write 3 more.

B -> next

A -> next -> next

B -> prev -> next -> next

A -> next -> prev -> next -> next

2) [4 pts] Insert values from the set {6, 22, 9, 14, 13, 1, 8} into the tree below so that it becomes a
BST.

Method of solution:
• We start by findig a number from the set to be the root of the tree. This number should 4
smaller numbers and 3 larger numbers. Clearly, this number is 13. So, 13 is chosen to be a root
of the tree.
• For each subtree we apply the same idea. For example, to find the root for the left subtree, we
should find from its 4 nodes (i.e. 1, 6, 8, 9) a number that has 1 smaller number and 2 larger
numbers. Clearly, this number is 6. So, 6 is chosen to be a root of the left subtree.

p. 9 of 7, ds_xs18f&a.docx, nassar
3) [10 pts] Sketch the BST which gives the following sequence when traversed post-order:
1, 5, 4, 3, 7, 9, 8, 6, 12, 13, 15, 14, 17, 16, 11
Hint: After you sketch the BST, traverse it post-order and make sure you get the above sequence.

Solution approach:
Post-order means l r R.
• Post-order traversal means the last number above, i.e. 11, is the root of the tree.
• With 11 being the tree root, the numbers just to the left of the 11 and greater than 11 (i.e. 12, 13,
15, 14, 17, 16) are in the right subtree of “11”. Let us call the set of these numbers R.
• Also, with 11 being the tree root, the numbers just to the left of the 12 (i.e. 1, 5, 4, 3, 7, 9, 8, 6)
are in the left subtree of “11”. Let us call the set of these numbers L.
• Let us take the right subtree, i.e. R ={12, 13, 15, 14, 17, 16}. Since the traversal is Post-order,
then the last of them, i.e. 16, is the root of this subtree.
o With 16 the root of the right subtree R, the numbers just to the left of the 16 and greater
than 16 (i.e. 17) are in the right subtree of “16”. Let us call the set of these numbers
RR.
o Also, with 16 the root of the right subtree R, the numbers just to the left of the 17 and
less than 16 (but greater than 11), i.e. 12, 13, 15, 14, are in the left subtree of “16”. Let
us call the set of these numbers RL.
• Keep repeating the above procedure till the tree shown below is built.

11

6 16

3 8 14 17

4 7 9 13 15
1

12
5

To validate the tree, we traverse it post order to obtain:


1, 5, 4, 3, 7, 9, 8, 6, 12, 13, 15, 14, 17, 16, 11
Sure enough, this sequence is identical to the one given in the problem.

p. 10 of 7, ds_xs18f&a.docx, nassar
4) [10 pts] An array-based circular queue of size 4 is implemented and operates as in the PPT
presentation and code you have received. The initial sketch of the queue is shown below. The
following operations are carried out in sequence: Enque 3, Enque 4, Enque 2, Deque, Enque 5,
Enque 8, Deque, Deque, Enque 6, Enque 7, Enque 9, Deque, Enque 2, Enque 1, Deque. Sketch
the queue after each operation, showing clearly the current contents and pointer (H and T)
positions.

Answer is as follows, with the steps given below.

Before showing the steps, here are the rules we use:


• Enqueue at where “T” is pointing, then increment “T” modulo 4 (i.e. rotate to location 0 if you
reached the end of the array.) Except for when the queue is full, “T” always points to a vacant
location.
• Dequeue from where “H” is pointing, then increment modulo 4. Except for when the queue is
empty, “H” always points to a filled location (value.)
• If the queue is full and an “enqueue” operation is attempted, the operation is not accepted.
• If the queue is empty and a “dequeue” operation is attempted, the operation is not accepted.

H T T H T H TH
1 6 4 11 8 6 5 16 8 6 7 2
2 5
Initially After enqueueing 5 After enqueueing 6 Enqueue 1 is not accepted

H T TH TH T H
2 3 7 8 4 2 5 12 8 6 7 5 17 6 7 2
After enqueueing 3 After enqueueing 8 After enqueueing 7 Finally: After dequeueing

H T T H TH
3 3 4 8 8 2 5 13 8 6 7 5
After enqueueing 4 After dequeueing Enqueue 9 is not accepted

H T T H H T
4 3 4 2 9 8 5 14 8 6 7
After enqueueing 2 After dequeueing After dequeueing

H T T H TH
5 4 2 10 8 6 5 15 8 6 7 2
After dequeueing After enqueueing 6 After enqueueing 2

p. 11 of 7, ds_xs18f&a.docx, nassar
5) [10 pts = 5 + 5] Consider these 9 student records.
ID Name GPA
39872 Fawzia 2.9
40370 Maha 2.2
68345 Samir 2.3
68328 Sherif 2.8
62881 Mohamed 3.4
47891 Ali 3.6
45866 Fatima 3.2
49757 Ahmed 3.7
62987 Shady 3.7

Using the hash function h(ID) = ID mod 9, insert the above records in the provided hash table shown.
a) using open addressing collision resolution.
Hash Table
Index ID Name GPA
0 68328 Sherif 2.8
1 62987 Shady 3.7
2 39872 Fawzia 2.9
3 47891 Ali 3.6
4 45866 Fatima 3.2
5 40370 Maha 2.2
6 49757 Ahmed 3.7
7 62881 Mohamed 3.4
8 68345 Samir 2.3

b) using closed addressing collision resolution. Denote a NULL pointer by “x” and a non-NULL
pointer by “●”, and connect the linked (chained) records by a horizontal arrow →.
Hash Table Overflow area
Index ID Name GPA Ptr
0 68328 Sherif 2.8 x
1
2 39872 Fawzia 2.9 ● → 47891 Ali 3.6 ● → 45866 Fatima 3.2 x
3
4
5 40370 Maha 2.2 ● → 49757 Ahmed 3.7 ● → 62987 Shady 3.7 x
6
7 62881 Mohamed 3.4 x
8 68345 Samir 2.3 x

You may use the following space to show your address (index) calculations:

Here we calculate the hash values (table indexes) from the hash fields (the IDs) using the hash function:
39872 mod 9 =2 Fawzia 2.9
40370 mod 9 =5 Maha 2.2
68345 mod 9 =8 Samir 2.3
68328 mod 9 =0 Sherif 2.8
62881 mod 9 =7 Mohamed 3.4
p. 12 of 7, ds_xs18f&a.docx, nassar
47891 mod 9 =2 Ali 3.6
45866 mod 9 =2 Fatima 3.2
49757 mod 9 =5 Ahmed 3.7
62987 mod 9 =5 Shady 3.7
6) [10 pts = 3 +7] Consider the following heap.

17

13 11

10 12 9 8

7 1 4 2 3 6
5

a) House the heap in the following 16-element array, starting at index 1.

Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Value /////// 17 13 11 10 12 9 8 5 7 1 4 2 3 6

b) Sketch the heap after deleting the root, sketching also all the shapes between the start shape and
the end one.

Steps: Noting that this is a max heap (i.e. highest element is in the root) we proceed as follows.
1- Copy the last element (6) into the root, overwriting what is there (17).
2- Swap the root (6) with the larger (13) of the two children of the root.
3- Keep this swapping process down the tree, till you find that the children are smaller than the
element to swap, at which point you stop.

6
17 13

12
13 11 6 11
1 3
10 12 9 8 10 6 9 8
12

5 7 1 4 2 3 6 5 7 1 4 2 3

13 13
6

12 11
6
13 11 4
2 10
Final answer
9 8
6
10 12 9 8
p. 13 of 7, ds_xs18f&a.docx, nassar
5 7 1 4 2 3
5 7 1 4 2
7) [10 pts: 6+2+2] You are given the following infix expression, where conventional precedence
rules apply:
a – b + c * d ↑ ((e + f) / (g – h)) * m / n ↑ k

a. Sketch its expression tree.


As can be seen, there are 10 operations in the expression. The first thing to do is to
number these operations according to the conventional precedence rules (see class notes
or search the Internet) as shown.

Now sketch the expression tree, starting at the bottom of the page with operation no. 1
(namely, the + in e+f), going up the tree as the operation number increases. Make sure
that the two operands of an operation be at the same level in the tree (this needs some
looking ahead). Also make sure to keep the order of the two operands in the tree as it
was in the expression: what is left should remain left and what is right should remain
right. For example, since we have in the expression (e + f), we should have in the tree
“e” on the left and “f” to its right.

+
- /

a b * ↑

* m n k

c ↑

d /

+ -

e f g h

b. Write down its prefix equivalent


The prefix equivalent of the infix expression is obtained by traversing the expression tree
above Pre-Order, i.e. R l r, as follows.

+ - a b / * * c ↑ d / + e f - g h m ↑ n k

c. Write down its postfix equivalent


The postfix equivalent of the infix expression is obtained by traversing the expression tree
above Post-Order, i.e. l r R, as follows.

a b - c d e f + g h - / ↑ * m * n k↑ / +

p. 14 of 7, ds_xs18f&a.docx, nassar

You might also like