Data Structure - PowerPoint PPT Presentation

1 / 57
About This Presentation
Title:

Data Structure

Description:

Title: Data Structure and Algorithm Author: stl0978 Last modified by: alaska Created Date: 11/1/2004 9:47:39 PM Document presentation format ... – PowerPoint PPT presentation

Number of Views:142
Avg rating:3.0/5.0
Slides: 58
Provided by: stl0
Category:

less

Transcript and Presenter's Notes

Title: Data Structure


1
Data Structure
???????????????
2
Data Structure
  • ???????????????????????? (Tree)
  • ???????????????????????
  • ?????????????????
  • ??????????????????????????????????
  • ??????????????????????????? (Binary Tree)
  • ???????????????????????????????

3
Data Structure and Algorithm?????????????????????
???????
  • ?????????????????????????????????

Root Node
Node
Link
?????????????????
????????????????????????
4
Data Structure and Algorithm?????????????????????
???????
  • ????????????????????????
  • ???????????????????????? (Non Linear)
    ??????????????????????????? ????????????????
    ????????????????????????????????????
  • ???????????????????????? ?????????????????????????
    ??????????????? ????????? ????????????????
  • ?????????????????? ???????????? (Node)
    ??????????????????????
  • ?????????????????????????? (Link)

5
Data Structure and Algorithm?????????????????????
???????
  • ????????????????????
  • ??????????????????? ?????????? (root node), R
  • ???????? ? ???????????? ?????????????????? n
    ????? ?????????????????????????????
    ???????????????????????????????
    ????????????????????????? (Sub tree)

6
Data Structure and Algorithm?????????????????????
???????
  • ???????????????
  • R ???????????????????
  • ???? A,B,C,D
  • A ???????????????????
  • ???? E, F, G
  • F ???????????????????
  • ???? J
  • B ??????????????????????? H ??? I

R
A
B
C
D
E
F
H
I
G
J
Sub Tree
7
Data Structure and Algorithm?????????????????????
???????
  • ???????????????????? ? ?????????
  • ????????????????????????????? ????????????????????
    ????????????????
  • ???? Father, Son, Grandson
  • ?????????????????????? Mother, Child, Grandchild
  • ???????????? (Level)
  • ?????????????????????????????????????????????????
    ??????
  • ??????????????????????????? 1 (??????????????????
    ???????????? 0)
  • ???????????????????????????????? ???? 3 1
    (???? 3 0) ???????

8
Data Structure and Algorithm?????????????????????
???????
  • ????????????
  • R ?????????? ????? 1
  • A,B,C,D ???????????? 2
  • E,F,G,H,I ???????????? 3
  • J ???????????? 4

R
A
B
C
D
E
F
H
I
G
J
9
Data Structure and Algorithm?????????????????????
???????
  • ???????????? (Level Degree)
  • ?????????????????????????????
  • A ??????? ???? 3
  • B ??????? ???? 2
  • F ??????????? 1
  • C,D,E,G,H,I,J ??????????? 0

R
A
B
C
D
E
F
H
I
G
J
10
Data Structure and Algorithm?????????????????????
???????
  • ????????????? (Leaf Node)
  • ????????????????????? 0
  • ????????????????????????? 0 ?????????????????
    (interior/branch node)
  • A,B,F ?????????????
  • C,D,E,G,H,I,J ??????????

R
A
B
C
D
E
F
H
I
G
J
11
Data Structure and Algorithm?????????????????????
???????
  • Predecessor, Successor
  • ??????????????????????/????
  • Predecessor (Ancestor) ???????????????
  • Successor (Descendant) ?????????????????
  • R,A ???? Predecessor ??? E,F,G,J
  • E,G ????? Successor
  • J ???? Successor ??? F
  • F ???? Predecessor ??? J

R
A
B
C
D
E
F
H
I
G
J
12
Data Structure and Algorithm?????????????????????
???????
  • Immediate Successor ???? Son ???? Child ??????? i
  • ???????????????????????? i ?????????????? i
    ??????????
  • Immediate Predecessor ???? Father ???? Mother
    ??????? i
  • ???????????????????????? i ?????????????????? i
    ??????????
  • E,F,G ??? Son/Child ??? A
  • J ???? Son/Child ??? F
  • F ???? Father/Mother ??? J

