1. INSERT (X, P, L) - PowerPoint PPT Presentation

About This Presentation
Title:

1. INSERT (X, P, L)

Description:

??????: ????? ????. ????? ???????? ????? ????? ... (CURSORS) ????? ????????? (???? ????) ????? ???????. H. L. R. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. element next ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 53
Provided by: mathT
Category:
Tags: insert | cursors

less

Transcript and Presenter's Notes

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
  • (8) q NEXT(q,L)
  • (9) 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 returm(L.Last1) end
5
???? ?????? ?????? (?????)
6
????? ??????
type celltype record element
elementtype next?celltype end
LIST ?celltype Position
?celltype
7
Function 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 (N)? (1)?
(1)? PREVIOUS (1)? (N)? (N)? END (1)?
(N)? (N)? Position ???? ?????
(????) ????? (????) ?????? ???? ?????? ??
?????? ?????? ???? ????? ???? ???? 1 ????
??????? ???? ?????
12
????? ?????? ?????
?????? ?????? ????? 1. ????? ??? (END(L 2.
????? ???? PREVIOUS (???? ????) 3. Position ?????
????? ?????? ?????
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
  • ??? ????? ?? ?????
  • 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
???? ?????? ??? ????? ????. ???? ???. Mapping
?????? 61-65 AHO.
22
???? ?????? ????? ????????
  • ???? ????? ????????? ?????
  • ? ???? ????? ?????? ??????? ?????? ????, ???
    ????? ?????.
  • ???? ???? ????????? ?????
  • ? ????? ??????? ??????? ????? ?????? ?????

Prog A . . . i 15 Call B . . .
Subroutine B . . . i 9 . . . return
23
????????? ??????????
????? ???? - ?? ?????? ??? ????? ????. - ????
????? ???? ????? (?? ????) ????? ????
??????? ?????? ????? ???? STACK. ??? ?????
???????? ????? ?????? ?????? -(PUSH(S ????
??????? ??? ???? ???????? ????? ??????
??????? (POP(S ???????? ??????. ???? ???? ???
????? ?????.
24
???? ??????? ???? ???? ???????
  • ???? ???? ????, ??? ??????.
  • ????? STACK ???
  • 1) ?? ??????? ????????.
  • 2) ????? ????.
  • 3) ?????? ??????. ???? ???????.
    (?? ?????)

Function FACT(N) (1) If N lt 1 then return
(1) (2) ELSE begin (3) M FACT(N-1) (4)
Return(NM) (5) END
25
???? ????? ??????? ???????
While X do ? SUBROUTINE WHILE ( ) IF
X then do begin CALL WHILE ( ) ELSE
RETURN END
????? ?? ?? ??????? ??????? ???? ?????? ?????
????? ????? ?????? ???? ?????? ????? ???????? ??
?????????.
26
?????? ????????
(cormen 18.4 , p.367)
  • ????? ?- STACK, QUEUE ????? ????.
  • ????? ?? ? HASH, HEAP
  • ???? ?????? ???? ?????, ????? ???????
    ????? ????? ????.????? ?????? ?????? ???? -
    ?? ????? ?????????
  • ????? (????????) (???? ?????? Doubling)
  • 1) ???? ???? ?????? ????? (???? N2i)
  • 2) ??????? ????? ?????? ????? N ???????
  • ?) ???? ???? ??? ????? ????? N2N ?) ????
    ?????? ????? ?????. ?) ???? ???? ????.

