Data Structure and Algorithm - PowerPoint PPT Presentation

1 / 94
About This Presentation
Title:

Data Structure and Algorithm

Description:

Title: CS321 : Data Structure / Algorithm Analysis Subject: Tree Author: Last modified by: Information Systems – PowerPoint PPT presentation

Number of Views:155
Avg rating:3.0/5.0
Slides: 95
Provided by: 5038
Category:

less

Transcript and Presenter's Notes

Title: Data Structure and Algorithm


1
Data Structure and Algorithm
????????????????????? (Tree)
2
?????????????? ????????? Tree
3
Tree
  • ???????????????????????????????????? ? ???????
    ?????????????????????????????????????????
    ??????????? 1 ????
  • ??????????????????????????????????????????????
    ???????????????????????????????????????????
    ?????????????????????????????????????????????????
    Tree ??????????????????????? node
    ?????????????????????????? ( Hierarchical
    Relationship )

4
Tree
  • ????????? tree ????????????????????? ?
    ???????????? ????????????????????????????????
    ?? ?????????????? tree
  • 1 ????????????????????????????????????????
  • 2. ?????????????????????????????
    ?????????????????????
  • 3. ?????????????????????????????????????? tree

5
Tree
6
???????? Tree
  • 1. ?????? Math ????????????????????
    ??
  • 2. Tree ???? set ????? T ??????????? 1 Node
    ???? ??????? ?????? node ????? 1 node ?????
    Root
  • 3. Node ????? ???????????????????????????????
    ??????????????????? ????????????? T1 ,, T2 ,
    Tn ( n gt 0 ) ?????????????????? ??????????
    ????? Subtree ????? n ???????????????????????

7
???????? Tree
V0 ???? root ??? Tree ?????????????? V1
, V2 , V3 , V4 , V5 , V6 , V7
8
???????? Tree
V1 ???? root ??? Tree ?????????????? V3 ,
V4
9
???????? Tree
V2 ???? root ??? Tree ??????????????
10
???????? Tree
  • ???????????? ??????? Root node ??? Sub Tree
    ?????????? ??? ?????? (Parent and child )
  • ??? (Father) ??? node ??????? root ??? Tree ????
    root ??? Subtree
  • ??? (child ) ??? node ?????????????????? node
    ??????????

11
???????? Tree
A
D
B
C
T
E
F
H
G
12
????????????? Tree
  • 1. Brother Node Node ???????????????????
  • 2. Branch / Edge ( ?????) ??????????????????
    ????????????????
  • 3. Degree ???????? ???? ??????????????????
    node
  • 4. Leaf node (???????) ??? Node ???????????
    Degree 0
  • 5. Branch Node Node ????????? Root , Leaf
    Degree??????? 0

13
????????????? Tree
Brother Node BCD EF HT
A
Branch/Edge
Branch Node
D
B
C
T
E
F
H
G
Leaf Node
1. Brother Node Node ???????????????? 2.
Branch / Edge ??????????????????
????????????????
14
????????????? Tree
Degree ???????? ???? ?????????????????? node
A
3
D
B
C
2
1
2
T
E
F
H
G
Leaf Node
4. Leaf node (???????) ??? Node ???????????
Degree 0 5. Branch Node Node ????????? Root
, Leaf Degree??????? 0
15
????????????? Tree
5. Descendant ??????? / Successor ???????????
Descendant ??? node x ??? ??? node ??? link
??? x ???????????
A
Descendant ??? A
D
B
C
H
F
G
T
E
16
????????????? Tree
Immediate Successor / Direct Descendant/ Son
?????????????
A
Direct Descendant ???A
D
B
C
H
F
G
T
E
17
????????????? Tree
6. Ancestor ????????? / Predecessor ?????????
Ancestor ??? node x ??? ??? node ??? Link
????????? x
A
B ??? A ??? Ancestor ??? E ??? F
D
Direct Ancestor ??? E ??? F
B
C
H
F
G
T
E
18
????????????? Tree
7. Immediate Predecessor / Direct Ancestor /
Father
A
B ??? Father ??? E ??? F
D
Direct Ancestor ??? E ??? F
B
C
H
F
G
T
E
19
????????????? Tree
  • 8 . Level ??? node (?????)
  • - ???????????????????? node ??????????????
    root ??????
  • - ??????????????????????? node ????????
  • - ??? node ??????? L
  • -gt node ????????????????????????? L1
  • 9. Height ???? ?????( Level )???????????? tree

20
????????????? Tree
Height 3
Level 1 . . . . . . . . . . . . . .
A
Level 2 . . .
D
B
C
H
F
G
T
Level 3
E
21
????????????? Tree
Height 3
??????????????
Level 0 . . . . . . . . . . . . . .
A
Level 1. . .
D
B
C
H
F
G
T
Level 2
E
22
????????????? Tree
Height of a tree ...
Brother node of .
Subtree ??? node .
23
????????????? Tree
Notation Node, children, edge, parent, ancestor,
descendant, path, depth, height, level, leaf
node, subtree.
depth of a node ????? ??? edge ??? root -
node
Height of a tree 1 max(depth of a node) 4
Subtree
Siblings of U
24
????????????? Tree
9. ??? (Forest)
25
Ordered Tree
  • Ordered Tree (????????????) ????
    ?????????????????
  • Tree ??? node ?? ? ?? subtree ???? degree
    ??????? 2 ?????? ???????????????????? ??? tree
    ??????????????
  • ?????????????????????????
  • ???????????????????? ??? tree
    ????????????????? ???????? Oriented tree

26
Ordered Tree
Ordered Tree
A
A
B
C
B
C
D
D
J
E
I
F
27
Tree
  • ????????????????????
  • ???????????????????
  • Decision Trees
  • ?????????????????????????????

28
???????????????????
  • ???????????????????????? Tree ???????????????????
    ???????? ?????????
  • 1. node ???? (Branch node) ???? node
    ?????? ???????????????????
  • 2. ???? node ???? (Leaf node) ???? node
    ?????? ????????????????????????????????????? ?

29
Tree
V1 V2 (V3 V4 V5 )
30
Decision Trees
  • Tree ?????????????????????????????????????????????
    ????? ?????????????????????????????????????????
    ?????????????????????????????? ???????? node
    ??? tree ???????????????????????????????????????
    ? tree ??????????????????????????? ?????? (
    Game Tree )
  • ??????????? ?????? ???????????????

31
Decision Trees
32
?????????????????????????????
  • ???????????? Catalog ???????????
    ????????????????????????????
  • ?????????????????????? ( Dewey Decimal System
    ) ????????????????????????????
    ??????????????????????? ? ???? ???????????????????
    ???????????????????????????? ????????????????????
    ??????????? ?????????????????????????????????
    ???????????????????????????? ???????

33
????????????????????? ?????????????
34
Array Implementation
Linked Lists Implementation
35
Binary Tree (????????????)
36
Binary Tree
  • ??????? node ???? subtree ???? degree ??????????
    2
  • ????? subtree ????? ??????????????? ???? node
    ??????????? ?????? ??? Root

T NULL
37
Binary Tree
  • ????????? Math ?????????????? Binary tree
    ??????recursive
  • ???? set ???????? node ?????????? set ????
    ?????????????? node ???????? Root ??? node
    ??????????????????? subtree ??? ????? node
    ??????? ??? subtree ???????? binary tree ????
  • Left ??? Right Subtree

38
Binary Tree Example
Binary Tree
Max depth 3 Height 4
Right son
Left son
39
Complete Binary Trees
Binary Tree
???????????????????????????????? node n 2l
-1 ????? l ??? max level
15
5
16
3
12
20
1
40
Array Implementation
Binary Tree
41
Linked List Implementation
Binary Tree
DATA
Rson
Lson
42
??????? Tree ?????? Binary Tree
43
??????? Tree ?????? Binary Tree
  • ??????? tree ?????? binary tree
  • ????????? Tree ????? degree gt 2
  • ??????????????????????????? ?????????????????
  • ??? node ?????? ??? node ??????

44
??????? Tree ?????? Binary Tree
45
??????? Tree ?????? Binary Tree
  • ???????
  • 1. ????????? node ???????????????? ( node
    ???????) ???????????
  • - ?????????????????? node ??????????????????
    ? ?????????
  • ?????? ????????????????????????
  • 2. ????? son ????????????? ???????? node
  • - son ??????? ??? node ?????????? node
    ????
  • - son ?????? ??? node ??????????? ?
    (????????????? / Brother )