R
A
B
C
D
E
F
H
I
G
J
13
Data Structure and Algorithm?????????????????????
???????
  • ??? (Forest)
  • ??? ?????????????? ???????????????????????????????
    ??????????????? ?????????????????????????????????
    ??????????????????? ?????????????
  • ???? ???????????????????????????????? ?
    ?????????????????????????

A
B
C
D
E
F
H
I
K
L
G
M
J
14
Data Structure and Algorithm?????????????????????
???????
  • ????????????????? (Ordered Tree)
  • ???????????????????????????????????????? ????
    ????, ?????????, ????????
  • ???????????????????? (Unordered Tree)
  • ?????????????????????????????????????????
    ???????????????? ?????????????????

A
B
?????? A ??? B ??????????????????????????????????
??????? ????????????????????? ???????????????
15
Data Structure and Algorithm?????????????????????
???????
  • ??????????????????????????????????
  • ???????????????????? ??????????????????????
    ???????????????? ?
  • ???????????????? Son ????? ????????? 5 son ??? k
    5 ????????? 3 son
  • ??? K 3 ??????????????????????????????????????
    ??????????
  • ???????????????????? k ??????????????????????????
    ????????????????

???????
Son,1
Son,2
Son,3
Son, k
16
Data Structure and Algorithm?????????????????????
???????
  • ??????????????????????????????????
  • ?????????????????????????????????????? ???? B
    ????? 4
  • ???????????? 9 ???? (A-I) ???????????????????????
    ???????????????????
  • 5 ?????????? 9 ???? ??????????????????
    ???????????????? ? ??????????

???????
A
B
C
Son,1
Son,2
Son,3
Son,4
D
E
F
G
H
I
17
Data Structure and Algorithm?????????????????????
???????
A
  • ??????????????????????????????????

B
C
D
E
F
G
H
I
1 2 3 4 5
6 7 8 9
A B C D E F G H I
2 4 8 - 9 - - - -
3 5 - - - - - - -
- 6 - - - - - - -
- 7 - - - - - - -
INFO SON,1 SON,2 SON,3 SON,4
18
Data Structure and Algorithm?????????????????????
???????
  • ??????????????????????????????????
  • ?????????????????????????????? ???????????????????
    ???????????????????
  • ??????????????????????????? ??????????????????????
  • ???????????????????? K ?????????? n ????
    ????????????????????????
  • ??????? n ???? ??????????????? kn ????
  • ???????????????????????? n-1 ????
    (?????????????????????????????????????)
  • ????????????????????????????? kn (n 1)
  • kn n 1
  • n(k 1) 1
  • 9(4 -1) 1 28 ?????????????

19
Data Structure and Algorithm?????????????????????
???????
  • ??????????????????????????? (Binary Tree)
  • ???????????? ???????????????????????????????? 2
    ????? ??????????????????? ????????????????????????
    ?????
  • ?????????????????? ???????? left subtree
  • ????????????????? ???????? right subtree
  • ?????????????????? ??? ???????????????????????????
    ???????? ???????????????

20
Data Structure and Algorithm?????????????????????
???????
  • ???????????????????????????
  • ???????????????????? ?????????????????????????????
    ??? 2
  • ?????????????????????????????? ???????????????????
    ?? ????????????????????
  • ?????????????????????????? Father
  • ???????????????????? LSon, ???????????????????
    RSon

Father
LSon
RSon
21
Data Structure and Algorithm?????????????????????
???????
  • ???????????????????????????

Binary Tree
Non Binary Tree
22
Data Structure and Algorithm?????????????????????
???????
  • ?????????????????????????????????????
  • ???????????????????????????? ?????????????????????
  • ?????????? ??? ?????? ????????????????????????????
    ?
  • ??? n ????????????????????, l ???????????????
    (level )
  • n 2l 1
  • ???????? ???????????????????? 23 1 7 ????

