You are on page 1of 8

--

CS 1 332 - Test 1

Name: Jtl\r O LOf tntn n


r-square iCnChfAn 3 I
Login:
rAs: KYIP ffrA Nnnrnt
Section: A1 GR IDK

Problem Possible Earned Grader


1 Big O 10
IO \v
2 Generic Arrays 2A
lr z7a

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

Honor Code of Georgia Tech.

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 -'. .

1. flA points) Big O

ffi'J:::::ixffi ':j"ffi ,*flffi:ffi ,"il",,


",,*Hix*""#il*fi :_H:ffi
e- Get the maximum
elementrurvlrr
from a simple
srmpte BST
BST
pop from
a staek ; r-, A) o(1)
-A
-f.
I
;;;;:;l:::",:lh,:,.,.
Kno+re._
tltvu3
,
c)
Removing and etement
s
o(.sn,
o(n)
from an AVL tree
C .
'l--evel
order traversal of a
BsT
D) o(nlogn)
Adding to the front
of a srn',i,,nn"oa,r,
E) o(nz;
-4L strrnELrLISr
3- Enqueuino o,, a or,.,,^ :^--: F) oe\
I aiii"iiii,.'tsffi;f,H1?,LXjed bv a doubry

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-

2.120 points) Generic Arrays

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.

public int fib (int n) t


if (n =: Q) return 0;
el-se if (n :: .1) return 1;
el-se return fib(n-1) + fib (n-2);

-Th,e rnethod wYttten {rb0 v€ hs$ an 0'[2h ] ruY}niYt ?


-lt haS Such c*-'b&d " trtCl -O hrCmulte #te
flfng.
rnelhDd d*e1 $re "S#"*ns. t&"1 {.t-tl ntt ost r nd"l qt\y

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

public 9ouble harmonic (int n) {

/ / base case:
ir (n r=l)
ed d

ti
ar stu

return
// recursive cal-1: Jg
n
else
is

return 1 /hnrrntrmrr, [n-t] ) *


@;
Th

-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

Complete the addFront method for this circular, singly


tinked list. Remember that in a circular
linked list the last element points to the-hEdth;fi;;";.
only 1 etement it points to itsetf. For
this method . Use only what has been provided.
lf you do not know how to code this
vi.-u ca1 receive 2 points by circling this sentence,
anything you write for this probtem witl not be graded you
if circle this sentence.
public class Circularlinkedlist<T> {
private class Node { Lt)(
private T data;
private Node next;
D+
public Node (T data) { this.data = data; }
/ / Stanaard getters and setters omitted

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

t€t k\e qdL) , Se+ Sle{ +^ t rts*) zuOCte };


ed d

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 '

O.hefn 4tr Vs lolocla '.


e tSC I '
Nodel n 5 net'P Node C heod 'do+o) ;
1
n.nex* = heqd .oex* )
hreod.hc\(f = n) NL
,%
This study source was downloaded by 100000776811472 from CourseHero.com on 09-19-2021 12:17:23 GMT -05:00
hecrcl. cLo.'ho- a d.a}ro-'t
https://www.coursehero.com/file/12154315/CS-1332-Exam-1/
6. (20 points) Debugging

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.

public class BST<T extends Comparable(T>) {


private class Node { \r
private T data;

)
private Node left, right;
/ / standard getters and setters omitted
/b
CJ{{

CO

// Start Debugging Here:

,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

private &ce addHelper (Node current, T data) {


aC s

Node node = new Node (data);


vi re

if (current == null) {
return node;
y

L
) else if (data.compareTo(current.getData) S 0) {
ed d

current. setleft (addHelper (current.getleft O , data) ) ;


ar stu

returnfnodeD rgtufn Cuf"ren* ;


) else {
sh is

current. se Right (addHelper (current.getRight O , data) ) ;


Th

retur I.€!,nrrn eUrrEnilrj


t.(^rA4
-1 rctd.n
ul*
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/
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

- t' I tront ir *hct wax'*


vi y re

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)

You might also like