Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Trees

Description:

Representing Arithmetic Expressions Complicated arithmetic expressions can be represented by an ordered rooted tree Internal vertices represent operators Leaves ... – PowerPoint PPT presentation

Number of Views:243
Avg rating:3.0/5.0
Slides: 61
Provided by: Micha953
Category:

less

Transcript and Presenter's Notes

Title: Trees


1
Trees
2
Introduction to Trees
  • ?????? (tree) ???????????????????????????????
    ????????????????????????????????
    ?????????????????? ????????????????????? ???????
  • ??????(tree) ??????????????????????????????(connec
    ted undirected graph) ????????????
  • Theorem ?????????????????????????????????????????
    ????????(???)???
  • ???(forest)???????????????????????????(???)???????
  • ????????(leaf) ??????????????? ??????????????
    pendant ?????????????(isolated)
    ????????(internal)???????????????????????
    (?????????????????????? ___ )

2
3
???????????????????????????
4
Forest
5
Basic Definition
  • ?????????? ?????????????????
  • ?????????????????????
  • ?????????????????(bond) ????????????

6
Rooted Trees
  • ??????????????(rooted tree) ???
    ?????????????????????????????????????????(root)???
    ?????????????????????????????? ?????????????????
    ????????????????????????? ????????????????????????
    ?????????????(unique) ????????????????????? ?
    ???????????????
  • ??????????????????????????????? n ????
    ?????????????????????????????????? n
    ????????????????

????????????????? ?????????????????????
root
root
?????????????????????? ??????????????????????????
7
Rooted Trees
  • Example

8
?????????????????????????????
  • ???/??? (Parent)
  • ??? T ?????????????????? ??? v ????????? T
    ???????????? ???????/????????? v ??? ??? u
    ???????????????????? u ????????? v ??? u
    ??? v ????????? ??? u ?????????????????? v
  • ??? (Child)
  • ????? u ???????/?????? v ???????????? v
    ????????? u
  • ?????????? (Siblings)
  • ??? 2 ?????????????? 2 ??? ????????/??????????
    ????????????????

9
?????????????????????????????
  • ????????? (Ancestor)
  • ????????????????? ? ?????????????????????
    ???????????????????????????????????????????
  • ??????? (Descendant)
  • ????????????? v ??? ?????? ? ???????? v
    ?????????????
  • ?? (Leaf)
  • ????? ? ??????????? ??????????
  • ???????? (Internal vertex/nodes)
  • ????????????????? ??????????????????????????????
    ?? ??????????????????????????

10
Rooted-Tree Terminology Example
11
Rooted-Tree Terminology Example
ancestors of h and i
12
Rooted-Tree Terminology Exercise
c
  • ??????? parent,children, siblings,ancestors,
    ??? descendants ??????? f

o
n
h
r
g
k, l
d
a, c
m
b
root
l, k, q
a
c
g
e
q
i
f
l
j
k
p
13
m-ary trees
  • ?????????????? ?????????? m ???(m-ary tree)
    ???????????????(children)?????????? m ??? ???
    ?????????????? m ??????????(full m-ary tree)
    ??????????????????????????? m ???
  • ?????? 2-ary(???????????? 2 ???)
    ??????????????????????(binary tree)
  • ????? ?????????????????? binary tree
    ?????????????????(????????????) ??????? ___,
    ???????????????? ___

3
2
14
?????????????????? m ???????????
Full 3ary tree
Full 2ary tree
Full 5ary tree
Not full 3ary tree
15
Some Tree Theorems
  • ??????? 1 ?????????????????? n ???
    ?????????????(????) e n-1 ????

16
Some Tree Theorems
  • ??????? 2 ?????? m ??????????(full m-ary tree)
    ?????????????????? i ??? ???????????????????
    nmi1 ??? ?????????? ?(m-1)i1 ???

17
Some Tree Theorems
  • ??????? 3 ????????? m ?????????????????? n
    ????????????????????? i ?????????????????? ?
    ??? ????????
  • i (n-1)/m ??? ? (m-1)n1/m
  • n mi 1 ??? ??? ? (m-1)i 1
  • n (m? - 1)/(m-1) ??? i (? -1)/(m-1)