23
Data Structure and Algorithm?????????????????????
???????
  • ???????????????????????????????????
  • ??????????????????? Son ????? ????????????????????
    ????????? 2
  • ??????????????????????????????????????????
  • ?. ???????????????????????????????
  • ?. ????????????????????????????????????????????
  • ??????????????
  • 1. ??????????????????????????????????????????????
    ???????
  • Leftmost son, next sibling
    (????????????????)
  • 2. ??????????????????????? 45?
    ?????????????????????????

24
Data Structure and Algorithm?????????????????????
???????
  • ??????????????????????????????????????
  • ????????????? B ???? leftmostson ??????? C ????
    next-sibling ??? B ?????? D ???? next-sibling ???
    C
  • ????????????? ????????????????????? 45?

A
A
A
?????????? 2
B
C
D
B
C
D
B
C
?????????? 1
D
25
Data Structure and Algorithm?????????????????????
???????
  • ???????????????????????????????
  • ?????? 2 ??????
  • ?. ????????????????????????
  • ?. ????????????????????????????
    ????????????????????????
  • ????????????????????????

Data
Data
????
LLink
RLink
LSon
RSon
26
Data Structure and Algorithm?????????????????????
???????
  • ????????????????????????
  • LLink ???? LSon ??????????????????????????????????
    ????????
  • RLink ???? RSon ??????????????????????????????????
    ???????
  • T ????????????????????????? ?????? T
    ???????????????????????????????
  • ??? T ?????????????????? ??? T
    ?????????????????

Data
Data
????
LLink
RLink
LSon
RSon
27
Data Structure and Algorithm?????????????????????
???????
  • ????????????????????????

T
A
A
B
X
B
X
0
0
C
D
C
D
0
0
0
E
Link ???????????????? ??????????? 0
??????????????????????????????????????????
T ??????????????????????????????????????
E
0
0
28
Data Structure and Algorithm?????????????????????
???????
  • ????????????????????????????
  • (???????????????????? Sequential Access)
  • ???????????????????????????????? 1
    ????????????????????
  • ?????????????????????????????????????????? (Full
    Binary Tree)
  • ?????????????????????????????????????????????? 1
    ????
  • ??????????????????????????????????????????????????
    ?? Full Binary Tree
  • ????????????????????????? 2l - 1

29
Data Structure and Algorithm?????????????????????
???????
  • ????????????????????????????

A
1
2
3
B
X
4
5
6
7
C
D
E
8
9
10
11
12
13
14
15
1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
A B X C D - - E - - - - - - -
30
Data Structure and Algorithm?????????????????????
???????
  • ????????????????????????????
  • ??????????????? ???(Father) ?????? ? (Lson, Rson)
  • ????????????? Father (i) ??? i 5 (??? D)
  • ?????????? (absolute value) ??? i/2 father
    ???????????????? 2
  • ?????????? Lson (i) (??? B)
  • ?????????? 2i Lson ???????????????? 4
  • ?????????? Rson (i) (??? C)
  • ?????????? 2i 1 Rson ???????????????? 5(D)

31
Tree Type
  • Binary Tree
  • ?????????????????????????
  • ?????????????????????????????????????????????????

parent
Left Son
Right Son
32
Tree Type
  • Binary Search Tree
  • ?????????????????????????? parent ???? child
    ?????????? 2
  • ???? child ??????? parent ?????????????
  • ???? child ?????? parent ????????????

33
Binary Search Tree
  • Representations
  • ??????????????????????????????????????????????????

typedef struct node int n_int node
left node right tree_node
Left Son pointer
Right Son pointer
34
BST Operation
  • Insert
  • ?????????????????? leaf ????????
  • ??????????????????????????????? root
  • ????????????????? ??????????????????????????????
    (?????????????? null)

D B F A C E G
D
B
F
G
E
A
C
35
BST Operation
  • Insert
  • ??????????????? Tree (1)

B A D C G F E
B
A
D
C
G
F
E
36
BST Operation
  • Insert
  • ??????????????? Tree (2)

