Title: Trees
1Trees
2Introduction to Trees
- ?????? (tree) ???????????????????????????????
????????????????????????????????
?????????????????? ????????????????????? ??????? - ??????(tree) ??????????????????????????????(connec
ted undirected graph) ???????????? - Theorem ?????????????????????????????????????????
????????(???)??? - ???(forest)???????????????????????????(???)???????
- ????????(leaf) ??????????????? ??????????????
pendant ?????????????(isolated)
????????(internal)???????????????????????
(?????????????????????? ___ )
2
3???????????????????????????
4Forest
5Basic Definition
- ?????????? ?????????????????
- ?????????????????????
- ?????????????????(bond) ????????????
6Rooted Trees
- ??????????????(rooted tree) ???
?????????????????????????????????????????(root)???
?????????????????????????????? ?????????????????
????????????????????????? ????????????????????????
?????????????(unique) ????????????????????? ?
??????????????? - ??????????????????????????????? n ????
?????????????????????????????????? n
????????????????
????????????????? ?????????????????????
root
root
?????????????????????? ??????????????????????????
7Rooted Trees
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)
- ????????????????? ??????????????????????????????
?? ??????????????????????????
10Rooted-Tree Terminology Example
11Rooted-Tree Terminology Example
ancestors of h and i
12Rooted-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
13m-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
15Some Tree Theorems
- ??????? 1 ?????????????????? n ???
?????????????(????) e n-1 ????
16Some Tree Theorems
- ??????? 2 ?????? m ??????????(full m-ary tree)
?????????????????? i ??? ???????????????????
nmi1 ??? ?????????? ?(m-1)i1 ???
17Some 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 ??
20Some More Tree Theorems
- ????? ?????(level) ??????????????????????????????
???????????(root)????????????
level 2
21Some 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
22Some 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??
23Applications 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
24Applications 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
25Why 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
26Representing 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
27Huffman code
- ??????????????????????????????????????????????????
???????????prefix code - ?????????????????????? Huffman code
?????????????????????????? - 1. ???????????????????????????????????????????????
????????????????????????? - 2. ??????????????????? 2 ????????????????
?????????????????????????????????? - 3. ??????????????????????????
- 4. ????????????2 ??? 3 ???????????????????????????
????????????????????????????? - 5. ???????????? ????????? 0 ??????????????????????
??????? 1 ????????????????(???????????????????????
????????) ??????????? Huffman tree
28Applications 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
29Huffmans Algorithm
30Huffmans Algorithm
31Huffmans Algorithm
32Huffmans Algorithm
33Huffmans Algorithm
34Huffmans Algorithm
35Huffmans Algorithm
36Huffmans Algorithm
37Huffmans Algorithm
38Tree 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)
39Tree 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
40Preorder Traversal
- Begins by visiting r.
- Next traverses T1 in preorder, then T2 in
preorder, , then Tn in preorder. - Ends after Tn has been traversed.
41Preorder Traversal
- Visit root, visit subtrees left to right.
42Preorder Traversal
43Inorder 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.
44Inorder Traversal
- Visit leftmost subtree,visit root, visit other
subtrees left to right.
45Inorder Traversal
46Postorder Traversal
- Begins by traversing T1 in postorder
- Next traverses T2 in postorder,then T3 in
postorder,...,then - Tn in postorder
- End by visiting r
47Postorder Traversal
- Visit subtree left to right,visit root.
48Postorder Traversal
49Postorder Traversal
- Applications compute space used by files in a
directory and its subdirectories.
50Tree 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.
51Representing 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
52Example
53Infix Notation
- Traverse in inorder adding parentheses for each
operation
x
y
2
x
3
y
2
?
/
54Prefix Notation(Polish Notation)
x
y
2
x
3
y
2
?
/
55Evaluating 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
56Example
/ 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
57Postfix Notation(Reverse Polish)
?
/
2
y
y
x
x
3
2
x
y
2
x
3
y
2
?
/
58Evaluating 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
59Example
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
60Tree 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