38
Chapter 11 B-tree Chapter 11 B-tree 11.1 m-way 11.1 m-way 搜尋樹 搜尋樹 11.2 B-tree 11.2 B-tree

Chapter 11 B-tree

  • Upload
    denise

  • View
    84

  • Download
    3

Embed Size (px)

DESCRIPTION

Chapter 11 B-tree. 11.1 m-way 搜尋樹 11.2 B-tree. B-tree B-tree 的功能非常強大,有許多資料庫系統皆採用 B-tree 來儲存與刪除其資料。. 11.1 m-way 搜尋樹. 何謂 m-waw 搜尋樹 (m-way search tree) ? 一棵 m-way 搜尋樹,所有節點的分支度( dgree )均小於或等於 m 。若 T 為空樹,則 T 亦稱為 m-way 搜尋樹,倘若 T 不是空樹,則必須具備下列的性質: - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 11 B-tree

Chapter 11 B-treeChapter 11 B-tree

11.1 m-way 11.1 m-way 搜尋樹搜尋樹 11.2 B-tree11.2 B-tree

Page 2: Chapter 11 B-tree

資料結構 - 使用 C 語言 2

B-treeB-tree B-tree 的功能非常強大,有許多資料庫

系統皆採用 B-tree 來儲存與刪除其資料。

Page 3: Chapter 11 B-tree

資料結構 - 使用 C 語言 3

11.1 m-way11.1 m-way 搜尋樹搜尋樹 何謂 m-waw 搜尋樹 (m-way search tree) ?

一棵 m-way 搜尋樹,所有節點的分支度( dgree )均小於或等於 m 。若 T 為空樹,則 T亦稱為 m-way 搜尋樹,倘若 T 不是空樹,則必須具備下列的性質: 節點的型態是 n, A0, (K1, A1), (K2, A2),...,(Kn, An)

其中 Ai 是子樹的指標 0 ≤ i ≤ n < m; n 為節點上的鍵值數, Ki 是鍵值 1 ≤ i ≤ n 及 1 ≤ n < m 。

Page 4: Chapter 11 B-tree

資料結構 - 使用 C 語言 4

11.1 m-way11.1 m-way 搜尋樹搜尋樹

節點中的鍵值是由小至大排列的,因此 Ki <

Ki+1 , 1 ≤ i< n 。 子樹 Ai 的所有鍵值均小於鍵值 Ki+1 但大於 Ki ,

0 < i < n 。 子樹 An 的所有鍵值均大於 Kn ,而且子樹 A0

的所有鍵值小於 K1 。 Ai 指到的子樹, 0 ≤ i ≤ n 亦是 m-way 搜尋樹。

Page 5: Chapter 11 B-tree

資料結構 - 使用 C 語言 5

11.1 m-way11.1 m-way 搜尋樹搜尋樹 例如有一 3-way 的搜

尋樹,其中有 12 個鍵值分別為 12, 17, 23, 25, 28, 32, 38, 45, 48, 55, 60, 70 。

表為圖 11-1 中每個節點之 3-way 的搜尋樹表示法。

Page 6: Chapter 11 B-tree

資料結構 - 使用 C 語言 6

11.1 m-way11.1 m-way 搜尋樹搜尋樹 由於 3-way 搜尋榼,每個節點的型態是

n, A0, (K1, A1), (K2, A2),...,(Kn, An) ,因此a 節點的格式為

2, b, (23, c), (48, d) 表示 a 節點有 2 個鍵值,在 b 節點中的

所有鍵值均小於 23 ,在 c 節點中的每個鍵值大小介於 23 與 48 之間,最後 d 節點的所有鍵值均大於 48 。

Page 7: Chapter 11 B-tree

資料結構 - 使用 C 語言 7

11.1 m-way11.1 m-way 搜尋樹搜尋樹 11.1.1 m-way 搜尋樹的加入

Page 8: Chapter 11 B-tree

資料結構 - 使用 C 語言 8

11.1 m-way11.1 m-way 搜尋樹搜尋樹

Page 9: Chapter 11 B-tree

資料結構 - 使用 C 語言 9

11.1 m-way11.1 m-way 搜尋樹搜尋樹

Page 10: Chapter 11 B-tree

資料結構 - 使用 C 語言 10

11.1 m-way11.1 m-way 搜尋樹搜尋樹

Page 11: Chapter 11 B-tree

資料結構 - 使用 C 語言 11

11.1 m-way11.1 m-way 搜尋樹搜尋樹11.1.2 m-way 搜尋樹的刪除 而在刪除方法上則與二元搜尋樹極為相

同,若刪除非樹葉節點上的鍵值,則以左子樹中最大的鍵值或右子樹中的最小鍵值取代之。

Page 12: Chapter 11 B-tree

資料結構 - 使用 C 語言 12

11.1 m-way11.1 m-way 搜尋樹搜尋樹 刪除 3 則直接刪除之:

Page 13: Chapter 11 B-tree

資料結構 - 使用 C 語言 13

11.1 m-way11.1 m-way 搜尋樹搜尋樹 刪除 8 :

刪除 12 :

Page 14: Chapter 11 B-tree

資料結構 - 使用 C 語言 14

11.1 m-way11.1 m-way 搜尋樹搜尋樹 刪除 7 :

刪除 10 :

Page 15: Chapter 11 B-tree

資料結構 - 使用 C 語言 15

11.2 B-tree11.2 B-tree 一棵 order 為 m 的 B-tree 是一 m-way 搜

尋樹。若是空樹,也算 B-tree ,假若高度 > 1 必須滿足以下的特性: 樹根至少有二個子節點( children ),亦即

