Ch 9 Binary Trees

Download Report

Transcript Ch 9 Binary Trees

Chapter 9
Binary Trees
1
Chap.9 Contents
9.1 Definition of Binary Tree
9.2 Properties of Binary Trees
9.3 The Binary Tree Theorem
9.4 External Path Length
9.5 Traversals of a Binary Tree
2
Here are some binary trees that
we will study in the next few chapters:
Binary Tree
Binary
Search
Tree
AVL
Tree
Expression Heap
Tree
Huffman
Tree
Red-Black
Tree
3
9.1 Definition of Binary Tree
binary tree t
可為 empty tree 或 包含:
一個 root element,
與兩個不同的 binary trees,
叫 t 的 left sub-tree 及
right sub-tree
4
圖形
三角形
矩形
正方形
平行四邊形
菱形
5
因為 Java 沒有 BinaryTree 這 class
所以用 functional 符號
two sub-trees 要寫成:
leftTree (t) and rightTree (t).
如用 object 符號 則要寫成:
t.leftTree( ) and t.rightTree( )
註:
functional 符號 為 object 符號出現前 軟體設計使用的方
法
6
Why is there no BinaryTree class in Java
collections framework?
ANS:
對於 Java collections framework 的
TreeMap and TreeSet 來說
BinaryTree class 不夠 flexible(彈性)
7
除 root 及 tree, 植物學術語有 :
Leaf:
沒有 left 與 right sub-trees
Branch:是連接此 element
與它的 left 或 right sub-tree 的一條線.
8
+
–
X
/
Y
Z
*
A
B
This is an expression tree:
Each leaf is an operand, and
each non-leaf is a binary operator.
9
50
30
12
90
40
100
binary search tree 是
94
每個 left subtree 的 elements
值都小於其 root element,
每個 right subtree 的 elements
值都大於其 root element, 且其
left and right sub-trees 本身也是 binary search trees.
10
Binary trees
(elements 大小無規定)
1
2
2
3
1
3
11
Binary search tree
(elements 左小右大)
2
1
3
12
Tri-nary (3-nary) search tree
2
1
3
4
5
What about N-nary search tree(n-元搜尋樹)?
It is called B tree.
13
建立一棵 binary tree 使其
所有 left sub-tree 的 elements 都小於其
root element 且
所有 right sub-tree 的 elements 都大於其
root element,
但此 binary tree is NOT
a binary search tree? Ans. see next page
14
This is NOT a binary search tree
5
3
4
7
1
9
8
Because 4 is NOT less than 3
15
Another binary search tree:
50
32
41
38
40
16
假設一棵 binary tree 是一條 chain –
也就是,
除 leaf 外, 每個 element
都只有 一條 branch.
If tree t has n elements, how many branches
are there from the root to the leaf?
ANS: n-1
17
9.2 Properties of Binary Trees
定義:
leaves(t) 計算 在 binary tree t 中
有多少 leaves?
Recursively!
18
Simplest case: When t is empty
Other simple case:
When t has only 1 element
Otherwise,
用 leftTree(t), rightTree(t) 有多少 leaves
來表示 t 有多少 leaves.
19
if t is empty
leaves(t) = 0
else if t consists of a root element only
leaves(t) = 1
else
leaves(t) = leaves(leftTree(t)) +
leaves(rightTree(t))
20
How about n(t),
the number of elements in t?
if t is empty
n(t) =
else
n(t) =
ANS: n(t) = 0
n(t) = n(leftTree (t)) + n(rightTree (t)) +1
21
現在使用一些家族術語:
child,
parent,
grandchild,
sibling,
cousin…
22
50
40
70
91
48
40 is the left child of 50
50 is the parent of 70
50 is the parent of 40
40 and 70 are siblings
What is 91 to 50? ANS: grandchild
What is 91 to 48? ANS: cousin
23
Descendant
D is a descendant 後代 of A (Ancestor 祖先)
if A is the parent of D
or
if the parent of D is a descendant of A
24
A path in a binary tree is:
A list of elements 使得
每個 element, 除最後一個外,
都是下一個 element 的 parent.
注意:
path 這觀念是基於之前定義的 list, parent 兩
個觀念來定義的.
軟體乃是逐步定義觀念,也可說,軟體就是
這樣一步一步開發出來的
25
50
30
80
62
90
84
Determine the path from 50 to 84.
Answer: 50, 80, 90, 84
26
height(t):
從 root 到最遠的 leaf 有多少 branches.
50
30
80
62
90
84
height(t)=? ANS: 3
27
50
30
80
62
90
height(t) = ? ANS: 2
28
50
30
80
height(t) = ? ANS: 1
29
50
height(t) = ? ANS: 0
30
One more example:
有棵 binary tree 的
left sub-tree 高度為 4
right sub-tree 高度為 10
這顆樹的高度 height 為多少?
ANS: 11
31
So, if a binary tree has height 0,
its left and right subtrees
must each have a height of what?
ANS: -1
32
if t is empty
height (t) = – 1
else
height (t) = ?
ANS: max ( height(leftTree (t) ),
height(rightTree (t)) )
+1
33
depth(x), element x 的深度:
由 root 到 element x 有多少 branches?
If x is the root element
depth(x) = 0
else
depth(x) =
ANS: depth (parent(x) ) + 1
34
level (x), the level of x,
is the same as the depth (x).
Is height (t) the same as depth(x)?
ANS: No. See next page,
height(50) is 3. depth(62) is 2.
35
In the following binary tree,
What is 1) depth(62)? 2) level(90)?
3) the height of the sub-tree rooted at 90?
50
30
80
62
90
ANS: 2, 2, 1
84
36
A binary tree t is a two-tree
如果 t 為空的,或
所有 t 的 elements
都有兩個 branches (兩孩子)
或沒有 branch (無孩子)
37
A n exa m p le o f a t wo -tr ee:
A
B
C
D
E
F
G
38
Is this a two-tree?
A
B
C
D
G
E
H
I
F
J
K
L
ANS: No, “C” does not have two branches.
39
用 Recursive 方式來說:
A binary tree t is a two-tree 如果
t 為空的 或
leftTree(t) 與 rightTree(t)
為空的 或 不為空的 two-trees.
40
A binary tree t is a full tree 如果
t 是棵 two-tree
而且
t 的所有 leaf 都擁有相同的 depth.
41
A f u ll b in ary t ree:
A
B
C
D
H
E
I
J
F
K
L
G
M
N
O
42
用 Recursive 方式來說:
A binary tree t is full 如果
t 為空的 或
height(leftTree(t)) = height(rightTree(t))
而且 leftTree(t) 與 rightTree(t)
都是 …?
ANS: full
43
A binary tree t is a complete tree
if t is full
through a depth of height(t) - 1, and
each leaf whose depth is height(t) is
as far to the left as possible.
44
A complete binary tree:
A
B
C
D
F
E
G
R
S
L
45
A binary tree that is not complete:
A
B
C
D
F
E
G
L
R
Q
B
G
46
Complete binary tree 的每個 element, 用一個
non-negative integer(非負整數) 來表示:
A0
B 1
D3
F7
C2
E 4
G8
L9
R5
S6
47
這暗示我們:
Complete binary tree
可用 array來實作.
A
B
C
D
E
R
S
F
G
L
48
然後,
當我們想要 random-access(隨機存取)array
內容時,就可 quickly access:
parent from child
children from parent.
49
Parent at 0, children at 1, 2
Parent at 1, children at 3, 4
Parent at 2, children at 5, 6
…
Parent at i, children at ?
ANS: 2i+1, 2i+2
50
Child at 1, parent at 0
Child at 2, parent at 0
Child at 3, parent at 1
Child at 4, parent at 1
Child at 5, parent at 2
Child at 6, parent at 2
…
Child at i, parent at ? ANS: ( i - 1) / 2
51
所以,用 array 來實作 complete binary
tree 相當有效率!
那我們可用 ArrayList 來實作 complete
binary tree 嗎?
Yes, 這與使用 array 是相同的概念.
How about a LinkedList? No good. (No
random access)
52
Exercise: 建構一棵 binary tree t 滿足:
1. t 是 two tree;
2. t 是 complete tree;
3. height (leftTree (t)) ==
height (rightTree (t));
4. t 不是 full.
53
ANS:
A
B
C
D
H
E
I
J
F
K
L
G
M
54
9.3 The Binary Tree Theorem
For any non-empty binary tree t:
n(t) + 1
1.
leaves(t) <=
2.0
n(t) + 1
2.
<= 2
height(t)
2.0
55
3. Equality holds in part 1
if and only if t is a two-tree.
4. Equality holds in part 2
if and only if t is a full tree.
56
What is the significance of the binary tree
theorem? Suppose t is full. Then,
n(t) + 1
=2
height(t)
2.0
So,
n(t) + 1
height(t) = log2 (
)
2.0
57
That is, a full tree has height that is
logarithmic in n.
The height of a complete binary tree is also
logarithmic in n.
What about the height of a chain?
linear in n.
58
Chapter 10
會看到特別型式的 binary tree:
Binary search tree
59
50
30
12
90
40
25
86
100
60
71
60
在一棵 binary search tree,
left subtree 的每個 element 都
小於 root element,
right subtree 的每個 element 都
大於 root element, 而且
the left and right subtrees are …?
ANS: binary search trees
61
In the BinarySearchTree class,
the “average” height of a
BinarySearchTree object is:
logarithmic in n,
and so the average time to insert, remove or
search is:
logarithmic in n.
62
But a BinarySearchTree object
can be a chain;
then, the time to insert, remove or search is:
linear in n.
63
For an (unsorted) array,
ArrayList, or LinkedList collection,
the time to insert, remove or search is:
linear in n.
64
9.4 External Path Length
t 為一棵非空的 binary tree.
E(t), the external path length of t, 為:
所有 t 的 leaves 的 depths 的總和
65
F or exa mp le, fi nd t h e externa l pa th l en g th
of th e f ollo w in g b in ary tree:
A
B
C
D
F
E
G
R
S
L
66
E (t) = 3 + 3 + 3 + 2 + 2 = 13
67
The external path length theorem:
Let t be a binary tree with k > 0 leaves.
Then,
E(t) >= (k / 2) floor (log2k)
68
9.5 Traversals of a Binary Tree
A traversal of a binary tree
是可走訪 binary tree 中
每一個 element 的演算法
69
下面的 binary-tree traversal algorithms
並不是 Java collection framework (JCF) 的
class 的 methods,
因為如前所述 JCF 並不定義 binary-tree
class.
70
inOrder(t)
{
if (t is not empty)
{
inOrder(leftTree(t));
access the root element of t;
inOrder(rightTree(t));
}
}
Left – Root – Right
71
50
30
12
90
40
86
100
使用 in-order traversal 的方式
決定走訪 elements 的順序
72
Answer:
12, 30, 40, 50, 86, 90, 100
73
postOrder (t)
{
if (t is not empty)
{
postOrder(leftTree(t));
postOrder(rightTree(t));
access the root element of t;
}
}
Left – Right – Root
74
使用 post-order traversal 決定走訪 elements
順序. Hint: An operator immediately follows
its operands.
+
–
X
/
Y
Z
*
A
B
75
A n sw er: X , Y , -, Z, A , B , * , / +
P os tfi x!
76
preOrder (t)
{
if (t is not empty)
{
access the root element of t;
preOrder (leftTree (t));
preOrder (rightTree (t));
}
}
Root – Left – Right
77
使用 pre-order traversal 決定走訪 elements
順序. Hint: An operator immediately
precedes its operands.
+
–
X
/
Y
Z
*
A
B
78
Answer:
+, -, X, Y, /, Z, *, A, B
Prefix!
79
對非空的 binary tree
做 breadth-first (廣度優先) traversal
1) 存取 root,
2) 從左到右存取 root 的 children (level 1),
3) 由左至右存取 root 的 grandchildren
(level 2),
4) 依此類推.
80
Perform a breadth-first traversal
of the following binary tree.
A
B
C
D
F
E
G
R
S
L
81
Answer:
A, B, C, D, E, R, S, F, G, L
82
Perform the following traversals of the tree:
inOrder (Left – Root - Right)
postOrder (Left - Right - Root)
preOrder (Root - Left - Right)
breadthFirst (Root - Level1 - Level2 …)
A
B
C
D
F
E
G
L
R
S
83
ANS:
inOrder
postOrder
preOrder
breathFirst
FDGBLEARCS
FGDLEBRSCA
ABDFGELCRS
ABCDERSFGL
84