Title: Data Structure
1Data Structure
???????????????
2Data Structure
- ???????????????????????? (Tree)
- ???????????????????????
- ?????????????????
- ??????????????????????????????????
- ??????????????????????????? (Binary Tree)
- ???????????????????????????????
3Data Structure and Algorithm?????????????????????
???????
- ?????????????????????????????????
Root Node
Node
Link
?????????????????
????????????????????????
4Data Structure and Algorithm?????????????????????
???????
- ????????????????????????
- ???????????????????????? (Non Linear)
??????????????????????????? ????????????????
???????????????????????????????????? - ???????????????????????? ?????????????????????????
??????????????? ????????? ???????????????? - ?????????????????? ???????????? (Node)
?????????????????????? - ?????????????????????????? (Link)
5Data Structure and Algorithm?????????????????????
???????
- ????????????????????
- ??????????????????? ?????????? (root node), R
- ???????? ? ???????????? ?????????????????? n
????? ?????????????????????????????
???????????????????????????????
????????????????????????? (Sub tree)
6Data 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
7Data Structure and Algorithm?????????????????????
???????
- ???????????????????? ? ?????????
- ????????????????????????????? ????????????????????
???????????????? - ???? Father, Son, Grandson
- ?????????????????????? Mother, Child, Grandchild
- ???????????? (Level)
- ?????????????????????????????????????????????????
?????? - ??????????????????????????? 1 (??????????????????
???????????? 0) - ???????????????????????????????? ???? 3 1
(???? 3 0) ???????
8Data 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
9Data 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
10Data 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
11Data 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
12Data 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
13Data Structure and Algorithm?????????????????????
???????
- ??? (Forest)
- ??? ?????????????? ???????????????????????????????
??????????????? ?????????????????????????????????
??????????????????? ????????????? - ???? ???????????????????????????????? ?
?????????????????????????
A
B
C
D
E
F
H
I
K
L
G
M
J
14Data Structure and Algorithm?????????????????????
???????
- ????????????????? (Ordered Tree)
- ???????????????????????????????????????? ????
????, ?????????, ???????? - ???????????????????? (Unordered Tree)
- ?????????????????????????????????????????
???????????????? ?????????????????
A
B
?????? A ??? B ??????????????????????????????????
??????? ????????????????????? ???????????????
15Data Structure and Algorithm?????????????????????
???????
- ??????????????????????????????????
- ???????????????????? ??????????????????????
???????????????? ? - ???????????????? Son ????? ????????? 5 son ??? k
5 ????????? 3 son - ??? K 3 ??????????????????????????????????????
?????????? - ???????????????????? k ??????????????????????????
????????????????
???????
Son,1
Son,2
Son,3
Son, k
16Data 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
17Data 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
18Data Structure and Algorithm?????????????????????
???????
- ??????????????????????????????????
- ?????????????????????????????? ???????????????????
??????????????????? - ??????????????????????????? ??????????????????????
- ???????????????????? K ?????????? n ????
???????????????????????? - ??????? n ???? ??????????????? kn ????
- ???????????????????????? n-1 ????
(?????????????????????????????????????) - ????????????????????????????? kn (n 1)
- kn n 1
- n(k 1) 1
- 9(4 -1) 1 28 ?????????????
19Data Structure and Algorithm?????????????????????
???????
- ??????????????????????????? (Binary Tree)
- ???????????? ???????????????????????????????? 2
????? ??????????????????? ????????????????????????
????? - ?????????????????? ???????? left subtree
- ????????????????? ???????? right subtree
- ?????????????????? ??? ???????????????????????????
???????? ???????????????
20Data Structure and Algorithm?????????????????????
???????
- ???????????????????????????
- ???????????????????? ?????????????????????????????
??? 2 - ?????????????????????????????? ???????????????????
?? ???????????????????? - ?????????????????????????? Father
- ???????????????????? LSon, ???????????????????
RSon
Father
LSon
RSon
21Data Structure and Algorithm?????????????????????
???????
- ???????????????????????????
Binary Tree
Non Binary Tree
22Data Structure and Algorithm?????????????????????
???????
- ?????????????????????????????????????
- ???????????????????????????? ?????????????????????
- ?????????? ??? ?????? ????????????????????????????
? - ??? n ????????????????????, l ???????????????
(level ) - n 2l 1
- ???????? ???????????????????? 23 1 7 ????
23Data Structure and Algorithm?????????????????????
???????
- ???????????????????????????????????
- ??????????????????? Son ????? ????????????????????
????????? 2 - ??????????????????????????????????????????
- ?. ???????????????????????????????
- ?. ????????????????????????????????????????????
- ??????????????
- 1. ??????????????????????????????????????????????
??????? - Leftmost son, next sibling
(????????????????) - 2. ??????????????????????? 45?
?????????????????????????
24Data 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
25Data Structure and Algorithm?????????????????????
???????
- ???????????????????????????????
- ?????? 2 ??????
- ?. ????????????????????????
- ?. ????????????????????????????
???????????????????????? - ????????????????????????
Data
Data
????
LLink
RLink
LSon
RSon
26Data Structure and Algorithm?????????????????????
???????
- ????????????????????????
- LLink ???? LSon ??????????????????????????????????
???????? - RLink ???? RSon ??????????????????????????????????
??????? - T ????????????????????????? ?????? T
??????????????????????????????? - ??? T ?????????????????? ??? T
?????????????????
Data
Data
????
LLink
RLink
LSon
RSon
27Data 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
28Data Structure and Algorithm?????????????????????
???????
- ????????????????????????????
- (???????????????????? Sequential Access)
- ???????????????????????????????? 1
???????????????????? - ?????????????????????????????????????????? (Full
Binary Tree) - ?????????????????????????????????????????????? 1
???? - ??????????????????????????????????????????????????
?? Full Binary Tree - ????????????????????????? 2l - 1
29Data 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 - - - - - - -
30Data 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)
31Tree Type
- Binary Tree
- ?????????????????????????
- ?????????????????????????????????????????????????
parent
Left Son
Right Son
32Tree Type
- Binary Search Tree
- ?????????????????????????? parent ???? child
?????????? 2 - ???? child ??????? parent ?????????????
- ???? child ?????? parent ????????????
33Binary Search Tree
- Representations
- ??????????????????????????????????????????????????
typedef struct node int n_int node
left node right tree_node
Left Son pointer
Right Son pointer
34BST Operation
- Insert
- ?????????????????? leaf ????????
- ??????????????????????????????? root
- ????????????????? ??????????????????????????????
(?????????????? null)
D B F A C E G
D
B
F
G
E
A
C
35BST Operation
- Insert
- ??????????????? Tree (1)
B A D C G F E
B
A
D
C
G
F
E
36BST Operation
- Insert
- ??????????????? Tree (2)
A B C D E F G
A
B
C
D
E
F
G
37BST Operation
- Delete
- ?? 3 ?????????????????
- ?? leaf
- ??????????? child ?????????
- ??????????? child ???????
D
2
3
B
F
1
A
E
C
38BST Operation
- Delete Leaf Node
- ??????????????????????????????????????????????????
? null - ?????? leaf
- ???????? root ?????????????????? tree ????
D
NULL
B
F
A
E
C
39BST Operation
- Delete 1 child node
- ????????????????????????????????????????????????
child - ??????
- ??????? root ????????????????????? root ????
D
B
F
A
E
C
40BST Operation
- Delete 2 child node
- ????????????????????????????????????????????????
- ????????????? ??????????????????
D
B
F
A
E
C
??????????????????????????????????????
?????????????
41BST Operation
- Delete 2 child node ??????????????????????
- ??????????????????????????????????????????????????
? - ?????????????????????????????????
- ??????????????????
D
A
B
F
E
A
C
42BST Operation
- Delete 2 child node ?????????????????????? (2)
F
C
D
H
J
B
E
A
C
43BST Operation
- Delete 2 child node ???????????????????
- ????????????????????????????????????????????????
- ??????????????????????????????
- ???????????????
D
C
B
F
E
A
C
44BST Operation
- Delete 2 child node ??????????????????? (2)
F
E
D
H
B
F
G
A
C
45BST Operation
- Searching
- ??????????????????????????????????????? root
- ???????? tree ??????????? insert
??????????????????? - ????? return ??? ???????? leaf ???????????????????
????????????? tree
root
D
search E
F
B
A
E
C
G
46BST 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
47BST 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
48BST Traversal
- Traverse Binary Search Tree
- Inorder
- Preorder
- Postorder
- Reverse Inorder
- Reverse Preorder
- Reverse Postorder
49BST Traversal
- Traverse Binary Search Tree
- ????????? tree ???? 3 ????
- R ? root
- TL ? left subtree
- TR ? right subtree
- ????? traverse subtree
- ???????????
R
TL
TR
50BST Traversal
- Traverse Binary Search Tree
- Inorder ? TLRTR
- Preorder ? RTLTR
- Postorder ? TLTRR
- Reverse Inorder ? TRRTL
- Reverse Preorder ? RTRTL
- Reverse Postorder ? TRTLR
51BST Traversal
TLRTR
TL
R
TL
TR
A B C
E F G
TR
R
D
D
A B C D E F G
52BST Traversal
RTLTR
TL
R
TL
TR
B A C
F E G
TR
R
D
D
D B A C F E G
53BST Traversal
TLTRR
TL
R
TL
TR
A C B
E G F
TR
R
D
D
A C B E G F D
54BST Traversal
TRRTL
TL
R
TL
TR
C B A
G F E
TR
R
D
D
G F E D C B A
55BST Traversal
RTRTL
TL
R
TL
TR
B C A
F G E
TR
R
D
D
D F G E B C A
56BST Traversal
TRTLR
TL
R
TL
TR
C A B
G E F
TR
R
D
D
G E F C A B D
57?????????????