46
??????? Tree ?????? Binary Tree
????????
1.????????? node ???????????????? ( node ???????)
???????????
47
??????? Tree ?????? Binary Tree
A
??????? 1
D
B
C
T
G
H
F
E
1.????????? node ???????????????? ( node ???????)
???????????
48
??????? Tree ?????? Binary Tree
A
??????? 1-1
D
B
C
T
G
H
F
E
1. ?????????????????? node ??????????????? ??? ?
????????? ?????? ????????????????????????
49
??????? Tree ?????? Binary Tree
??????? 2
????? son ????????????? ???????? node - son
??????? ??? node ?????????? node ???? - son
?????? ??? node ??????????? ?
(????????????? / Brother )
50
?????????
A
?????? Tree ??????? Binary Tree
C
B
D
1
3
2
4
5
9
6
7
8
10
A
B
C
G
E
D
F
J
K
L
M
51
??????? Forest ?????? Binary Tree
  • ??????? Forest ?????? binary tree
  • ???????????????? ??????? tree ?????? binary tree
  • ??? ?????? Root ??????????? ??????????????
    Subtree ????

52
??????? Forest ?????? Binary Tree
A
E
H
B
G
C
F
J
K
D
????????
53
??????? Forest ?????? Binary Tree
A
E
B
G
C
F
H
J
K
D
Step 1
54
??????? Forest ?????? Binary Tree
Step 2
55
Binary Search Tree
56
Binary Search Tree
???????????????? node ??????????? tree
????????????????????????????? Node ?????????
Tree ?????????????? ??????????
???????????????????????? ??? ??? key
???????????? ? ?????? 1. ?????????? key
??? node ?????????????? 2.
?????????????? key ??? node ?????????????
57
Binary Search Tree
???????????????????????? ??? ???key ????????????
? ?????? 1. ?????????? key ?????????????????????
2. ?????????????? key ????????????????????
65
40
75
70
95
30
55
35
80
58
Binary Search Tree
???????????????????????? ??? ???key ????????????
? ?????? 1. ?????????? key ?????????????????????
2. ?????????????? key ????????????????????
65
40
75
70
95
30
55
35
80
59
Binary Search Tree
Ken , 45
Jim , 35
Mandy , 15
Patty , 60
Bobby , 60
Jone , 40
John , 20
??????????????????????? ?
60
Binary Search Tree
???????? Binary Search Tree - ??????????
??????? Add ?????? 1 ??? ( node ) ???????? Tree
- ?????????? Root ???? ???????????? node
???? ? ???????? ??????????????????????????????
node ????
61
Binary Search Tree
65
Root
ADD
65
65 40 75 30 55 35 70 95 80
62
???????? Binary Search Tree
40
ADD
65
40
65 40 75 30 55 35 70 95 80
63
???????? Binary Search Tree
75
ADD
65
40
75
65 40 75 30 55 35 70 95 80
64
???????? Binary Search Tree
30
ADD
65
40
75
30
65 40 75 30 55 35 70 95 80
65
???????? Binary Search Tree
55
ADD
65
40
75
30
55
65 40 75 30 55 35 70 95 80
66
???????? Binary Search Tree
35
ADD
65
40
75
30
55
35
65 40 75 30 55 35 70 95 80
67
???????? Binary Search Tree
70
ADD
65
40
75
70
30
55
35
65 40 75 30 55 35 70 95 80
68
???????? Binary Search Tree
95
ADD
65
40
75
70
30
55
95
35
65 40 75 30 55 35 70 95 80
69
???????? Binary Search Tree
80
ADD
65
40
75
70
95
30
55
35
80
70
???????? Binary Search Tree
???? ???????? Binary search Tree ????????
??????????? 65 40 75 30 55 35 70 95 80
65
40
75
70
95
30
55
35
80
71
???????? Binary Search Tree
40
ADD
1. ?????????
65
??? !!
40
75
70
95
30
55
35
80
72
???????? Binary Search Tree
40
ADD
2 . ??????????? Node ??????
65
??? !!
2
40
75
70
95
30
55
35
80
73
???????? Binary Search Tree
40
ADD
3. ??? List list ????????
65
40
40
75
??? !!
70
95
30
55
35
80
74
???????? Binary Search Tree
????????? ????? Binary Search Tree ??????
Add ?????????????? 65 40 30 70 75 55 35 95 80
75
Complete Binary Tree
76
Complete Binary Trees
Complete Binary Tree
???????????????????????????????? node n 2l
-1 ????? l ??? max level
15
5
16
3
12
20
1
77
Complete Binary Tree
65
Height 3
40
70
75
80
35
55
????? Node ??????? n n 2 l - 1 ???? l ???
Max level ??? Tree
78
Complete Binary Tree
65
Height 3
40
70
75
80
35
55
Max level 1 n 21-1 1 Max level 2
n 22-1 3 Max level 3 n 23-1 7
79
Complete Binary Tree
??? n 7 node ??? Complete Binary Tree
??????????????? ?
n 2l-1 7 2l-1 2l 71 8 23
65
40
70
75
80
35
55
n 2l-1 n1 2l log2( n1) log2 2l
log2( n1) l
Height (l) max level log2( n1)
80
???????????????Binary Tree (Binary Tree
Traversals)
81
??????????????? Binary Tree
  • ??????????????? Binary Tree ???
    ??????????????? node ????? node ?? 1 ?????
    ???????? node ?????????????
  • ??????????????????????????????????? ????
    ???????????
  • ?????????????????????????????? Binary Tree ???
    ?????? Root ( R ) , Subtree ???????? ( TL ) ,
    Subtree ??????? ( TR )
  • ?????????? 3 ???? (????????????????????????
    ????????)
  • 1. Inorder Traversal
  • ???????? TL R TR ( Root ??????????)
  • 2. Preorder Traversal
  • ???????? RTL TR ( Root ????????)
  • 3. Postorder Traversal
  • ???????? TL TR R ( Root ????????)

