資結講義2021 5

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 16

第五講:樹 Trees

5.1 Introduction
Definition: A tree is a finite set of one or more nodes(節點) such that
(1) There is a specially designated node called the root.
(2) The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn, where
each of these sets is a tree. T1, ..., Tn are called the subtrees of the root.
至少有一個節點,剩餘節點安
排在其子樹(分支度 Degree)

Figure: Lineal (European languages) KK: [ ] 直系、世襲

Figure:Pedigree (of Dusty’s ancestors) KK: [ ] 家譜

1
A sample tree:

Terminalogy 術語
 The degree of a node is the number of subtrees of the node (往下分支的個數)
The degree of A is 3; the degree of H is 1.
 The node with degree 0 is a leaf or terminal node. (leaf :無子樹的 node)
 A node that has subtrees is the parent of the roots of the subtrees. (父節點)
○ ←parent 父節點
 The roots of these subtrees are the children of the node. (子節點) ○ ○
 Children of the same parent are siblings.(兄弟)
 The ancestors of a node are all the nodes along the path from the root to the
node. (祖先) ex: K 點的祖先: E、B、A
 The descendants of a node are all the nodes that are in its subtrees. (後代)
ex:A 的後代: 除了 A 以外的其他節點
 Assume the root is at level 1, then the level of a node is the level of the node’s
parent plus one.
 The height or the depth of a tree is the maximum level of any node in the tree.
(tree 的最大 Level 值)

List Representation of Trees

( A ( B ( E ( K, L),F ) , C( G ) , D( H( M ), I, J ) ) )

2
For several applications it is desirable to have a representation that is specified to
trees. As it is often easier to write algorithms for a data representations when the node
size is fixed, in practice one used only nodes of a fixed size to represent tree nodes.

Possible node structure for a tree of degree of k.

 Lemma 5.1: If T is a k-ary tree (i.e., a tree of degree k) with n nodes, each
having a fixed size as in Figure 5.4, then n(k-1) + 1 of the nk child fileds are 0,
n ≥ 1.

 Left Child-Right Sibling Representation


 Each node has two links (or pointers).
 Each node only has one leftmost child and one closest sibling.

3
☆Tree 轉 Binary tree (BT)解題小觀念:
Step1: 建立 sibling 間的 link
Step2: 保留最左子點的 link,其餘刪除
( 將各二元樹 root 連結起來 )
Step3: 順時鐘轉 45°

考題(台大資工)
Assume that we have a forest of three trees as illustrated in the following figure.
Please transform the forest into its binary tree representation.

4
☆題目:
一棵 Full binary tree 有 n 個節點,求 depth=?
解:
2k–1 =n
2k=n+1
k log2=log2(n+1)
k=log2(n+1)
☆題目:
一棵 complete 有 n 個節點,求 depth=?
解:(1) └log2n┘+1
解:(2) ┌log2(n+1)┐
5.2 Binary trees (二元樹)
Definition:: A binary tree is a finite set of nodes that is either empty or consists of a
root and two disjoint binary trees called the left subtree and the right subtree.
 There is no tree with zero nodes. But there is an empty binary tree.
 Binary tree distinguishes between the order of the children while in a tree we
do not.
完滿二元樹
Definition: A full binary tree of depth k is a binary tree of depth k having 2k – 1
nodes, k ≥ 0.
完整二元樹
Definition: A binary tree with n nodes and depth k is complete iff its nodes
correspond to the nodes numbered from 1 to n in the full binary tree of depth k.

Lemma 5.2 [Maximum number of nodes]


1) The maximum number of nodes on level i of a binary tree is 2i-1, i ≥ 1.
2) The maximum number of nodes in a binary tree of depth k is 2k–1, k
≥ 1.

Lemma 5.3 [Relation between number of leaf nodes and nodes of degree 2]: For
any non-empty binary tree, T, if n0 is the number of leaf nodes and n2 the number
of nodes of degree 2, then n0 = n2 + 1.
☆一定要記的三個要點!!
(1). n=n2+n1+n0
n =n0+n1+n2
(2). n0=n2+1
n-1=n0×0 + n1×1 + n2×2 (3). n-1=n0*0+n1*1+n2*2
證明:任何一棵 binary tree,下列關係必成立
n0=n1+1
proof:

5
n=n2+n1+n0
n-1=n0*0+n1*1+n2*2
牙籤數
n-1=n1+2n2
n0+n1+n2-1=n1+2n2
n0=n2+1
Complete Binary Tree:
(1) 將 node 依 level 由小到大,由左而右,將號碼依順序植入 Full Binary Tree

(2) 一棵 complete binary tree 必須存在 1~n 的連續號碼裡


☆題目:
判斷下列 binary tree 是否為 complete binary tree?
(A) (B) (C)
○ ○ ○
○ ○ ○ ○ ○
○ ○ ○ ○○

(X) (X) (V)


☆考題:
一棵 complete binary tree 有 1000 節點,依 level 由小到大、由左而右依序編號
1….1000,求下列問題:
(1). 此樹的高度 depth=?
(2). 第 10th level 有多少個 nodes?
(3). 最後一個 parent 編號=?
(4). 編號 225 的兄弟節點為何?
(5). degree=0、degree=1、degree=2 的節點分別有幾個?
解:
(1) (2) (3) (4) (5)
2k-1=1000 1000-(29-1)=489 └1000/2┘ 224 n0=500
2k=999 512-23=489 =500 (兄弟: 奇數-1 n2=499
K=10 、偶數+1 n1=1

定義:n0、n1、n2 代表 degree=0、degree=1、degree=2 的數目,n 代表總 node 數


觀察:一棵 complete binary tree ,n1 的值{當 n 為偶數,n1=1;當 n 為奇數,
n1=0}

6
☆題目:
(1)假設一棵二元樹共有 100 個 nodes,其中 leaf node 共有 40 個,求 degree=1 的
個數?
解:
n=100 100=40+39+n1
n0=40 100=79+n1
n2=39 n1=21
(3) 一棵 complete binary tree 共有 n 個 nodes,假設 n 為奇數,則此數共有多少
leaf nodes?
解:
n1=0 n=n0+n1+n2
n=n0+0+n2
n=2n0+1
n0=(n+1)/2
☆題目:
一棵二元樹 degree=1 有 1 個、degree=2 有 10 個,問 leaf node 有幾個?
解:
n=n0+n1+n2 _------
n-1=n0*0+n1*n+n2*2 ------
n-1=1*1+10*2
n-1=21
n=22 帶入
22=n0+1+10
n0=11
☆題目:
有一棵五元樹,其中 degree=1 的 node 數=1、degree=2 的 node 數=2、degree=3 的
node 數=3、degree=4 的 node 數=4、degree=5 的 node 數=5,求此樹的 leaf node 有
多少個?
解:
n=n0+n1+n2+n3+n4+n5
n=n0+15 -----
n-1=n0*0+n1*1+n2*2+n3*3+n4*4+n5*5
n-1=55
n=56 ------
帶入
56=n0+15
n0=41

7
A Full Binary Tree

*如果是 Full Binary Tree,就一定是 complete binary tree ,


但如果滿足 complete binary tree,不一定是 Full binary tree

8
Binary tree Representation-Array
1

2 3

4 5 6 7

8 9 10 11

Lemma 5.4
If a complete binary tree with n nodes (depth =élog (n + 1)ù is represented
sequentially, then for any node with index i, 1<=i<=n, we have:
 parent(i) is at if i≠1. If i=1, i is at the root and has no parent.
 left_child(i) is at 2i if 2i ≦ n. If 2i > n, then i has no left child.
 right_child(i) is at 2i+1 if 2i +1 ≦ n. If 2i +1 >n, then i has no right child.

☆Example: The following figures are 2 different binary trees, give the corresponding
array representations for these 2 trees.
(* 給一個二元樹,用 array 存,寫上編號,由上到下、由左到右)

9
Binary tree Representation-Linked List

How many links(child fields) in a binary tree with n nodes? Ans: 2n


How many zero child fields in a binary tree with n nodes? Ans: n+1

Proof: zero =n+1


zero=n0*2+n1*1 -----
n0=n2+1 ------
n=n0+n1+n2 ------
帶入
n2=n0-1
n=n0+n1+n0-1
2n0+n1=n+1 -----
綜合 得 zero = n+1

10
5.3 Binary Tree Traversals
Inorder, Preorder and Postorder
考題(元智)
Suppose we have the preorder sequence: ABCKEFGHI and the inorder sequence
BCAEDGHFI of the same binary tree please construct the binary tree.
ANS:

考題
Suppose we have the preorder sequence: ABDFHCEG and the inorder sequence
DHFBACGE of the same binary tree please construct the binary tree.
ANS:

5.5 Threaded Binary Trees 引線二元樹


若 X 為 Thread Binary Tree 的一個 node
1. 先寫出中序
2. X→Left Child=Nil(左指標未使用)
改為在引線指向中序前一個 Node
3. X→Right Child=Nil(右指標未使用)
改為在引線指向中序後一個 Node

*好處:當資料鏈結如果斷裂,還能連上。

11
堆積樹(Heap Max /Min Heap Tree)
 A max tree is a tree in which the key value in each node is no smaller than
the key values in its children. A max heap is a complete binary tree that is
also a max tree. (堆積樹是完整樹)
例:Insert 一個 Node 到 Heap Tree Max 最大堆積樹
1. 先依 complete binary tree 建構新的 node x
2. 與 x 的父節點比較,若 x>父節點→對調,直到條件不成立,或是 x 是 root
為止。
20
20 21

15 2
15 21 15 20

14 10 21
14 10 2 14 10 2
例:(師大考題)
Heap:Assume that there are 10 data (0、3、5、2、1、6、7、9、8、7)to be inserted to
construct a Max Heap Tree , please show the result. (*允許同樣的值存在)

☆同上題,刪除節點 9
8
1 8
9
7
1
8 7 6
8 6 2 1 6

10 7 7 3 10 2 7 3 10
7 7 3

0 2 0 1
0 2 1 1

12
例:由 Max Heap Tree 中 Delete 節點→刪除 root 輸出值(Sorting 排序)
1.將最後一個 Node x 補上 root(Complete Binary Tree)
2.比較 x 與左、右子節點→對調直到關係(父>=子)

21 2 20

15 20 15 20 15 2

14 10 2 14 10 10
14

output 21 output 20

20 10 15 15

15 2 15 2 10 2 14 2

14 10 14
14 10

output 15
10 14 10

2
14 2 10 2 2

output 14 output 10 output 2

13
例:(交大考題)
Do the heap operation INSERT(16)DELETE(25)INSERT(24) on the following
Max Heap Tree by step, show the results after each operation.
25

20 22

8 10 12 9

解:

☆題目:
Support that we have the following key value: 7、16、49、82、5、31、6、2、44
(1) Write out the min heap after each value is inserted.
(2) Write out the result after deleting 2、5.
解:
(1) (2) // 一個刪除再刪另一個
2
6
5 6 7 31
7 16 49 31
44 16 49 82
82 44

14
搜尋樹(Binary Search Tree→BST)
*一棵二元樹,可能為空,Key 不可重覆(唯一性)
A binary search tree is a binary tree. It may be empty. If it is not empty then it
satisifes the following properties:
 Every node has extractly one key and the keys in the tree are distinct.
 The keys in a nonempty left subtree (right subtree) are smaller (larger)
than the key in the root of subtree. 對任一節點 x 左子樹所有節點比 x 小,
右子樹所有節點比 x 大

 The left and right subtrees are also binary search trees.

20 左小於父 右大於父
20

15 25 15 25

4 16
4 16 22

在二元搜尋樹中,加入節點 X:
用 search algo 找到 X 的位置 X 與節點比大小(由 root 開始搜尋)
X 大→往右子樹 X 小→往左子樹

例:(淡江考題)
The following data is inserted into an initidly empty binary search tree(BST):
50、40、60、80、90、56、52、54、53、70。
(1)Draw the final BST 50
取左子樹中最大者
(2)Delete 52 from BST in(1) 40 56
(3)Delete 60 from BST in(2)
解: 54 80

(1) (2) (3) 53 70 90


50 50
50

40 60 40 60
40 60
56 80 56 80 50
56 80
52 70 90 54 70 90 40
54 70 90 70
54 53 取右子樹中最小者 56
53 80
53
解析: 54 90
(2) 刪除節點:
53

15
1. Delete a leaf node 直接刪除
– A leaf node which is a right child of its parent
– A leaf node which is a left child of its parent
2. Delete a non-leaf node
– A node that has one child:將 X 父節點的指標指向 X 的兒子
– A node that has two children
3. Replaced by the largest element in its left subtree, or Replaced by the
smallest element in its right subtree X 左子樹中最大值取代 X or X 右子樹中最小值
取代 X

16

You might also like