Title: ???????????????????????? (Tree)
1????????????????????????(Tree)
2Tree
- ??????? ??????????????????????????????????????????
?????????? ???????????????????????????????????????
???? ?????????????????????????????????????? Root
Node ??????????????????????????????
????????????????????????????????????????
???????????? Subtree
3???????????????????????
- ???????????????????????? ?????????????????
- 1. ????????????????? ?????????? (Root node) ,
R - 2. ??????????????????????????????? n ?????
????????????????????????? ?????????? ???? ?????
T1 , T2 ,..Tn (n gt0) ???????????????????????????
???? ???????????????????? (Subtree)
4????????????
?????? R ???????????????????? A,B,C,D A
???????????????????? E,F,G F ????????????????????
J B ???????????????????? H ??? I
5??????????????????????
6???????????? (Level)
- ????????????????? ? ??????????????????????????????
?????????????????????????????????????
??????????????????????????????????????????? 1
?????????????????????????????? ????????? 1 ?????
????????????????? ? ??????????????????????????????
????????????????? F ?????????????? 4 ???????
7???????????? (Level Degree)
- ???????????? ??? ??????????????????????????
?????? ???? X ??????? 1 ???? A ??????? 2 ????????
H ??????? 3 ???? B ??????? 1 ??????? E ??????? 0
???????
8????????????? (Leaf Node)
- ????????????? ????????????????????????? 0
???????? C, D, E, J, F ??? G ?????????????????????
??????? 0 ???????? ????????? ???? interior node
???? branch node
9Immediate Successor ???? SON ??????? i
- Immediate Successor ????????????????????????????
i ???????????????????????? i ????????? ???? SON
??????? H ??????? E, I, ??? F
10Immediate Predecessor ???? father ??????? i
- Immediate Predecessor ????????????????????????????
??? i ????????? ???? FATHER ??????? J ??????? I,
FATHER ??????? I ??????? H ???????
11??????????????? (Tree Structure)
R
Root Nodes
Nodes X ???? Immediate Predecessor ??? Y ???? X
???? Father (Parent) ??? Y
B
A
X
D
C
H
Y
Nodes Y ???? Immediate Successor ??? X ???? Y
???? son (Child) ??? X
I
E
G
Leaf Nodes
12??????????????? (Tree Structure)
Level ????????????????????????????????????????????
?????? Root Node ????????????????????????????????
??????????? 1 ?????
13??????????????? (Tree Structure)
Degree ??????????????? Subtree ??????????? ???? A
?? Degree 2, X ?? Degree 1
14??????????????? (Tree Structure)
Leaf Node ???????????????? Degree 0 ???? C, D,
E, I ,G ???????? ????? Degree ltgt 0 ????????
Branch Node ???? Interior Node
15??????????????? (Tree Structure)
Nodes X ???? Immediate Predecessor ??? Y ???? X
???? Father (Parent) ??? Y
Nodes Y ???? Immediate Successor ??? X ???? Y
???? son (Child) ??? X
Predecessor ??????? ??? Node ??????????????? Succe
ssor ??????? ??? Node ???????????? ???? R, B,
H ???? Predecessor ??? E, I, I ????
Successor ??? H
16??????????????? (Binary Tree)
- ???????????????? rooted binary tree ????????????
???????????????????????????????????? 2
???????????????????????? ???????????????????????
(?????????????????????????????)
???????????????????? (left subtree)
??????????????????? (right subtree)
????????????? ????????? ??????????????????
??????????????????????????????????????????????????
???
17A
C
S
V
B
C
G
F
D
D
E
F
G
(?)
X
(?)
(?)
??? (?), (?) ??? (?) ????????????????
18(?)
(?)
(?)
??? (?), (?) ??? (?)??????????????????
19?????????????????????? (Complete Binary Tree)
- ?????????????????????? ??????????????????????????
?????????????????????????????? (??????????????????
????? left son ??? right son ) ????????? (leaf
nodes) ?????????????????????? n ??? (?)
??????????????????????????????? 3 ?????
D
R
K
M
W
O
U
(?)
20- ????????????????????????????????????????????? n
?????????????????????? 2n-1
W
V
D
N
S
A
Q
?????? ???????????????? 23-1 7 ????
21???????????????????????????????
- ???????????????????????? 2 ??? ???
- 1. ????????????????????????
- 2. ???????????????????????????? ?????????????
??????????? - (sequential)
22????????????????????????
- ?????????????????????????????????????????????
DATA
DATA
????
LLINK RLINK
LSON RSON
???????????????????????????????
LLINK ???? LSON ??????????????????????????????????
????? ???? RLINK ???? RSON ??????????????????????
????????????????
23???????????????????????????? ????????????????????
????
- ?????????????????????????????????? 1
????????????????? ???????????????????????????????
????????????? complete binary tree ??????
?????????????????????????????????????????????
???????????? 1 ????? 2 ...????????? ?????????? k
?????????????????????????????????????????
?????????????????????? 1 ????
??????????????????????????????????????????????????
???????????? ???????????????????????????????
241
A
3
2
B
X
4
6
7
5
C
D
13
11
8
14
15
9
10
12
E
??????????????????????????????????????????????????
?
2
4
5
7
8
9
10
11
12
13
14
15
1
3
6
B
X
C
D
-
-
E
-
-
-
A
-
-
-
-
????????????????????????????? ????????????????
??????????? x ??????????????????????? 2x
?????????????????????????? 2x1
25?????????? Tree ??????? Binary Tree
?????????????????(ordinary) ?????????????????????
???????????????????????????????
????????????????????? binary tree
?????????????????????????????????????????????
??????????????? 1. ?????????????????????????????
???????????????? 2. ?????????????????????????????
???? 1 ???????????????????????????????????????????
?????????? 3. ????????? 1 ??? 2
???????????? 4. ??????????????????? ?????? 45
????
26?????????? Tree ??????? Binary Tree
??????? 1
??????? 4
??????? 2
??????? 3
27?????????? Tree ??????? Binary Tree
A
???????? ????????????????????????????????????
?????
B
F
A
B
F
C
K
D
G
J
C
K
D
G
J
E
H
L
I
E
H
A
L
I
B
F
??????? 1-3
C
G
D
J
K
???????
E
L
H
I
28????????????? (Tree Traversal)
Tree Traversal ???????????????????????????????????
??????????????????????????????? ????
????????? ??????????? 3 ???? (??????????) 1.
Pre-Order Traversal (RTLTR) 2. In-Order
Traversal (TLRTR) 3. Post-Order Traversal
(TLTRR)
29??????????? Binary Tree
??????????????? ??????? ????????????????????????
??????????????????????????? ?????????????????
??????????????? ??????????????????? 3 ???? 1.
Inorder traversal ???? Symmetric order
?????????????????????????????????????????????????
??????????????????????????????????????????????????
??????????? (Left/ Root/Right) 2. Preorder
traversal ????????????????????????
??????????????????????????????????????????
????????????????????????????????????????????
(Root/ Left/ Right) 3. Postorder traversal ????
Endorder ???????????????????????????????????????
?????????? ???????????????????????????????????
?????????? ???????????????? (Left/ Right/ Root)
30??????????? Binary Tree
A
1. ????????????? (Left/ Root/Right)
??????????? BAC 2. ?????????????(Root/ Left/
Right) ??????????? ABC 3. ?????????????(Left/
Right/ Root) ??????????? BCA 1.
????????????? (Left/ Root/Right) ???????????
DB A EG C HFI 2. ?????????????(Root/ Left/
Right) ??????????? A BD CEG FHI 3.
?????????????(Left/ Right/ Root) ???????????
DB GE HIF C A
B
C
A
B
C
D
E
F
G
H
I
31??????????????????? (Binary Tree Traversal)
start
stop
??? Pre-Order Traversal
Y
Result
R
A
C
D
X
Y
G
32??????????????????? (Binary Tree Traversal)
start
stop
??? In-Order Traversal
Y
Result
C
A
D
R
G
Y
X
33??????????????????? (Binary Tree Traversal)
start
stop
??? Post-Order Traversal
Y
Result
C
D
A
G
Y
X
R
34A
B
C
D
E
F
G
H
I
Pre-order ABDGCEHIF In-order
DGBAHEICF Post- order GDBHIEFCA
35EX1
-
A
D
B
C
???????????????????? ??? 1.Pre-order
2.In-order 3.Post-order
36Expression Tree
?????????????????????? ????????? operands,
??????????????????????, ????????????? operators
?????????? Expression tree ??? (a b c)
((d e f ) g)
37Expression Tree
????????????????????????????????(operand)
??????????????(operators) ??????????????????????
??????????????????????????(leave node)
????????????????????????????????
?????????????????????????????????????????(unary
operator) ????????????????????????????????
??????????? ??????????????????????????????????????
??????????????????????????????????????????
?????? - log() cos()
?????????????????????? ???????????????????????????
????? ?????? ??????????????????
??????????????? ???????? ????????????
??????????????????????? ????????? X Y Z
Z
X
Y
38Expression Tree
- ???????? ??????????????????????????????????? (X
((Y / R) D)) - ????????
- ????????????? ?????????????????? X-Y/RD
???????????????????????? - ????????????? ?????????????????? -X/YRD
???????????????????????? - ????????????? ????????????? ????? XYR/D-
????????????????????????
-
X
/
D
Y
R
39???????? Expression Tree
- ?????????????????????????????? ???????????????????
operand ??????????????? tree ????????????? push
??????? stack ??????????????????? operator ???
pop ??? stack 2 ????? ????????? trees T1 ??? T2
(T1 ?????????) ???????????????????? tree
???????????? (root) ??????? operator ????? left
??? right children ???? T2 ??? T1 ????????
????????????? tree ????????????? stack
401
2
3
414
5
426
7
43?????????
- 1. ??????????????????????? ???????????????????????
???????????????? ??? Preorder ,Inorder ,
Postorder
A
H
B
J
F
G
I
D
K
M
2. ??????? Expression Tree ?????????????????
2.1 (A - 2 (B C) D E) F 2.2 A
(B C) D ( E F )
44?????????
- 3. ?????????????????????????
A
H
B
J
C
F
G
I
D
E
K
M