> For the complete documentation index, see [llms.txt](https://garychang.gitbook.io/data-structure/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://garychang.gitbook.io/data-structure/lecture2-tree-and-binary-tree/lecture-2.8-advanced-trees/2.8.7-b-tree.md).

# 2.8.7 - B Tree

### 1. B Tree of order m

是一個Balanced m-way search tree，主要用於external search/sort，滿足:

1. 2 ≤ Root's Degree ≤ m
2. 除了root和failure nodes外，其餘節點滿: $$\[\frac{m}{2}]≤node's\ degree≤m$$&#x20;
3. 所有failure nodes皆位於同一level（Balanced）
4. B tree of order 3又稱2-3 tree；B tree of order 4又稱2-3-4 tree

圖示:

![](https://2769815795-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGckN3OAfinKuVIrkMj%2F-LHqZP-ogYC816j9L8cv%2F-LHqZQtvSo_mXorL2CFs%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-07-20%2015.22.23.jpg?alt=media\&token=2be0dfe3-464c-4ff2-81ff-f35f0d4d3959)

* 高度h的B Tree of order m

  * 最多節點數 $$=\frac{m^h-1}{m-1}$$&#x20;
  * 最多資料數 $$=m^h-1$$&#x20;
  * 最少節點數: root's degree = 2，nodes' degree = $$\[\frac{m}{2}]$$&#x20;

  &#x20;       $$1+2+2\[\frac{m}{2}]+2\[\frac{m}{2}]^{2}+2\[\frac{m}{2}]^{3}+...+2\[\frac{m}{2}]^{h-2}\\\ \ = 1+2(\[\frac{m}{2}]^{0}+\[\frac{m}{2}]^{1}+\[\frac{m}{2}]^{2}+...+\[\frac{m}{2}]^{h-2})\\\ \ =1+2(\frac{\[\frac{m}{2}]^{h-1}-1}{\[\frac{m}{2}]-1})$$&#x20;

* 最少資料數

  $$1+2(\frac{\[\frac{m}{2}]^{h-1}-1}{\[\frac{m}{2}]-1})\*(\[\frac{m}{2}]-1)\\=2\[\frac{m}{2}]^{h-1}-1$$&#x20;

### 2. 操作

#### 2.1 Insert x in an B tree of order m

1. search(x)找到適合存放的節點
2. 檢查該節點是否overflow
   1. overflow : Data數 > m-1，作`split`處理，接著檢查父點
   2. 未overflow: Data數 ≤ m-1，直接放入x

![](https://2769815795-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGckN3OAfinKuVIrkMj%2F-LHrdFnVR4CZDb7FC9Ex%2F-LHre5d79CnK0bEz5DYj%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-07-20%2020.21.40.jpg?alt=media\&token=c2ca0846-e8b0-44f4-a6c0-21f845eb70ba)

#### 2.2 Delete x in an B tree of order m

步驟流程圖:

![](https://2769815795-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGckN3OAfinKuVIrkMj%2F-LHrnjZLqvUcxQAVslRt%2F-LHroEix95-bck3Ig_1J%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-07-20%2021.10.02.jpg?alt=media\&token=468026bb-3961-440c-b822-023f66e878fa)

* **Rotation**

![](https://2769815795-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGckN3OAfinKuVIrkMj%2F-LHrqvqIbpagt8ds_ORp%2F-LHrqxEd-QU5nNURWORa%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-07-20%2021.22.53.jpg?alt=media\&token=7cd8e715-96f2-45c7-afde-2d19e115889f)

* **Combine**

![](https://2769815795-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGckN3OAfinKuVIrkMj%2F-LHrrhGKM-J3yMaUOLSx%2F-LHrrnIOsM4tYWv0SGHp%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-07-20%2021.26.16.jpg?alt=media\&token=c6cb5a93-64d5-4854-9a93-4cb85452a0f2)