27
  • ???????
  • 1) ???? ????? ?PER OPERATION) INSERT)
  • ?? ??? ????? (O(1
  • ?? ?? ????? (O(N
  • 2) AMORIZED ANALYSIS CORMEN 356-363) AMORTIZED
    COST)
  • ???? ??? ????? ?? ????? ????? ????? ?????.
  • ???? ? T(n)/n , ???? (T(n ??? ???? ??????? ?? n
    ??????.
  • ???? ????????? (ACCOUNTING)
  • ??????? ?????? ???? ???????.
  • ????? ????? ????? ?? ????? ??? ?????.
  • ??????? ??????? ????? ?????? ????? ?????? Credit
    ?????? (??????) ?????.

28
3) Amortized Complexity ?? ???? ?- Doubling
  • ?) ????? ????
  • ???? ????? ?? n ??????
  • n ??????.

? (T(n)O(n???? ????? - T(n)3n ????
?????? (Amortized) T(n)/n 3
29
?) ????? ????? ????????? (accounting)
??? ????? ???? ?????? ?? ???? 3
1 - ???? ??????. 1 - ???? ?????? ?????? ????. 1 -
?????? ???? (??? ??- n/2 ) ???? ??????
?????? ????.
30
???? Trees
?? ???? ?????
????? ?????????
1. ???? ????. ??? ?? ???? ???.
2. ?? n ??? ???? ? T1.TK ???? ????, ???? ?????
?? ??? ??? n ????? ? T1.TK ???? ???
????.
?????? ???? Node ?? Tree ???? Root ??-??
Subtree
31
????? ????? ??????? ????
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
32
??-?? - ???? ?? ?? ?????? ???? ??? - ???? ??????
????? ????? ?????? ????. (height) ???? ???? -
???? ?????? ?????? ?????. (depth)
??? ??????
??? - ?????? ???? ??????. ?????? ????? ?????.
?? ???? ?? ???? - ?? ?? ?????(unordered tree)
33
????? ??????? ?????? ?????? ???
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
34
Procedure 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
35
???? ?????? ??? ??? ?????? ???? ?????
PRE ??? ???? ???? ???????.
IN ??? ?? ???? ???????. ??? ???? ?????
???? ?????.
POST ??? ???? ???? ???????.
36
???? ??????? ???? ??????? (Labeled Trees,
Expression Trees)
???? ??????? ?????? ?? ????? ?????? ??????. ???
??????? ??? ????? ????? ?????.
?????
(ab)(cd)
?????? ?????? ??? ??????? 1. ?? ??? ?????
???????.
3. ??????? ????? - ??? ?????.
37
??? ?????? ?? ??? ????? ???? ???? ???? ?? ??????
- 1. Inorder - ????? ????? (ab)(cd) 2.
Postorder ( ?????) ab cd ? d
(??????? HP) (??? ??? ?????? !!) ?????
?????? ??????????? ????? ?? POST
Post(x) - desc(x) lt Post(y) lt Post(x)
38
???? ?????? ???????? ?? ??
?????? ???????? ?????
?) r ????? ????? ? U
?) ? r ?? i ????, i ??? ????. T1,Ti
39
????? ????? 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
40
????? ?? ????????? ?? PREORDER
  • ??? ????? ??? ????? ??? ?????
  • ???? ???? ????? ??? ????? ?? ????? ??????
  • ?? ??? - ???? ????? ????? ????? ?????
  • ???? ?????
  • ????? ?????? ??? ????? ??? ??? ?? ??????
    ?????? ?? ????? ??? ??? ??????.
  • ?????? ? ???????

41
???? ???? ?????????
?? ???? ????? 3 ???????
???? ???? ??????? ??????? ?? ??????? Cursors
42
???? ????????
- ?? ??? ?? ??? ???? ?? ?? ????? ?? ??? ????,
??? ?????
?????
???
- ???? ????? ???????
- ????? ????? ????????
???? ?????
Pre, Post - ???? ????? ??? ??? ????? ??? ????.
In - ?? ????? ???!
43
?????
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
?????? ? - ????? ??? ??????? ?????
????? ???? ??????? ??????
44
?????
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
- ???? ??? ??? (???? ????)
- ????? ??????
- ??????? ?????? ?? ?????
- ??? ?? ???? ???? ??????????
- ?? ??? ??? ?? ????? ????? ?????
45
?????
0.12 0.4 0.15 0.08
0.25
46
???? ??????
1) ????? ?? ???? ????? ??? ???? ??? ?????.
2) ??? ?? ????? ??????.
47
3) ????? ????-??? (?????)
???????
- ??? ????? ??????? ???? - ??????? ??.
- ????? ???? - ???? ?? TREE
- ????? ?? ? T1 ? T2 ?????? T.
- ????? T2 ???? ?????? ??? T2 ? TLAST ?????
??????? ? LAST
48
  • ???? ????????
  • ???? ???? ??????? ????????
  • type node record lchild ?node rchild
    ?node parent ?nodeend
  • ????? ???? ???????? ?????
  • ??? ????? ?????
  • ?? ???? x ??? i
  • Ichild(x) 2i ??
  • Rchild(x) 2i1
  • ??? ?????? ?? ??? ????? (?? ????? ???? ?????
    ??????).

49
X963.43
???? ????
  • ???? ?? ??????? ???? (???? ?????)
  • ???? ????? ????? ?- PARENT
  • ???? Ai ??? Ai ??? ??? (Cursor) ????? ??
    i
  • ???? ????? ?- LEFTMOST_CHILD
    RIGHT_SIBLING
  • ???? ????? LABEL (????? ????)

?????
????? LCHILD ?- RSIBLING ?? ????? ???
?????????? ?????? !
???? ???? ???? ???? ??? ?????? ????? .
50
X963.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
51
X963.48
??? ?????? (CREATEz (v,T1,Tz
1) ??? ????? ???? V ??? ?? ????? ?????
(??? ????? ????? Avail ????? ??????) 2) V.
left T1 Z) T1.right T2
52
X963.49
???? ?????? ??????? (??? ! )
????? ???? ?????? ???????
Write a Comment
User Comments (0)
About PowerShow.com