Title: 1. INSERT (x, p, L)
1???? ?????? ??????? ???????
- ??????
- ????? ????
- ????? ???????? ????? ????? ??????
- ???? ????? ?????, ?????, ?????, ?????.
2??????
- 1. INSERT (x, p, L)
- ?? ?? p ?????? a1, a2,..,ap-1,
x, ap,.,an - ?? (pEND(L
a1, a2,..,an,x - ???? Undefined
- 2. LOCATE(x, L)
- ???? ????? ?? .x ?? ?? ????? (END(L
- 3. RETRIEVE (p,L)
- ????? ?? ?? p ???? ? - L.
- 4. DELETE(p,L)
- a1, a2,..,an ?a1, a2,..,ap-1,ap1,.,an
????? ?? ?? p ? -L. - 5. NEXT(p,L), PREVIOUS(p,L) ????? ????
- 6. MAKENULL(L)
- 7. FIRST(L)
3????? ????? ??????
- Procedure PURGE (Var LLIST)
- Var
- p,q Position
- p Current, q Search
- begin
- (1) p FIRST(L)
- (2) while pltgtENDL do begin
- (3) q NEXT(p,L)
- (4) while qltgtEND(L) do
- (5) if same (RET(p,L), RET(q,L))
- (6) then DELETE(q,L)
- (7) else
- q NEXT(q,L)
- end
- (10) p NEXT(p,L)
- end
- end PURGE
4??????? ?? ???? ???????
- ????, ????? ??????, (?????)
- ????? ?????
Const maxlength 99 type LIST record
elements array1..maxlength of element type
last integer end Position
integer function END(var LLIST)
Position begin return(L.Last1) end
5????? ?????? ?????? (?????)
- ?? ?? ????? ? - ??? ????? -
????? ????? ??? - ????? ?? ???????
6????? ??????
type celltype record element
elementtype next?celltype end
LIST ?celltype Position
?celltype
7Function END (LLIST) position returns a
pointer to the last cell of L Var q
position begin q L while q ?. next lt gt nil
do q q ? . next return (q) end (END)
8????? ????? ??????? ?????
(CURSORS)
- ????? ????????? (???? ????) ????? ???????.
- ??????? ??? ?????? ( ????? ??????)
- ????? ????? ?? 0.
- ???? - ???? ?????? ????? ?? 0.
9?????
L y, e, s M n, o Avail 9, 10, 11, 6, 2, 4
10????? ????
??? ?? ??? ??????? ??????? ?????????? ??? ???
??? ?????? ????!!!! ????? ??? p ???? ??? q
??? ?- p ????? ???? ???? ???? ??? ?-q ?????
????. ???? (?? ???? ????!) ?) ???? ? temp ?)
?????? ????? ????? ?????.
11?????? ???????
???????? ???? ?????? ????? ????
??????? ????? ?? ?????
?????/???? INS/DEL O(N) O(1)
O(1) PREVIOUS O(1) O(N) O(N) END O(1)
O(N) O(N) Position ???? ?????
(????) ????? (????) ?????? ???? ?????? ??
?????? ?????? ???? ????? ???? ???? 1
???? ??????? ???? ?????
12????? ?????? ?????
?????? ?????? ????? 1. ????? ??? (END(L 2.
????? ???? PREVIOUS (???? ????)
????? ?????? ?????
type celltype record element
elementtype next, previous?celltype
end Position ?celltype
????? ????? DELETE
13????? ????? ????
?????? ? ???? ????? ?????? ??????? ?? NIL.
?????? ? ????? ?????? (????? ?? HEAD )
14?????? ????? ????? ??????
- ???????
- ???? ?????? ???????
- ???? ?????? ????
- ????? ????????? ?????
- ???????
- ?? ????? ???? ????.
15???? ?????? ???????
- ?????? STACK
- ???? ???? ?? ????.
- LIFO
- ????? ?????? ???? ?????? ?? ??????
??????
1. MAKE NULL (S) 2. TOP (S) 3. POP (S) 4. PUSH
(S) 5. EMPTY (S) (TRUE FALSE)
16- ???????
- ?? ??????? ?????? ????? ?? ??????.
- ?????? ??????? ?? ????? ????.
- ???? - ????. ??? ????? ??????. (
?? ????? ?????? (O(N )
?????
Type STACK record top integer elements
array 1MAXL of element type end ???????
????????? ???? ?????? ????. ???? ???.
17??? Queue
??? Queue
- ??? ????? ?? ?????
- FIFO
- ?????? ?- REAR , ?????? ?- FRONT .
??????
1) MAKENULL (Q) 2) ENDQUEUE (x,Q) INSERT (x,END
(Q),Q) 3) FRONT (Q) RET (FIRST (Q), Q) 4)
DEQUEUE (Q) DELETE (FIRST (Q), Q) 5) EMPTY (Q)
TRUE FALSE
18- ?????
- ?? ???????? ??????? ?????? (??????)
- ???? ?????? ????? ?????. ?????? ????
???? ????? ?????? ?????? ?? ???????. - ???? ??? ??? (?????? ????? ?????? ????)
- ??????? ?? ????? ????.
TYPE QUEUE Record front, rear ? celltype
19??????? ???????
MAKENULL (Q)
ENQUEUE (X)
ENQUEUE (Y)
DEQUEUE (Q)
20????? ??? ????? (?????)
REAR, FRONT - ???? ?? ????? FRONT - ?? ?????,
REAR - ?? ????? F R ? ???? ???? F R 1 ?
??? F R 2 ? ???
21????? ?????? ?????? ????????
- ???? ????? ????????? ?????
- ? ???? ????? ?????? ??????? ?????? ????, ???
????? ?????. - ???? ???? ????????? ?????
- ? ????? ??????? ??????? ????? ?????? ?????
Prog A . . . i 15 Call B . . .
Subroutine B . . . i 9 . . . return
22????????? ??????????
????? ???? - ?? ?????? ??? ????? ????. - ????
????? ???? ????? (?? ????) ????? ????
??????? ?????? ????? ???? STACK. ??? ?????
???????? ????? ?????? ?????? -(PUSH(S ????
??????? ??? ???? ???????? ????? ??????
??????? (POP(S ???????? ??????. ???? ???? ???
????? ?????.
23????? ??????? ???? ???? ?? ???????
- ???? ???? ????, ??? ??????.
- ????? STACK ???
- 1) ?? ??????? ????????.
- 2) ????? ????.
- 3) ?????? ??????. ???? ???????.
(?? ?????)
- Function FACT(N)
- If N lt 1 then return (1)
- ELSE begin
- M FACT(N-1)
- Return(NM)
- (5) END
24????? ????? ??????? ???????
While X do ? SUBROUTINE WHILE ( ) IF
X then do begin CALL WHILE ( ) ELSE
RETURN END
????? ?? ?? ??????? ??????? ???? ?????? ?????
????? ????? ?????? ???? ?????? ????? ???????? ??
?????????.
25?????? ????????
(cormen 18.4 , p.367)
- ????? ?- STACK, QUEUE ?????? ????.
- ????? ?? ? HASH, HEAP
- ???? ?????? ???? ?????, ????? ??????? ?????
????? ????.????? ?????? ?????? ???? - ?? ?????
????????? - ????? (????????) (???? ?????? Doubling)
- 1) ???? ???? ?????? ????? (???? N2i)
- 2) ??????? ????? ?????? ????? N ???????
- ?) ???? ???? ??? ????? ????? 2N ?) ???? ??????
????? ?????. ?) ???? ???? ????.
26????????
- 1) ???? ????? ?PER OPERATION) INSERT)
- ?? ??? ????? (O(1
- ?? ?? ????? (O(N
- 2) AMORTIZED ANALYSIS CORMEN 356-363) AMORTIZED
COST) - ???? ??? ????? ???? ????? ????? ????? ?????.
- ???? ? T(n)/n , ???? (T(n ??? ???? ??????? ?? n
??????. - ???? ????????? (ACCOUNTING)
- ??????? ?????? ???? ???????.
- ????? ????? ????? ?? ????? ??? ?????.
- ??????? ??????? ????? ?????? ????? ??????
Credit?????? (??????) ?????.
273) Amortized Complexity ?? ???? ?- Doubling
- ?) ????? ????
- ???? ????? ?? n ??????
- n ??????.
? (T(n)O(n???? ????? - T(n)3n ????
?????? (Amortized) T(n)/n 3
28????? ????? ????????? (accounting)
??? ????? ???? ?????? ?? ???? 3
1 - ???? ??????. 1 - ???? ?????? ?????? ????. 1 -
?????? ???? (??? ??- n/2 ) ???? ?????? ??????
????.
29???? (Trees)
???? Trees
?? ???? ??????
????? ?????????
1. ???? ????. ??? ?? ???? ???.
2. ?? n ??? ???? ? T1.TK ???? ????, ???? ?????
?? ??? ???n ????? ? T1.TK ???? ??? ????.
?????? ???? Node ?? Tree ????
Root ??-?? Subtree
30????? ????? ??????? ????
book c1 s1.1 s1.2 s1.3 c2
s2.1 s2.2 c3 s3.1
??????
book - ????/Parent (???) ?? c1, c2, c3
c1, c2 - ?????/children ?? book
s2.1 - ????/Descendant (?? ????) ?? book
n1, n2,nk - ?????/Path (?? ?? ???? ?? ?????)
???? ?????? ?? ??????
?? ?????? (???? ???)
???? ??? ????? ???/Leaf
n1 - ?? ?????/Ancestor ?? nk
31??-?? - ???? ?? ?? ?????? ???? ??? - ???? ??????
????? ????? ?????? ???? (height) ???? ???? - ????
?????? ?????? ????? (depth)
??? ??????
??? - ?????? ???? ??????. ?????? ????? ?????.
?? ???? ?? ???? - ?? ?? ????? (unordered tree)
32????? ????????? ?????? ?????? ???
????? ??????? ?????? ?????? ???
Preorder , postorder , Inorder
- ????? ?????????
- ?? T ??? ??? - ??? ?????? ????? ??? Preorder
(Post,IN) - ?? T ??? ???? ???? (n) ??? ?????? ?????? ?- n
???? Post,In) Preorder) - ???? ?? T ??? ?? ?? ???? n ???? ???? T1,,Tk ???
n, PRE(T1), , PRE(TK) PREORDER
POST (T1), , POST (TK), n POST
IN(T1), n , IN(T2), ,IN(TK) IN
33Procedure PREORDER (n node) (1) list n (2)
for each child c of n, in order from left
do PREORDER (C) end
?????? ????? POSORDER, INORDER ????.
?????
1,2,3,4,5,6,7,8 PRE
3,2,5,4,6,1,7,8 IN
3,5,6,4,2,7,8,1 POST
34???? ??????
??? ??? ?????? ???? ?????
PRE ??? ???? ???? ???????.
IN ??? ??? ???? ???????. ??? ???? ?????
???? ?????.
POST ??? ???? ???? ???????.
35???? ??????? ???? ???????
(Labeled Trees, Expression Trees)
???? ??????? ?????? ?? ????? ?????? ??????. ???
??????? ??? ????? ????? ?????.
?????
(ab)(cd)
?????? ?????? ??? ??????? 1. ?? ??? ?????
???????.
3. ??????? ????? - ??? ?????.
36??? ?????? ?? ??? ????? ???? ???? ???? ?? ??????
- 1. Inorder - ????? ????? (ab)(cd) 2.
Postorder ( ?????) ab cd ? (???????
HP) (??? ??? ?????? !!) ????? ??????
??????????? ????? ?? POST
Post(x) - desc(x) lt Post(y) lt Post(x)
37???? ?????? ???????? ?? ??
?????? ???????? ?????
?) r ????? ????? ? U
?) ? r ?? i ????, i ??? ????. T1,Ti
38????? ????? PREORDER ????? ? ADT
Procedure PREORDER (n node) list the
descendants of n in PREORDER var c node
Begin print (LABEL (n, T)) c
LEFTMOST_CHILD(n, T) while c ltgt Nil do
begin PREORDER(c) c
RIGHT_SIBLING(c, T) end end PREORDER
39????? ?? ????????? ?? PREORDER
- ??? ????? ??? ????? ??? ?????
- ???? ???? ????? ??? ????? ?? ????? ??????
- ?? ??? - ???? ????? ????? ????? ?????
???? ????? ????? ?????? ??? ????? ??? ??? ??
?????? ?????? ?? ????? ??? ??? ??????.
?????? ? ???????
40Procedure NPREORDER(T TREE) nonrecursive
preorder traversal of tree T var
mnode a temporary SSTACK stack of
nodes holding path from the root to the parent
TOP(S) of the current node m begin
initialize MAKENULL(S) m
ROOT(T) while true do if m lt gt L then
begin print (LABEL(m, T)) PUSH(m,
S) explore leftmost child of m m
LEFTMOST_CHILD(m, T) end else begin
exploration of path on stack is now complete
if EMPTY(S) then return explore right
sibling of node on top of stack m
RIGHT_SIBLING(TOP(S), T) POP(S) end
end NPREORDER
41????? ????
X963.43
- ???? ?? ??????? ???? (???? ?????)
- ???? ????? ????? ?- PARENT
- ???? Ai ??? Ai ??? ??? (Cursor) ????? ??
i - ???? ????? ?- LEFTMOST_CHILD
RIGHT_SIBLING - ???? ????? LABEL (????? ????)
?????
????? LCHILD ?- RSIBLING ?? ????? ??? ?????
?????? ?????? !
???? ???? ???? ???? ??? ?????? ????? .
42X963.44
??? - ???? R_SIBLING
Function R_SIBLING (nnode TTree) node
return R_SIB of n in T (o else)
var i,parent node begin parent T(n) for
i n1 to max nodes do if T(i) parent
then return (i) return (0) end DATA
TYPES type node integer TREE array 1
max nodes of node
43????? ???? ??-??? ?????? ????
- ??? ???? - ????? ???? - ?? ???? ????? - ????,
??? ???? ???? ????? ? ?????? ?????? ??????.
44???? ??????? ??????
Type node integer list position
Tree record header array1..MAXNODES
of list labels array1..MAXNODES of label
type end
???? L-CHILD Function L-CHILD(n node, T Tree)
node var Llist begin L T.headern
if emptyL then return(0) else
return(RETRIEVE(FIRST(L), L)) end
45????? ?????? ?? ?????? ??????? ???? ??????
- ??????? ????????
- ???? ????? ??? ??? ???????.
- ??? ????? ?????.
- ??? ???? ???? (...,CREATEi(V
- ???? ?? ????? ? cellspace ??????? ??? ??? ??
header ????.
46????? ????
- ?? ??????????? ????? ???
- ?? ????? (?????)
- ?? ????? (????)
??? ???? ??? ? Parent
??????? 1) ???? ??? ????? (???? ????, ??????
???) 2) ?? ???? ?? ??? ????? ?????
47??? ?????? (CREATE2(V,T1,T2?
1) ??? ???? ???? V ??? ?? ????? ????? (???
????? ????? available ????? ??????)
2) V.left T1
3) T1.right T2
48????? ???? ?????????
?? ???? ????? 3 ???????
???? ???? ??????? ??????? ?? ??????? Cursors
49????? ?????? ??????? (???!)
X963.49
????? ???? ?????? ???????
50???? ????????
- ?? ??? ?? ??? ???? ?? ?? ????? ?? ??? ????,
??? ?????
?????
???
- ???? ????? ???????
- ????? ????? ????????
???? ?????
Pre, Post - ???? ????? ??? ??? ????? ??? ????.
In - ?? ????? ???!
51?????
1) ???? ?? ??????
Var cellspace array1maxnodes of record
leftchild integer rightchild integer
end
????? ??? Huffman
- ???? ?? ?? ?????? a, b, c, d, e
- ??????? ???? ?????????? ????? 0.12, 0.4, 0.15,
0.08, 0.25
- ????? ????? ? 0/1 ????? ?????
?????? ? - ?????
a 0 0 0 b 0 0 1 c 0 1 0 d 0 1 1 e
1 0 0
?????? ? - ????? ??? ??????? ?????
????? ???? ??????? ??????
???? ????? 3
52?????
a 0 0 0 b 1 1 c 0 1 d 0 0 1 e 1 0
???? ????? 3(0.120.15)2(0.40.082.5)2.27
???? ?????? ?????? ?????????? ??? ??? ???????
????.
???? ??? ??????? ????? ?? ??????
- ?????? ?????
- 0/1 ?????? (????0, ????1)
???? Huffman
- ???? ??? ??? (???? ????)
- ????? ??????
- ??????? ?????? ?? ?????
- ??? ?? ???? ???? ??????????
- ?? ??? ??? ?? ????? ????? ?????
53?????
54???? ??????
1) ????? ?? ???? ????? ??? ???? ??? ?????.
2) ??? ?? ????? ??????.
553) ????? ????-??? (?????)
???????
- ??? ????? ??????? ???? - ??????? ??.
- ????? ???? - ???? ?? TREE
- ????? ?? ? T1 ? T2 ?????? T.
- ????? T2 ???? ?????? ??? T2 ? TLAST ?????
??????? ? LAST
56???? ????????
- ???? ???? ??????? ????????
- type node record lchild ?node rchild
?node parent ?nodeend - ????? ???? ???????? ?????
- ??? ????? ?????
- ?? ???? x ??? i
- Ichild(x) 2i ??
- Rchild(x) 2i1
- ??? ?????? ?? ??? ????? (?? ????? ???? ?????
??????). -
57 X963.48
??? ?????? (CREATEz (v,T1,Tz
1) ??? ????? ???? V ??? ?? ????? ?????
(??? ????? ????? Avail ????? ??????) 2) V.
left T1 3) T1.right T2