HW1 465 Part1
HW1 465 Part1
HW1 465 Part1
Assignment 1: Review of Data Structures
Total points: 100 pts
Due: Friday Feb. 5, 2021. 11:59 pm.
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.
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.
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?
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.)
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.
Write an algorithm, given an array A[0...n-1], determine whether the array is almost
increasing. If yes, output “Yes”, otherwise output “No”.
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
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
else
count a no deadly