CMPSC 465 Data Structures and Algorithms
Assignment 1: Review of Data Structures
Total points: 100 pts
Due: Friday Feb. 5, 2021. 11:59 pm.
Part I. Homework Exercises. (60 pts)
Submission: Submit your solution is a pdf file or image files to CANVAS before the due
time. If you choose to submit image files, make sure the filenames are correctly ordered
(e.g. 1.jpg, 2.jpg ...). If you choose to take a picture of your handwriting, the picture
should be clear enough so that all the information is recognizable.
1. Show the elements of the stack in correct order after the following series of
operations. Assume that the stack is initially empty.
push(12); push(17); pop(); push(2); push(18); push(22); pop(); pop();
12,2
2. Show the elements of the queue in correct order after the following series of
operations. Assume that the queue is initially empty.
enqueue(4); enqueue(5); dequeue(); enqueue(2); dequeue(); enqueue(9);
219
3. Draw the undirected graph from the following adjacency lists representation.
Determine whether it is connected.
a e
b e,g
c d,f is
d c
e a,b,g
f c
g b,e
4.
(1) Draw the undirected weighted graph from the following adjacency matrix
representation.
(2) Determine the minimum weight path from vertex a to f.
a b c d e f
10
a b
a ∞ 10 20 ∞ ∞ 90
20
b 10 ∞ ∞ 20 50 ∞
C
c 20 ∞ ∞ ∞ 30 ∞ 50
d ∞ 20 ∞ ∞ ∞ 70 no
30
90
e
e ∞ 50 30 ∞ ∞ 20
d
f 90 ∞ ∞ 70 20 ∞
20
To
5.
a t aoaa.se t
Ee.iooL0ae of
(1) Consider the following binary search tree with the key values of the nodes shown.
Draw the BST after inserting a node with key 5.
(2) Then, suppose we insert another node with key 11. Draw the tree after the insertion.
l 2
I to Go
6. For the following operations, which data structure performs better, linked list or array?
a) Remove the first element linkedlisatrray
b) Return the value of the 1000th element
c) Searching for a specific element, given that the linked list/array is sorted. (Recall
binary search you have learnt in CMPSC 122.)
array
In the following questions, you will practice using pseudo codes to represent algorithms.
Please refer to the pseudo code standard handout on CANVAS. The standard could be
a good starting guide for writing pseudo code.
7. Write a recursive algorithm to determine sum of elements (assume that the elements
are numbers) of all the nodes of a BST.
You can assume that given node v, the following functions are provided. (You do not
need to implement them, just use them as needed.)
Parent(v): Return the parent node of v.
LeftChild(v), RightChild(v): Return the left/right child of v. Return NULL if does not
exist.
Key(v): Return the key value of v.
Element(v): Return the value of element of v.
To help you work comfortably with the pseudo code, the following pseudo code to count
the total number of nodes of a BST is provided. You can modify the code to compute
the sum of elements of all nodes.
Algorith SumBIcv
u Null
return o
return Cv t sumBTW 7left It sumB.tw
key gw
Algorithm countNodes(v)
// Count the number of nodes in a BST given the root v
// Input: v, which is the root node of the BST
// Output: An integer, which is the number of nodes in the BST
if (v == NULL) return 0
return 1 + countNodes(LeftChild(v)) + countNodes(RightChild(v))
8. Write an algorithm, so that given any positive integer n, the algorithm outputs the
integer part of the positive real number x such that x3+100x = n. Besides assignment
and comparison, your algorithm may only use the four basic arithmetical operations,
namely addition/subtraction/multiplication/division. 2
3 4100 X't0 tooX 5oooo i 0
For example, when n = 50000, the equation x3+100x = 50000 has a positive real
number solution x=35.9357.... In this case the algorithm should output 35.
9. An array is called “almost increasing” if it is in increasing order, or after swapping a
pair of elements, the array becomes in increasing order.
For example, the array [1,3,50,6,8,20,5] is almost increasing, since after swapping 50
and 5, the array becomes in increasing order.
Write an algorithm, given an array A[0...n-1], determine whether the array is almost
increasing. If yes, output “Yes”, otherwise output “No”.
Part II. Programming Project. (40 pts)
Details to be given in another file.
y's a i i it b i to it D
double
j
it Uy't odRyu 0111 yl ORR Y22 0
i i
in
Tint count o
bubble sort forcinti O ion tiitt
with comparer
forcintj oijcn i tjj.lt
4input array
arraylength
Titcarruissarreitly
original array
tinttemp arr
no outpatit's
arrcjtb.it
awcjg
avid function arrejtigatewp
show answer in
text
ion I Ii et
for linti O
if carraj Edmciis
count et's
it count 2
counts yes Kendli
else
count a no deadly