Professional Documents
Culture Documents
This Study Resource Was: Icnchfan Kyip
This Study Resource Was: Icnchfan Kyip
This Study Resource Was: Icnchfan Kyip
CS 1 332 - Test 1
m
t5
er as
3 Stacks 15
NR
co
4 Recursion 15 Ir{ h5
eH w
5 Linked List Coding 20 2fr A-
o.
AJ
6 rs e
Binary Search Tree Debugging 20
It Jd
ou urc
7 Extra Credit 10 q VD
o
Total 100 9e #S
aC s
vi re
All code for this Exam must be written in Java to receive any c;edit. Code that is not written in
Java will not be graded and will receive a 0 for the specific problem. To the best of your ability
write code that will compile. DO NOT WRITE lN PEN, USE PENCIL ONLY.
y
ed d
CELL PHONES, CALCULATORS, etc. ARE NOT ALLOWED lN ANY WAY DURING
THE EXAM . NOT EVEN TO PROVIDE THE TIME. TURN THEM OFF AND PUT
ar stu
THEM AWAY. IF YOUR PHONE RINGS, YOU WILL LOSE CREDIT ON THIS EX.AM.
By signing this page, you signify that it is your work and that you have neither given nor
sh is
received inappropriate help during the taking of this exam in compliance with the Academic
Th
This study source was downloaded by 100000776811472 from CourseHero.com on 09-19-2021 12:17:23 GMT -05:00
https://www.coursehero.com/file/12154315/CS-1332-Exam-1/
.+"$::F ae:l -'. .
m
A'avlist
er as
rrom an
*n :.:::':1.::,"*'"nt ruoA
WcrA
co
a doubre, circurarry
ffij[i,'if:T,"rt.rr9m
eH w
I ;:,ff;::1:#asmpeBsr
o.
rs e
ou urc
o
aC s
vi y re
ed d
ar stu
k 1t')
sh is
Th
This study source was downloaded by 100000776811472 from CourseHero.com on 09-19-2021 12:17:23 GMT -05:00
https://www.coursehero.com/file/12154315/CS-1332-Exam-1/
+.Br*h +'='-;. +..-: ,-,+.<-4-
Arrays must be regrown once you try to add an element into a full aray. Complete the method
below.
lf you do not know how to code this you can receive 2 ooints by circling this sentence,
anything you write for this problem witt not Oe gradeJt you circte this ientence.
/**
* Regrows an array by creating a new array of double the size,
* copying the original data int.o the new array, and then returning
* the new array.
* Gparam arr The array to be regrown. You may assume that the size
of the array is greater than 0, and that it is fulr.
*
* @return An array that is doubre arr, s size, and contains
m
er as
all- of the same data in the same indices.
:,
co
private T[] regrow(T[] arr)
eH w
!
' ,r. rsrtnrc =-,1g3,-K*lqy::l+,!^qlltglttt + zl);
-L
o.
rs e
=(tcr) (re* 6iec+ cor(. t ); tenQjh
ou urc
a.rJ
,fon(i.nt r=o, L< om.wnSth r Lt+ ) t
net-.rQrr Ctl = o*r'( Ct] I
o
aC s
3
vi re
Fefurn na"Rrlr-;
y
ed d
fif ,a
ar stu
sh is
Th
This study source was downloaded by 100000776811472 from CourseHero.com on 09-19-2021 12:17:23 GMT -05:00
https://www.coursehero.com/file/12154315/CS-1332-Exam-1/
I
1
3. (15 points) stacks rigst tn,,t9Sf B t
Perform the following operations on a stack of integers and draw the state of the$adrtri
each operation, even if it is empty. For each element removed, indicate which elemerfiras
removed.
push 0l t/
popo y'
push 0) {
push (3) t/
push (Lj') /
popo r/
push (44{
popo /
m
er as
pop o
'/
,e)f nle
co
push ( 10B )
eH w
t3
o.
a$u,B*giHl ,ff.il. rs e qr
ou urc
-3
7
o
aC s
vi re
y
ed d
ar stu
is
Th
sh
This study source was downloaded by 100000776811472 from CourseHero.com on 09-19-2021 12:17:23 GMT -05:00 2" Nr-
,-e?s
https://www.coursehero.com/file/12154315/CS-1332-Exam-1/
4. (15 points) Recursion
a. (5 points) Consider the following recursive algorithm for computing the nth Fibonacci
number. Explain why this is a bad algorithm. Make sure to mention Big-O in your exptanation.
m
f {.}"u{€ i"$ frf'v*f 5 the dgt a v6t ves t
er as
titr,e5 be S"fmre
it jutl
co
{ecgtteu hek$ Sarn
eH w
o.
rs e
ou urc
b. (10 points) You will be completing a method to compute the nth harmonic number. The n-
th harmonic number is 1/1 + 112 + 113 + 114 + ... + 1ln. Using the provided structUre complete
the recursive method to compute this value. H(1) = 1, and you may assume that all values of n
o
passed into this method are greater than or equal to 1. You need to complete the code
for the
base case, and recursive call.
aC s
vi y re
/ / base case:
ir (n r=l)
ed d
ti
ar stu
return
// recursive cal-1: Jg
n
else
is
-l
.\
n5;J). -r _f
sh
-l
! A^
er.*{-q v, }
-*
1.1sr+Wr
f-'' (ffirq) 5
I f
I
_*L*
b.*1w,.-i
iruA:
tYttl
tl
t \ns
{
I
1r"*-* Z \1
\
L
L^ n.-
qrL I
t{r
I ha*"rtroY**j
This study source was downloaded by 100000776811472 from CourseHero.com on 09-19-2021 12:17:23 GMT -05:00
,f t,
https://www.coursehero.com/file/12154315/CS-1332-Exam-1/
5. (20 points) Linked List Goding
m
private Node head;
er as
co
// there is no size variable or methodl donrt use it. (and don,t
eH w
// worry about updating it)
o.
public void addFront (T data)
rs e {
ou urc
irtdotff=-nl.*r$)
feturn i
*
VOde rtet*r1{t36' Y\q)\rr tqoda{'*ryt&\;
o
aC s
etgetHeodl) = : n**-$ i f
vi re
i
SelHmdlne'.: nrocfe), /
y
1
ar stu
etse e
nr-,r.^;NOcl4, , Setwext(gekl{radl} i,:
, NOd.c C{.-ur /
sh is
= d{*F\€odt) /
(curr. qprulev+ I::
Th
"L{^i\e %u+tkmdt_} J
cwrr = crr-rYr qc-+t'rjg k+ ( \ '
3-0j
c*rr. Se+Ne(+LnC\d bJgde \ J
SetHeod [rtew Node n)I '
Find the errors in this code, if they exist, and fix them. Do not changeladd more than S lines of
code. You are searching for looicalerrors, not compilation errors. The BST class declaration
and provided Node class are correct.
)
private Node left, right;
/ / standard getters and setters omitted
/b
CJ{{
CO
,i
m
er as
public void add(T data) {
co
if (data *= nuII) {
eH w
gpt- addHelper (rooL, data)
o.
;
x@ rs e b
ou urc
,l
o
if (current == null) {
return node;
y
L
) else if (data.compareTo(current.getData) S 0) {
ed d
https://www.coursehero.com/file/12154315/CS-1332-Exam-1/
7. $A points) Extra Credit
a. (4 points) Wtrat is the maximum number of nodes there can be on the nth level
of a binary
tree? Remember that the root is on level 0.
a"
b- $ points) lMrat is the maximum number of nodes there can be in a binary tree with n levels?
m
er as
(
eo = f*t;l
co
ao
eH w
.-t <--
o.
rs e
ou urc
c, (2 points) Pick one or more of the TAs and give thqm a theme song.
o
aC s
K3\e
ed d
ar stu
sh is
Th
#e'8@*44@*;
This study source was downloaded by 100000776811472 from CourseHero.com on 09-19-2021 12:17:23 GMT -05:00
https://www.coursehero.com/file/12154315/CS-1332-Exam-1/
Powered by TCPDF (www.tcpdf.org)