18
????????
  • ??????????????????????????????????????????????????
    ????????????????? ? ??????????????????????????????
    ?????????????????????????????? ? ??? 4 ??
    ??????????????????????????????????????????????????
    ??????????????????????? ?????????????????????????
    ??????????????????? ?????????????????????????????
    ???????????? ????????? 100 ?????????????????????
    ?????????????????????

19
????????
  • ?????????????????????????????????????????? 4 ???
    ?????????????? ??????????????????????????????????
    ??????????????????????????? ??? i
    ???????????????? ??? n ?????????????????
    ???????? m 4 ???????? ????????
  • n (m? -1)/m-1
  • (4 ? 100)-1/(4-1) 133
  • ??????????????????????????????? 133 ??
  • ??? i (? -1)/(m-1)
  • (100-1)/(4-1) 33
  • ?????????????????????????????????????? 33 ??

20
Some More Tree Theorems
  • ????? ?????(level) ??????????????????????????????
    ???????????(root)????????????

level 2
21
Some More Tree Theorems
  • ???????(height) ????????? ????????????????????????
    ??

??????????? (??? a) 0 ??????????? b, j,
k 1 ??????????? c, e, f, l
2 ??????????? d, g, i, m, n 3 ???????????
h 4 ??????? ???????????????? T 4
22
Some More Tree Theorems
  • ?????? m ???(m-ary tree) ???????? ???????????? h
    ???????????????????(balanced) ????????????????????
    ?????? h ???? h-1
  • ??????? ??????????????????????? mh ?? ????????
    m ???(m-ary tree) ???????????? h
  • ??????????? ?????? m-ary ?????????? ?
    ??????????? h?logm?? ?????????????????????(
    full m-ary) ??????????????? h?logm??

23
Applications of Trees Binary Search Tree Format
  • ???????????????????????????????????????
    ????????????????????????????????????????? x ???,
  • ??????????????????????????????????(left
    subtree)??? x ??????????????? x
  • ?????????????????????????????????(right
    subtree)??? x ?????????????? x

????????
7
3
12
1
5
9
15
0
2
8
11
24
Applications of Trees Codes
  • Codes That Can Be Decoded
  • Fixed-length codes
  • Every character is encoded using the same number
    of bits.
  • To determine the boundaries between characters,
    we form groups of w bits, where w is the length
    of a character.
  • Examples
  • ASCII
  • Prefix codes
  • No character is the prefix of another character.
  • Examples
  • Huffman codes

25
Why Prefix Codes ?
  • ?????????????????????????????? a prefix codea
    01 m 10 n 111 o 0 r 11 s
    1 t 0011
  • ???????????????????????? You are a
    star.?????????????????????????????? star ???
    1 0011 01 11
  • ??????????????????????????????????????????(decodes
    )????????????????????????? ????????????????????
    100110111 10 0 11 0 111 moron

26
Representing a Prefix-Code Dictionary
  • Our example space 000 A 0010 E
    0011 s 010 c 0110 g 0111 h 1000
    i 1001 l 1010 t 1011 e 110 n
    111

1
0
0
0
1
1
0
0
0
0
1
1
1
1
s
n
e
spc
1
0
0
0
0
1
1
1
g
t
c
h
i
A
E
l
27
Huffman code
  • ??????????????????????????????????????????????????
    ???????????prefix code
  • ?????????????????????? Huffman code
    ??????????????????????????
  • 1. ???????????????????????????????????????????????
    ?????????????????????????
  • 2. ??????????????????? 2 ????????????????
    ??????????????????????????????????
  • 3. ??????????????????????????
  • 4. ????????????2 ??? 3 ???????????????????????????
    ?????????????????????????????
  • 5. ???????????? ????????? 0 ??????????????????????
    ??????? 1 ????????????????(???????????????????????
    ????????) ??????????? Huffman tree

28
Applications of Trees
  • Q Use Huffman coding to encode the following
    five symbols with given frequencies
  • A 45 , B 13 ,C 12 , D 16 , E 9, F 5

