You are on page 1of 4

thi gia k HK1/2009

Mn: Cu trc d liu v Gii thut


Thi gian: 60 pht (Khng s dng ti liu)
Ghi ch: thi gm tt c 7 cu. Sinh vin lp KSTN lm ht 7 cu, thang im 12/12.
Sinh vin lp thng lm 6 cu (t cu 1 n cu 6), thang dim 10/10.

Cu 1 (1.5 im): Tnh ton big-O ca cc hm di y v sp xp chng theo th t t nh n ln theo
big-O:
p p:
a) (1 im) Tnh big-O
a. 2
n
=O(2
n
)
b. n! =O(n!)
c. n
3.5
=O(n
3.5
)
d. n +n
2
+n
3
=O(n
3
)
e. 10
5
=O(1)
f. 150,000 =O(1)
g. nlog
2
(n) =O(nlog
2
(n))

b) (0.5 im) Sp xp theo big-O:
10
5
<=150,000 <nlog
2
(n) <(n+n
2
+n
3
) <n
3.5
<2
n
<n!

Cu 2 (2.5 im): Cho mt DSLK n gm cc s nguyn c cu trc nh hnh
bn. Trong , mi thnh phn ca DSLK n l mt cu trc c data l s
nguyn v con tr link tr n phn t k tip. DSLK n ch dng mt con tr
head ch n phn t u tin ca danh sch. Nu danh sch rng, con tr
head ny l null. Vit mt phng thc bng pseudocode nhn vo mt s
nguyn, tm trong DSLK n v loi b i cc phn t c gi tr bng hoc hn
s nguyn ny 1 hoc 2. Lu , khng dng thm bt k phng phc hoc hm
ph tr no (k c t vit li).
V d, vi danh sch l {12, 13, 5, 6, -8, 9, 7, -2, 5, -1, 6, -3} v s nguyn nhn c l 5 th danh
sch kt qu l {12, 13, -8, 9, -2, -1, -3}, tc l cc phn t 5,6,7 b xa i.

p p:
algorithm remove_in_range (val x <int>)
Post Cc phn t c data y sao cho (y-x) l 0,1,2 b xa i
Trng H Bch Khoa Tp.HCM
Khoa KH&KT My tnh
P N
Node
data <int>
link <pointer>
end Node

Linked List
head <pointer>
end Linked List

1. pre =null, tmp =head
2. loop (tmp is not null)
1. if ((tmp->data ==x) or (tmp->data ==x+1) or (tmp->data ==x+2))
1. if (pre is null) //delete first
1. head =head->link
2. delete tmp
3. tmp =head
2. else //delete the element after pre
1. pre->next =tmp->link
2. delete tmp
3. tmp =pre->link
3. end if
2. else
1. pre =tmp
2. tmp =tmp->link
3. end if
3. end loop
end remove_in_range

Cu 3 (2 im): Vit
mt hm ton cc
(global function) bng
pseudocode nhn vo
mt queue v o ngc
queue . Gi s rng
cc phng thc ca
queue v stack c cho
theo c t ca hnh bn
cnh. Ch : khng c vit v dng thm cc hm ph tr no khc.

p p:
algorithm reverse_queue (ref queue <Queue>)
Post cc phn t trong queue s b o ngc v tr

1. stack =Create a stack
2. loop (not queue.isEmpty())
1. queue.QueueFront (x)
2. stack.Push (x)
3. queue.DeQueue()
3. end loop
4. loop (not stack.isEmpty())
1. stack.Top (x)
2. queue.EnQueue (x)
3. stack.Pop()
5. end loop
end reverse_queue
Stack ADT
<void>Create()
<ErrorCode>Push (val DataIn <DataType>) //Thm 1 phn t vo nh stack
<ErrorCode>Pop () //B phn t trn nh stack
<ErrorCode>Top (ref DataOut <DataType>) //Xem phn t trn nh stack
<boolean>isEmpty ()

Queue ADT
<void>Create()
<ErrorCode>EnQueue (val DataIn <DataType>) //Thm 1 phn t vo cui queue
<ErrorCode>DeQueue () //B 1 phn t u queue
<ErrorCode>QueueFront (ref DataOut <DataType>) //Xem phn t u queue
<ErrorCode>QueueRear (ref DataOut <DataType>) //Xem phn t cui queue
<boolean>isEmpty ()
Cu 4 (1.5 im): Hy trnh by tng bc qu trnh to mt cy nh phn tm kim (BST) bng cch thm
vo trong cy rng ban u cc kha ln lt nh sau: F,O,R,G,E,T bit rng gi tr so snh ca cc kha ny
l th t ca chng trong bng ch ci.
p p:






Cu 5 (1.5 im): Trnh by
tng bc qu trnh tm kim
kha 31 dng phng php tm
kim nh phn binary_search_1
(forgetful version) trn danh
sch lin kt (DSLK) n c
th t nh sau: {1, 12, 31, 35,
63, 98 }. C bao nhiu ln so
snh trn kha?



p p:

+bottom =0, top =5
bottom <top: true
mid =(0+5)/2 =2
target=31 >data
2
=31 : false =>top =mid =2 1 ln so snh

+bottom =0, top =2
bottom <top: true
mid =(0+2)/2 =1
target=31 >data
1
=12: true =>bottom ==mid+1 =2 1 ln so snh

+bottom =2, top =2
idx 0 1 2 3 4 5
data 1 12 31 35 63 98

<ErrorCode>binary_search_1 (val target <KeyType>, ref position <int>)

1. bottom =0
2. top =size of the list
3. loop (bottom <top)
1. mid =(bottom +top)/2
2. if (target >data
mid
)
1. bottom =mid +1
3. else
1. top =mid
4. end if
4. end loop
5. if (top <bottom)
1. return notFound
6. else
1. position =bottom
2. if (target =data
position
)
1. return found
3. else
1. return notFound
4. end if
7. end if
end binary_search_1
F
O
R G
F
O
R G
E
F
O
R
F F
O
F
O
R G
E
T
bottom <top: false
+target=31 =data
2
: true =>found 1 ln so snh

Vy c tng cng 3 ln so snh

Cu 6 (1 im): Cho cy nh phn nh hnh v, hy cho bit kt qu thc thi ca gii thut sau nu gii thut
c gi t phn t gc ca cy (nt c gi tr 12).








p p:
<12><3><10><1><22><7><9><13><5><21><8><6><17>

Cu 7 (2 im Dnh cho lp KSTN): Danh sch lin kt n vng (xem
c t hnh bn cnh) c qun l nh sau:
- Con tr current ch n phn t u tin.
- Nu danh sch rng, current l NULL. Ngc li, con tr link ca
phn t cui ch vo phn t u tin.
Vit mt phng thc bng pseudocode m s phn t ca danh sch
ny. Lu , khng dng thm bt k phng phc hoc hm ph tr no (k c t vit li).
p p:
algorithm circular_list_size ()
Pre Danh sch lin kt n vng
Post S phn t c m
Return S phn t ca danh sch

1. if (current is null)
1. return 0
2. else
1. size=1
2. tmp =current
3. loop (tmp->link !=current)
1. size++
2. tmp =tmp->link
4. end loop
3. end if
4. return size
end circular_list_size
10
1 22
7
9
3
8
17 6
5
21
13
12
al gori thm XYZ (val subroot <pointer>)
1. i f (subroot is not null)
1. print "<"
2. print (subroot->data)
3. print ">"
4. XYZ (subroot->left)
5. XYZ(subroot->right)
2.end if
end XYZ
Node
data <int>
link <pointer>
end Node

Circular Linked List
current <pointer>
end Circular Linked List

You might also like