82
??????????????? Binary Tree
  • Binary Tree ???????????? Subtree ?????
    Subtree ?????????? Subtree ????? ???
    ??????????????????? ????? root ??? subtree ???
    ???? ?????

Root node
A
B
C
E
F
D
Right Subtree
Left Subtree
G
H
I
A
C
B
E
F
D
G
H
I
83
Inorder Traversal
  • Inorder Traversal ???????? TL R TR
    ( Root ??????????) 1. ?????? Left
    Subtree ??? Inorder 2. ?????? Root 3.
    ?????? Right Subtree ??? Inorder

Root node
A
B
C
E
F
D
Right Subtree
Left Subtree
G
H
I
84
Inorder Traversal
???????? Subtree ?????????????????? ???? Root
??? Subtree ???? ???????????? Subtree ??????
4
6
3
5
8
1
9
2
7
?????????????????????? Binary Tree ????????
Inorder ????? DGBAECHFI
85
Preorder Traversal
  • Preorder Traversal ???????? RTL TR
    ( Root ????????) 1. ?????? Root 2.
    ?????? Left Subtree ??? Preorder 3.
    ?????? Right Subtree ??? Preorder

Root node
A
B
C
E
F
D
Right Subtree
Left Subtree
G
H
I
86
Preorder Traversal
???????? Root ??? Subtree ???????? ????????????
Subtree ??????????????????? Subtree ??????
1
5
2
6
7
3
9
4
8
?????????????????????? Binary Tree ????????
Preorder ????? ABDGCEFHI
87
Preorder Traversal
  • Preorder Traversal ???????? RTL TR
    ( Root ????????) 1. ?????? Root 2.
    ?????? Left Subtree ??? Preorder 3.
    ?????? Right Subtree ??? Preorder

Root node
A
B
C
E
F
D
Right Subtree
Left Subtree
G
H
I
88
Preorder Traversal
???????? Root ??? Subtree ???????? ????????????
Subtree ??????????????????? Subtree ??????
1
5
2
6
7
3
9
4
8
?????????????????????? Binary Tree ????????
Preorder ????? ABDGCEFHI
89
Postorder Traversal
  • Postorder Traversal ???????? TL TRR
    ( Root ????????) 1. ?????? Left Subtree
    ??? Postorder 2. ?????? Right Subtree ???
    Postorder 3. ?????? Root

Root node
A
B
C
E
F
D
Right Subtree
Left Subtree
G
H
I
90
Postorder Traversal
???????? Subtree ??????????? ????????????
Subtree ?????????????????? Root ??? Subtree
????
9
8
3
4
7
2
6
1
5
?????????????????????? Binary Tree ????????
Postorder ????? GDBEHIFCA
91
Tree Traversal
  • Preorder

A
B
C
D
E
F
G
92
Tree Traversal
  • Inorder

A
B
C
D
E
F
G
93
Tree Traversal
  • Postorder

A
B
C
D
E
F
G
94
?????????
?????????????? Binary Tree ?????????????Preorder,
Inorder ??? PostOrder
1.
A
A
3.
B
D
B
C
C
D
E

2.
E
A
-
G

D
B
C
Write a Comment
User Comments (0)
About PowerShow.com