A B C D E F G
A
B
C
D
E
F
G
37
BST Operation
  • Delete
  • ?? 3 ?????????????????
  • ?? leaf
  • ??????????? child ?????????
  • ??????????? child ???????

D
2
3
B
F
1
A
E
C
38
BST Operation
  • Delete Leaf Node
  • ??????????????????????????????????????????????????
    ? null
  • ?????? leaf
  • ???????? root ?????????????????? tree ????

D
NULL
B
F
A
E
C
39
BST Operation
  • Delete 1 child node
  • ????????????????????????????????????????????????
    child
  • ??????
  • ??????? root ????????????????????? root ????

D
B
F
A
E
C
40
BST Operation
  • Delete 2 child node
  • ????????????????????????????????????????????????
  • ????????????? ??????????????????

D
B
F
A
E
C
??????????????????????????????????????
?????????????
41
BST Operation
  • Delete 2 child node ??????????????????????
  • ??????????????????????????????????????????????????
    ?
  • ?????????????????????????????????
  • ??????????????????

D
A
B
F
E
A
C
42
BST Operation
  • Delete 2 child node ?????????????????????? (2)

F
C
D
H
J
B
E
A
C
43
BST Operation
  • Delete 2 child node ???????????????????
  • ????????????????????????????????????????????????
  • ??????????????????????????????
  • ???????????????

D
C
B
F
E
A
C
44
BST Operation
  • Delete 2 child node ??????????????????? (2)

F
E
D
H
B
F
G
A
C
45
BST Operation
  • Searching
  • ??????????????????????????????????????? root
  • ???????? tree ??????????? insert
    ???????????????????
  • ????? return ??? ???????? leaf ???????????????????
    ????????????? tree

root
D
search E
F
B
A
E
C
G
46
BST Operation
  • Searching
  • ??????????? search ??? tree ??????????????????????
    linear

A
B
C
D
E
F
G
search E
D
Linear 5 comp. Tree 3 comp.
F
B
A
E
C
G
47
BST Operation
  • Searching
  • ?????????????????????

Data Time Comparison Time Comparison
Data Linked List Tree
A 1 3
B 2 2
C 3 3
D 4 1
E 5 3
F 6 2
G 7 3
Total 28 17
Average 28/7 4 17/7 2.43
48
BST Traversal
  • Traverse Binary Search Tree
  • Inorder
  • Preorder
  • Postorder
  • Reverse Inorder
  • Reverse Preorder
  • Reverse Postorder

49
BST Traversal
  • Traverse Binary Search Tree
  • ????????? tree ???? 3 ????
  • R ? root
  • TL ? left subtree
  • TR ? right subtree
  • ????? traverse subtree
  • ???????????

R
TL
TR
50
BST Traversal
  • Traverse Binary Search Tree
  • Inorder ? TLRTR
  • Preorder ? RTLTR
  • Postorder ? TLTRR
  • Reverse Inorder ? TRRTL
  • Reverse Preorder ? RTRTL
  • Reverse Postorder ? TRTLR

51
BST Traversal
  • Inorder Traversal

TLRTR
TL
R
TL
TR
A B C
E F G
TR
R
D
D
A B C D E F G
52
BST Traversal
  • Preorder Traversal

RTLTR
TL
R
TL
TR
B A C
F E G
TR
R
D
D
D B A C F E G
53
BST Traversal
  • Postorder Traversal

TLTRR
TL
R
TL
TR
A C B
E G F
TR
R
D
D
A C B E G F D
54
BST Traversal
  • Reverse Inorder

TRRTL
TL
R
TL
TR
C B A
G F E
TR
R
D
D
G F E D C B A
55
BST Traversal
  • Reverse Preorder

RTRTL
TL
R
TL
TR
B C A
F G E
TR
R
D
D
D F G E B C A
56
BST Traversal
  • Reverse Postorder

TRTLR
TL
R
TL
TR
C A B
G E F
TR
R
D
D
G E F C A B D
57
?????????????
Write a Comment
User Comments (0)
About PowerShow.com