29
Huffmans Algorithm
30
Huffmans Algorithm
31
Huffmans Algorithm
32
Huffmans Algorithm
33
Huffmans Algorithm
34
Huffmans Algorithm
35
Huffmans Algorithm
36
Huffmans Algorithm
37
Huffmans Algorithm
38
Tree Traversal
  • Is a procedure that systematically visits every
    vertex of an ordered rooted tree.
  • Visiting a vertex processing the data at the
    vertex.
  • ?????????????????????????????(Traversal
    algorithms)
  • Pre-order traversal NLR (node,left,right)
  • In-order traversal LNR (left,node,right)
  • Post-order traversal LRN (left,right, node)

39
Tree Traversal
  • Let T be an ordered rooted tree with root r.
    Suppose that T1, T2, , Tn are the subtrees at r
    from left to right in T.

r
55
T2
T1
24
94
49
60
3
88
46
58
52
91
76
45
40
Preorder Traversal
  • Begins by visiting r.
  • Next traverses T1 in preorder, then T2 in
    preorder, , then Tn in preorder.
  • Ends after Tn has been traversed.

41
Preorder Traversal
  • Visit root, visit subtrees left to right.

42
Preorder Traversal
43
Inorder Traversal
  • Begins by traversing T1 in inorder
  • Next visit r, then traverses T2 in inorder, ,
    then Tn in inorder.
  • Ends after Tn has been traversed.

44
Inorder Traversal
  • Visit leftmost subtree,visit root, visit other
    subtrees left to right.

45
Inorder Traversal
46
Postorder Traversal
  • Begins by traversing T1 in postorder
  • Next traverses T2 in postorder,then T3 in
    postorder,...,then
  • Tn in postorder
  • End by visiting r

47
Postorder Traversal
  • Visit subtree left to right,visit root.

48
Postorder Traversal
49
Postorder Traversal
  • Applications compute space used by files in a
    directory and its subdirectories.

50
Tree Traversal Shortcut
  • Preorder list is obtained by
  • listing each vertex the first time this curve
    passes it.
  • Result a, b, d, h, e, i, j, c, f, g, k.
  • Inorderlist is obtained by
  • listing a leaf the first time the curve passes it
    and
  • listing each internal vertex the second time the
    curve passes it.
  • Result h, d, b, i, e, j, a, f, c, k, g.
  • Postorderlist is obtained by
  • listing a vertex the last time it is passes on
    the way back up to its parent.
  • Result h, d, i, j, e, b, f, k, g, c, a.

51
Representing Arithmetic Expressions
  • Complicated arithmetic expressions can be
    represented by an ordered rooted tree
  • Internal vertices represent operators
  • Leaves represent operands
  • Build the tree bottom-up
  • Construct smaller subtrees
  • Incorporate the smaller subtrees as part of
    larger subtrees

52
Example
  • (xy)2 (x-3)/(y2)

53
Infix Notation
  • Traverse in inorder adding parentheses for each
    operation

x
y
2


x

3
y

2
?
/
54
Prefix Notation(Polish Notation)
  • Traverse in preorder

x
y
2


x

3
y

2
?
/
55
Evaluating Prefix Notation
  • In an prefix expression, a binary operator
    precedes its two operands
  • The expression is evaluated right-left
  • Look for the first operator from the right
  • Evaluate the operator with the two operands
    immediately to its right

56
Example
/ 2 2 2 / 3 2 1 0
/ 2 2 2 / 3 2 1
/ 2 2 2 / 1 1
/ 2 2 2 1
/ 4 2 1
2 1
3
57
Postfix Notation(Reverse Polish)
  • Traverse in postorder


?
/



2
y
y
x
x
3
2
x
y
2


x

3
y

2
?
/
58
Evaluating Postfix Notation
  • In an postfix expression, a binary operator
    follows its two operands
  • The expression is evaluated left-right
  • Look for the first operator from the left
  • Evaluate the operator with the two operands
    immediately to its left

59
Example
2 2 2 / 3 2 1 0 /
4 2 / 3 2 1 0 /
2 3 2 1 0 /
2 1 1 0 /
2 1 1 /
2 1
3
60
Tree Traversal
-
prefix expression
5

-
(
(
(
3
4
)
2
8


(
(
)
)
)
7

)

7
infix expression

5

(
3
4
)
2
7

5

-
(
8

(
)
)
)
)
(
(

2
postfix expression

8

3
4
2
7

5

-
8

4
3
Write a Comment
User Comments (0)
About PowerShow.com