節點內至少有一鍵值( key value )。 除了樹根外,所有節點至少有個子節點,至

多有 m 個子節點。此表示至少應有 - 1 個鍵值,至多有 m-1 個鍵值(表示大於 m/2 的最小正整數)。

所有的樹葉節點皆在同一階層。

Page 16: Chapter 11 B-tree

資料結構 - 使用 C 語言 16

11.2 B-tree11.2 B-tree

Page 17: Chapter 11 B-tree

資料結構 - 使用 C 語言 17

11.2 B-tree11.2 B-tree 在圖 11-2 中 (a) 不屬於 B-tree of order 3 ,

因為樹葉節點不在同一階層上,而 (b) 是屬於 B-tree of order 3 ,因為所有的樹葉節點皆在同一階層。

B-tree of order 3 表示除了樹葉節點外每一節點的分支度( degree )不是等於 2就是等於 3 ,因此 B-tree of order 3 就是著名的 2-3 tree 。假使 m=4 ,則是 2-3-4 tree 。

Page 18: Chapter 11 B-tree

資料結構 - 使用 C 語言 18

11.2 B-tree11.2 B-tree11.2.1 B-tree 的加入 假設加入 P 節點,若

該節點少於 m-1 個鍵值。 該節點的鍵值已等於 m-1 ,則將此節點分為

二,因為一棵 order 為 m 的 B-tree ,最多只能有 m-1 個鍵值。

Page 19: Chapter 11 B-tree

資料結構 - 使用 C 語言 19

11.2 B-tree11.2 B-tree 請看下例之說明(此處的 B-tree 為 order

5 )

Page 20: Chapter 11 B-tree

資料結構 - 使用 C 語言 20

11.2 B-tree11.2 B-tree 加入 88 於圖 11-3

Page 21: Chapter 11 B-tree

資料結構 - 使用 C 語言 21

11.2 B-tree11.2 B-tree 承 (1) 加入 98

Page 22: Chapter 11 B-tree

資料結構 - 使用 C 語言 22

11.2 B-tree11.2 B-tree 承 (2) 加入 91

Page 23: Chapter 11 B-tree

資料結構 - 使用 C 語言 23

11.2 B-tree11.2 B-tree 承 (3) 加入 93

Page 24: Chapter 11 B-tree

資料結構 - 使用 C 語言 24

11.2 B-tree11.2 B-tree 承 (4) 加入 99

Page 25: Chapter 11 B-tree

資料結構 - 使用 C 語言 25

11.2 B-tree11.2 B-tree11.2.2 B-tree 的刪除 B-tree 的刪除與 2-3 tree 和 2-3-4 tree 的

刪除基本上原理是相同的,此處也分成兩部份: 刪除的節點是樹葉節點( leaf node ), 刪除的節點為非樹葉節點( non-leaf nod

e )。

Page 26: Chapter 11 B-tree

資料結構 - 使用 C 語言 26

11.2 B-tree11.2 B-tree 以 B-tree of order 5 如圖 11-4 來說明。

Page 27: Chapter 11 B-tree

資料結構 - 使用 C 語言 27

11.2 B-tree11.2 B-tree

若刪除的節點是樹葉節點若刪除的節點是樹葉節點 如將圖 11-4 刪除 70

Page 28: Chapter 11 B-tree

資料結構 - 使用 C 語言 28

11.2 B-tree11.2 B-tree

如欲將圖 11-4 中的鍵值 26 刪除

Page 29: Chapter 11 B-tree

資料結構 - 使用 C 語言 29

11.2 B-tree11.2 B-tree

若欲刪除 85

Page 30: Chapter 11 B-tree

資料結構 - 使用 C 語言 30

11.2 B-tree11.2 B-tree

有一棵 B-tree of order 5 如圖 11-5 所示

Page 31: Chapter 11 B-tree

資料結構 - 使用 C 語言 31

11.2 B-tree11.2 B-tree 先從圖 11-5 刪除 59

Page 32: Chapter 11 B-tree

資料結構 - 使用 C 語言 32

11.2 B-tree11.2 B-tree 由於合併後 c 節點僅存放一個鍵值,不

符合 B-tree 的定義

Page 33: Chapter 11 B-tree

資料結構 - 使用 C 語言 33

11.2 B-tree11.2 B-tree

若刪除的節點為非樹葉節點若刪除的節點為非樹葉節點

Page 34: Chapter 11 B-tree

資料結構 - 使用 C 語言 34

11.2 B-tree11.2 B-tree 若刪除圖 11-6 的鍵值 50 ,找到 p' 節點

為 g ,從中取出最小值 52 ,並代替 50 。

Page 35: Chapter 11 B-tree

資料結構 - 使用 C 語言 35

11.2 B-tree11.2 B-tree 若再刪除 52

Page 36: Chapter 11 B-tree

資料結構 - 使用 C 語言 36

11.2 B-tree11.2 B-tree 承上圖,若繼續刪除 55

Page 37: Chapter 11 B-tree

資料結構 - 使用 C 語言 37

11.2 B-tree11.2 B-tree 由於 g 節點其鍵值數少於 個鍵值,且其

兄弟節點 h 也沒有大於 個鍵值,故將 g 、h 、與 c 的鍵值 65 合併於 g 節點,結果如下圖:

Page 38: Chapter 11 B-tree

資料結構 - 使用 C 語言 38

11.2 B-tree11.2 B-tree 此時 c 節點的鍵值數也少於 ,且其兄弟

節點的鍵值數不大於 ,故將 b 、 c 與 a節點合併,結果如下: