Вы находитесь на странице: 1из 9

Exercises

For Exercises 1-10, indicate which structure would be a more suitable choice for each of
the following applications by marking them as follows:
A. Stack
B. Queue
C. Tree
D. Binary search tree
E. Graph
1.
A bank simulation of its teller operation to see how waiting times
would be affected by adding another teller
!
2.
A program to recei"e data that is to be sa"ed and processed in the
re"erse order
A
3.
An electronic address book ordered by name
#
4.
A word processor to ha"e a $F key that causes the preceding command
to be redisplayed E"ery time the $F key is pressed, the program is to
show the command that preceded the one currently displayed
A
5.
A dictionary of words used by a spelling checker to be built and
maintained
#
6.
A program to keep track of patients as they check into a medical clinic,
assigning patients to doctors on a first-come, first-ser"ed basis
!
7.
A program keeping track of where canned goods are located on a shelf
A
8.
A program to keep track of the soccer teams in a city tournament
%
9.
A program to keep track of family relationships
% or E
10.
A program to maintain the routes in an airline
E
For Exercises 11 - 30 !ark the ans"ers true or #a$se as #o$$o"s%
A. True
B. Fa$se
11.
A binary search cannot be applied to a tree
!
12.
A stack and a &ueue are different names for the same A#'
!
13.
A stack displays F(F) beha"ior
!
14.
A &ueue displays *(F) beha"ior
!
15.
A leaf in a tree is a node with no children
A
16.
A binary tree is a tree in which each node can ha"e +ero, one, or two
children
A
17.
A binary search tree is another name for a binary tree
!
18.
'he "alue in the right child of a node ,if it exists- in a binary search
tree will be greater than the "alue in the node itself
A
19.
'he "alue in the left child of a node ,if it exists- in a binary search tree
will be greater than the "alue in the node itself
!
20.
(n a graph, the "ertices represent the items being modeled
A
21.
Algorithms that use a list must know whether the list is array based or
linked
!
22.
An list may be linear or nonlinear, depending on its implementation
!
23.
'he root of a tree is the node that has no ancestors
A
24.
!inary search trees are ordered
A
25.
)n a"erage, searching in a binary search tree is faster than searching in
a list
!
26.
)n a"erage, searching in a binary search tree is faster than searching in
a linked list
A
27.
A binary search tree is always balanced
!
28.
.i"en the number of nodes and the number of le"els in a binary search
tree, you can determine the relati"e efficiency of a search in the tree
A
29.
(nsertion in a binary search tree is always into a leaf node
!
30.
A binary search tree is another implementation of a sorted list
A
'he following algorithm ,used for Exercises /1 - //- is a count-controlled loop going from 1 through 0 At
each iteration, the loop counter is either printed or put on a stack depending on the result of !oolean
function RanFun() ,'he beha"ior of RanFun() is immaterial- At the end of the loop, the items on
the stack are popped and printed !ecause of the logical properties of a stack, this algorithm cannot print
certain se&uences of the "alues of the loop counter 1ou are gi"en an output and asked if the algorithm could
generate the output 2espond as follows:
A 'rue
! False
% 3ot enough information
Set count to 0
&'()E *count + ,-
Set count to count . 1
(F */anFun*--
&rite count 0 0
E)SE
1ush*!yStack count-
&'()E *23T (sE!pty*!yStack--
1op*!yStack nu!4er-
&rite nu!4er 0 0
31. 'he following output is possible using a stack: 1 / 0 4 5
!
32.
'he following output is possible using a stack: 1 / 0 5 4
A
33.
'he following output is possible using a stack: 1 / 0 1 /
!

'he following algorithm ,used for Exercises /5 - /6- is a count-controlled loop going from 1 through 0 At
each iteration, the loop counter is either printed or put on a &ueue depending on the result of !oolean
function RanFun() ,'he beha"ior of RanFun() is immaterial- At the end of the loop, the items on
the &ueue are de&ueued and printed !ecause of the logical properties of a &ueue, this algorithm cannot print
certain se&uences of the "alues of the loop counter 1ou are gi"en an output and asked if the algorithm could
generate the output 2espond as follows:
A 'rue
! False
% 3ot enough information
Set count to 0
&'()E *count + ,-
Set count to count . 1
(F */anFun*--
&rite count 0 0
E)SE
En5ueue*!yQueue count-
&'()E *23T (sE!pty*!yQueue--
De5ueue*!yQueue nu!4er-
&rite nu!4er 0 0
34. 'he following output is possible using a &ueue: 1 / 0 4 5
A
35.
'he following output is possible using a &ueue: 1 / 0 5 4
!
36.
'he following output is possible using a &ueue: 1 / 0 1 /
!
Exercises 36 7 ,0 are short-ans"er 5uestions.
37. 7hat is written by the following algorithm8
1ush*!yStack ,-
1ush*!yStack 8-
1ush*!yStack 8-
1op*!yStack ite!-
1op*!yStack ite!-
1ush*!yStack ite!-
&'()E *23T (sE!tpy*!yStack--
1op*!yStack ite!-
&rite ite! 0 0
5 0
38. 7hat is written by the following algorithm8
En5ueue*!yQueue ,-
En5ueue*!yQueue 8-
En5ueue*!yQueue 8-
De5ueue*!yQueue ite!-
De5ueue*!yQueue ite!-
En5ueue*!yQueue ite!-
&'()E *23T (sE!tpy*!yQueue--
De5ueue*!yQueue ite!-
&rite ite! 0 0
5 5
39.
7rite an algorithm that sets 4otto! e&ual to the last element in the
stack, lea"ing the stack empty
&'()E *23T (sE!pty*!yStack--
1op*!yStack 4otto!-
40.
7rite an algorithm that sets 4otto! e&ual to the last element in the
stack, lea"ing the stack unchanged
&'()E *23T (sE!pty*!yStack--
1op*!yStack 4otto!-
push*ne"Stack 4otto!-
&'()E *23T (sE!tpy*ne"Stack--
1op*ne"Stack ite!-
1ush*!yStack ite!-
41.
7rite an algorithm to create a copy of !yStack, lea"ing !yStack
unchanged
&'()E *23T (sE!pty*!yStack--
1op*!yStack ite!-
push*ne"Stack ite!-
&'()E *23T (sE!tpy*ne"Stack--
1op*ne"Stack ite!-
1ush*!yStack ite!-
1ush*copyStack ite!-
42.
7rite an algorithm that sets $ast e&ual to the last element in a &ueue,
lea"ing the &ueue empty
&'()E *23T (sE!pty*!yQueue--
De5ueue*!yQueue $ast-
43.
7rite an algorithm that sets $ast e&ual to the last element in a &ueue,
lea"ing the &ueue unchanged
&'()E *23T (sE!pty*!yQueue--
De5ueue*!yQueue $ast-
En5ueue*ne"Queue $ast-
&'()E *23T (sE!tpy*ne"Queue--
De5ueue*ne"Queue ite!-
En5ueue*!yQueue ite!-
44.
7rite an algorithm to create a copy of !yQueue, lea"ing
!yQueue unchanged
&'()E *23T (sE!pty*!yQueue--
De5ueue*!yQueue $ast-
En5ueue*ne"Queue $ast-
&'()E *23T (sE!tpy*ne"Queue--
De5ueue*ne"Queue ite!-
En5ueue*!yQueue ite!-
En5ueue*copyQueue ite!-
45.
7rite an algorithm /ep$ace that takes a stack and two items (f the
first item is in the stack, replace it with the second item, lea"ing the
rest of the stack unchanged
/ep$ace*!yStack 9rst secon:-
1op*!yStack ite!-
&'()E *(te! 23T e5ua$ #urst-
push*ne"Stack ite!-
1op*!yStack ite!-
(F *23T (sE!pty*!yStak--
1ush*ne"Stack secon:-
&'()E *23T (sE!tpy*ne"Stack--
1op*ne"Stack ite!-
1ush*!yStack ite!-
46.
7rite an algorithm /ep$ace that takes a &ueue and two item (f the
first item is in the &ueue, replace it with the second item, lea"ing the
rest of the &ueue unchanged
/ep$ace*!yQueue 9rst secon:-
De5ueue*!yQueue ite!-
&'()E *(te! 23T e5ua$ #urst-
En5ueue*ne"Queue ite!-
De5ueue*!yQueue ite!-
(F *23T (sE!pty*!yQueue--
En5ueue*ne"Queue secon:-
&'()E *23T (sE!tpy*ne"Queue--
De5ueue*ne"Queue ite!-
En5ueue*!yQueue ite!-
47.
#raw the binary search tree whose elements are inserted in the
following order:
50 72 96 94 107 26 12 11 9 2 10 25 51 16 17 95
insert tree like one on page 690 (11) of C++ Plus, 3rd edition. remove
box and arrow. as answer
48.
(f 1rint is applied to the tree formed in Exercise 59, in which order
would the elements be printed8
2 9 10 11 12 16 17 25 26 50 51 72 94 95 96 107
49.
Examine the following algorithm and apply it to the tree formed in
Exercise 59 (n which order would the elements be printed8
1rint; *tree-
(F *tree is 23T nu$$-
1rint *ri<ht*tree-- == /ecursi>e ca$$ /1
&rite in#o*tree-
1rint*$e#t*tree-- == /ecursi>e ca$$ /;
107 96 95 94 72 51 50 26 25 17 16 12 11 10 9 2
50.
Examine the following algorithm and apply it to the tree formed in
Exercise 59 (n what order would the items be printed8
1rint3 *tree-
(F *tree is 23T nu$$-
1rint *ri<ht*tree-- == /ecursi>e ca$$ /1
1rint*$e#t*tree-- == /ecursi>e ca$$ /;
&rite in#o*tree-
107 95 94 96 51 72 17 16 25 10 2 9 11 12 26 50
Exercises ,1 7 ,, are short ans"er 5uestions 4ase: on the #o$$o"in< :irecte:
<raph.
???????
???????
51.
(s there a path from )regon to any other state in the graph8
3o
52.
(s there a path from :awaii to e"ery other state in the graph8
1es
53.
From which state,s- in the graph is there a path to :awaii8
'exas
54.
;how the table that represents this graph
An F !eans that there in not a $ink@ a T !eans there is a $ink.
A$aska
F F F F T F F
Ca$i#ornia
F F F F F F F
'a"aii
T T F T F T F
2e" Aork
F F F F F F F
3re<on
F F F F F F F
Texas
F F T F F F T
Ber!ont
T T F T F F F
A$aska Ca$i#. 'a"aii 2e" Aork 3re<on Texas Ber!ont
55.
%an you get from Ber!ont to 'a"aii8
3o
????????
???????
Exercises ,C 7 C0 are short ans"er 5uestions 4ase: on the #o$$o"in< :irecte:
<raph.
56.
;how the depth first tra"ersal from Dean to San:er
<ean, *ance, Fran, <ohn, ;usan, ;ander ,always taking the left-most arc
if there is more than one-
57.
;how the depth first tra"ersal from )ance to Dar$ene
*ance, Fran, <ohn, ;usan, #arlene
58.
;how the breadth first tra"ersal from Dean to San:er
<ean, *ance, ;usan, Fran, !rent, ;ander
59.
;how the breadth first tra"ersal from )ance to Dar$ene
*ance, Fran, <ean, !rent, <ohn, ;usan, #arlene
60.
;how the table that represents this graph
Brent
F F F T F F T F F F
Dar$ene
F F F F F F F T F T
Fran
F F F F F T T F T F
Fre:
T F F F F F F F F F
Dean
F F F F F F T F F T
Dohn
F F T F F F F F F T
)ance
T F T F T F F F F F
Eike
F T F F F F F F F F
San:er
F F T F F F F F F T
Susan
F T F F T T F F T F
Brent Dar$ene Fran Fre: Dean Dohn )ance Eike San:er Susan
Exercise C1 7 CF are short ans"er exercises.
61.
.i"en the record )ist containing the array >a$ues and the "ariable
$en<th, write the algorithm for Get)en<th
Get)en<th
/ETG/2 $en<th
62.
Assume that record )ist has an additional "ariable current1osition,
initiali+ed to the first item in the list 7hat is the "alue of
current1osition8
0
63.
7rite the algorithm for Eore(te!s, which returns T/GE if there are
more items in the list, and FA)SE otherwise
Eore(te!s*$ist-
/ET/G2 )ist.current1osition 23T e5ua$ to $en<th
64.
7rite the algorithm for Get2ext*!y)ist ite!- so that ite! is the
next item in the list !e sure to update current1osition
Get2ext*$ist ite!-
Set ite! to $ist.>a$uesH$ist.current1ositionI
Set $ist.current1osition to $ist.current1osition . 1
65.
Exercises 61- 65 create the algorithms that allow the user of a list to see
the items one at a time 7rite the algorithm that uses these operations to
print the items in a list
&'()E *Eore(te!s*$ist--
Get2ext*$istite!-
1rint*ite!-
66.
7hat happens if an insertion or deletion occurs in the middle of an
iteration through the list8 Explain
1ou cannot predict exactly what might happen, but you can be sure it
probably will cause problems (t depends on where you are in the
tra"ersal (f you add an item at the end of an unsorted list, e"erything
will probably be ok (f you add an item in a sorted list, you may miss
the new item in the tra"ersal (f you delete an item, it might ha"e
already processed the item
67.
%an you think of a way to keep the user from doing an insertion or
deletion during an iteration8
As a part of the documentation, write that no operations that change the
list can be made during an iteration ;uch statements are called $re-
conditions or assumptions
68. #istinguish between "alue and reference parameters
A "alue parameter is one for which a copy of the argument is gi"en to
the subprogram A reference parameter is one for which the address of
the argument is gi"en to the subprogram
69. :ow are arguments and parameters matched8
Arguments and parameters are matched by position on the subprogram
heading and the calling statement