Sunteți pe pagina 1din 6

C Sc 227 Practice Final Section Leader ________ Name ___________________________ 125 pts

0. Upon which day is o r !inal e"am# __$ed_____ %&pts' (his tension red cer will )e easier to answer on the !inal 1. Use the !ollowin* )inary tree to write o t each o! the three tra+ersals indicated )elow. %15pts'

Preorder tra+ersal _____1__2__&__,__5__-__________________________ .norder tra+ersal _____2__&__1__5__,__-__________________________ Postorder tra+ersal _____&__2__5__-__,__1__________________________ 2. .s this a /inary Search (ree# %yes or no'# __N0___ %,pts'

,. .s this a /inary Search (ree# %yes or no'# _N0__ %,pts'

&. $hat potential )ene!it is there to sin* a )inary search tree to store a set o! elements rather than a sorted array to store the same collection o! elements# %,pts' /etter r ntime !or insert and remo+e1 0%lo* n' rather than 0%n' 5. $hat is re2 ired o! o! the +al e3s type to )e stored in a )inary search tree# %,pts' (he type m st implement Compara)le or some inter!ace that e"tends the Compara)le inter!ace so it has compare(o -. 4raw a pict re o! the tree that res lts !rom the !ollowin* code on yo r OrderedSet class. /e s re to ha+e the instance +aria)le root re!erence the root node sin* an arrow. %5pts'
OrderedSet<String> set = new OrderedSet<String>(); root 66667 .nception set.insert("Inception"); 8 9 set.insert("Shaft"); /ean Sha!t set.insert("Bean"); 8 9 set.insert("Scream"); set.insert("Titanic"); Scream (itanic

7. Complete method maxKey in OrderedSet that ret rns a re!erence to the :ey with the ma"im m +al e. $rite two di!!erent implementations o! a pri+ate helper method. 0ne method m st ha+e a rec rsi+e sol tion. (he other method will ha+e an iterati+e sol tion. (hese assertions sho ld pass1
@Test p !"ic #oid test$a%&e'() ( OrderedSet<Integer> intTree = new OrderedSet<Integer>(); assert) ""(intTree.ma%&e'()); intTree.insert(*); assert+, a"s(new Integer(*)- intTree.ma%&e'()); intTree.insert(.); assert+, a"s(new Integer(.)- intTree.ma%&e'()); intTree.insert(/0); assert+, a"s(new Integer(.)- intTree.ma%&e'()); 1 22222222222222222222222222222222222222222222222222222222222 p !"ic c"ass OrderedSet<+ e%tends 3ompara!"e<+>> ( pri#ate c"ass Tree)ode ( pri#ate Tree)ode "eft; pri#ate Tree)ode right; pri#ate + data; p !"ic Tree)ode(+ e"ement) ( data = e"ement; "eft = n ""; right = n ""; 1 1 22 end c"ass Tree)ode pri#ate Tree)ode root; 22 3reate an empt' tree p !"ic OrderedSet() ( root = n ""; 1

a' $rite the p !"ic and pri#ate methods sin* a rec rsi+e sol tion. p !"ic + ma%&e'() ( if (root == n "") ret rn n ""; e"se ret rn ma%&e'(root); 1 pri#ate & ma%&e'(Tree)ode t) ( if (t.right == n "") ret rn t.4e'; e"se ret rn ma%&e'(t.right); 1

%5pts'

)' $rite the complete p !"ic method with an iterati+e sol tion. %5pts' p !"ic + ma%&e'() ( if (root == n "") ret rn n ""; e"se ( Tree)ode t = root whi"e(t.right 5= n "") t = t.right; ret rn t.data; 1 1 5. Complete the o tp t that wo ld )e *enerated )y this code. %5pts'
S'stem.out.print("ist.get(6i) 7 " "); 22 6 sho "d ha#e !een i. not !eing tric4'

/* /* 89 90 === tr e fa"se /* 90 ;. Complete combine so it ret rns a :ist that only has any and all elements that are contained in )oth List<.nte*er7 ar* ments. (he !ollowin* assertions m st pass. %12pts'
:ist<Integer> ts8 = new ;rra':ist<Integer>(); ts8.add(9); ts8.add(<); ts8.add(9); ts8.add(<); :ist<Integer> ts9 = new :in4ed:ist<Integer>(); ts9.add(9); ts9.add(9); ts9.add(<); ts9.add(=); ts9.add(<); 22 com!ine(>9- <- 9- <?- >9- 9- <- =- <?) sho "d ret rn >9- <? or ><- 9? :ist<Integer> res "t = combine(ts8- ts9); assertEquals(9- res "t.si@e()); assertTrue(res "t.contains(9)); assertTrue(res "t.contains(<)); p !"ic :ist<Integer> combine (:ist<Integer> s8- :ist<Integer> s9) (

:ist<Integer> res "t = new ;rra':ist<Integer>(); for (int i = 6; i < s8.si@e(); i77) ( Integer c rrent = s8.get(i); if (s9.contains(c rrent) AA 5res "t.contains(c rrent)) res "t.add(c rrent); 1 return res "t; 1
10. (o o r OrderedSet class shown earlier= add method valuesGreaterThan(+ e"ement) to ret rn a :ist<+>

o! all elements in the OrderedSet o)>ect that are *reater than e"ement. %12pts'
OrderedSet<Integer> om = new OrderedSet<Integer>(); om.insert(*6); om.insert(=6); om.insert(<*); om.insert(**); :ist<Integer> "ist = om.valuesGreaterThan(*6); assert+, a"s(9- "ist.si@e()); assertTr e("ist.contains(**)); assertTr e("ist.contains(=6)); assertFalse("ist.contains(<*)); assertBa"se("ist.contains(*6)); p !"ic :ist<+> valuesGreaterThan(+ e"ement) (

:ist<+> "ist = new ;rra':ist<+>(); gather;""CreaterThan(e"ement- root- "ist); ret rn "ist;

pri#ate #oid gather;""CreaterThan(+ e"ement- Tree)ode t- :ist<+> "ist) ( if (t 5= n "") ( if (t.data.compareTo(e"ement) > 6) "ist.add(t.data); gather;""CreaterThan(e"ement- t."eft- "ist); gather;""CreaterThan(e"ement- t.right- "ist); 1 1 11. (o o r OrderedSet class= add method isFull() that ret rns tr e i! all nodes are lea+es or ha+e e"actly two children. .! any node has precisely one child= ret rn !alse. ?n empty tree is ! ll. %12pts'
p !"ic !oo"ean isFull() (

p !"ic !oo"ean isB ""() ( ret rn isB ""(root); 1 pri#ate !oo"ean isB ""(Tree)ode t) ( if (t == n "") ret rn tr e; e"se if (t.right == n "" AA t."eft 5= n "") ret rn fa"se; e"se if (t."eft == n "" AA t.right 5= n "") ret rn fa"se; e"se ret rn isB ""(t."eft) AA isB ""(t.right); 1 12. (o o r OrderedSet class= add method reverse() that chan*es the orderin* property o! s ch that an in order tra+ersal wo ld +isit all elements in descendin* order. @mpty tree and trees with only one node will not chan*e. (rees with two or more nodes will chan*e. %12pts'
root D $ root D $

!ecomes

&

2 C root D EE 2 D 89 *** root D $ 2 D C S 2 D F G

D C root D EE 2 D *** 89 root D $ 2 D S C 2 D G F

!ecomes

!ecomes

p !"ic #oid reverse() (

p !"ic #oid re#erse() ( mirror(root); 1 pri#ate #oid mirror(Tree)ode t) ( if (t 5= n "") ( Tree)ode temp = t.right; t.right = t."eft; t."eft = temp; mirror(t."eft); mirror(t.right); 1 1

1,. Complete the o tp t that wo ld )e *enerated )y this code. %;pts'


Hash$ap<Integer- String> S'stem.out.print"n(map.p S'stem.out.print"n(map.p S'stem.out.print"n(map.p S'stem.out.print"n(map.p map = t(*6t(9*t(*6t(I*new Hash$ap<Integer- String>(); "$")); "B")); "3")); "J"));

S'stem.out.print"n(map.si@e());

S'stem.out.print"n(map.get(9*)); S'stem.out.print"n(map.get(*6)); S'stem.out.print"n(map.get(I*)); S'stem.out.print"n(map.get(EE));

n n $ n 0 B 3 J n

"" "" ""

""

1&. $rite the o tp t *enerated )y the !ollowin* pro*ram when the inp t !ile Ashort.t"tA has the one line. %- pts'

Spain rain in Spain in Spain


p !"ic c"ass ; ( p !"ic static #oid main(String>? args) ( Scanner scanner = n ""; tr' ( scanner = new Scanner(new Bi"e("short.t%t")); 1 catch (Bi"e)otBo nd+%ception e) ( 1 Tree$ap<String- Integer> m'ster' = new Tree$ap<String- Integer>(); whi"e (scanner.has)e%t()) ( String word = scanner.ne%t(); if (m'ster'.contains&e'(word)) ( Integer c rrent = m'ster'.get(word); c rrent77; m'ster'.p t(word- c rrent); 1 e"se ( m'ster'.p t(word- 8); 1 1 3o""ection<String> co"" = m'ster'.4e'Set(); for(String str K co"") S'stem.out.print"n(str 7 " " 7 m'ster'.get(str)); 1 1

Spain 0 in 9 rain 8

S-ar putea să vă placă și