- 資料結構與演算法
- DSA - 首頁
- DSA - 概述
- DSA - 環境設定
- DSA - 演算法基礎
- DSA - 漸近分析
- 資料結構
- DSA - 資料結構基礎
- DSA - 資料結構和型別
- DSA - 陣列資料結構
- 連結串列
- DSA - 連結串列資料結構
- DSA - 雙向連結串列資料結構
- DSA - 迴圈連結串列資料結構
- 棧與佇列
- DSA - 棧資料結構
- DSA - 表示式解析
- DSA - 佇列資料結構
- 搜尋演算法
- DSA - 搜尋演算法
- DSA - 線性搜尋演算法
- DSA - 二分搜尋演算法
- DSA - 插值搜尋
- DSA - 跳躍搜尋演算法
- DSA - 指數搜尋
- DSA - 斐波那契搜尋
- DSA - 子列表搜尋
- DSA - 雜湊表
- 排序演算法
- DSA - 排序演算法
- DSA - 氣泡排序演算法
- DSA - 插入排序演算法
- DSA - 選擇排序演算法
- DSA - 歸併排序演算法
- DSA - 希爾排序演算法
- DSA - 堆排序
- DSA - 桶排序演算法
- DSA - 計數排序演算法
- DSA - 基數排序演算法
- DSA - 快速排序演算法
- 圖資料結構
- DSA - 圖資料結構
- DSA - 深度優先遍歷
- DSA - 廣度優先遍歷
- DSA - 生成樹
- 樹資料結構
- DSA - 樹資料結構
- DSA - 樹的遍歷
- DSA - 二叉搜尋樹
- DSA - AVL樹
- DSA - 紅黑樹
- DSA - B樹
- DSA - B+樹
- DSA - 伸展樹
- DSA - 字典樹
- DSA - 堆資料結構
- 遞迴
- DSA - 遞迴演算法
- DSA - 使用遞迴的漢諾塔
- DSA - 使用遞迴的斐波那契數列
- 分治法
- DSA - 分治法
- DSA - 最大最小問題
- DSA - Strassen矩陣乘法
- DSA - Karatsuba演算法
- 貪心演算法
- DSA - 貪心演算法
- DSA - 旅行商問題(貪心法)
- DSA - Prim最小生成樹
- DSA - Kruskal最小生成樹
- DSA - Dijkstra最短路徑演算法
- DSA - 地圖著色演算法
- DSA - 分數揹包問題
- DSA - 帶截止日期的作業排序
- DSA - 最優合併模式演算法
- 動態規劃
- DSA - 動態規劃
- DSA - 矩陣鏈乘法
- DSA - Floyd-Warshall演算法
- DSA - 0-1揹包問題
- DSA - 最長公共子序列演算法
- DSA - 旅行商問題(動態規劃法)
- 近似演算法
- DSA - 近似演算法
- DSA - 頂點覆蓋演算法
- DSA - 集合覆蓋問題
- DSA - 旅行商問題(近似演算法)
- 隨機化演算法
- DSA - 隨機化演算法
- DSA - 隨機化快速排序演算法
- DSA - Karger最小割演算法
- DSA - Fisher-Yates洗牌演算法
- DSA有用資源
- DSA - 問答
- DSA - 快速指南
- DSA - 有用資源
- DSA - 討論
B+樹
B+樹是B樹的擴充套件,旨在提高插入、刪除和搜尋操作的效率。
B+樹的特性與B樹類似,不同之處在於B樹可以在所有內部節點和葉子節點中儲存鍵和記錄,而B+樹在葉子節點中儲存記錄,在內部節點中儲存鍵。B+樹的一個重要特性是所有葉子節點都以單鏈表的形式相互連線,並且有一個數據指標指向磁碟檔案中存在的資料。這有助於以相同的磁碟訪問次數獲取記錄。
由於主記憶體的大小有限,B+樹充當無法儲存在主記憶體中的記錄的資料儲存。為此,內部節點儲存在主記憶體中,葉子節點儲存在輔助儲存器中。
B+樹的特性
B+樹中除根節點外的每個節點最多包含m個子節點和(m-1)個鍵,最少包含$\mathrm{\left \lceil \frac{m}{2}\right \rceil}$個子節點和$\mathrm{\left \lceil \frac{m-1}{2}\right \rceil}$個鍵,因為樹的階數為m。
根節點必須至少有兩個子節點和至少一個搜尋鍵。
B樹中的所有路徑必須在同一級別結束,即葉子節點必須在同一級別。
B+樹始終保持資料排序。
B+樹的基本操作
B+樹支援的操作包括插入、刪除和搜尋,每個操作的時間複雜度為O(log n)。
它們與B樹操作幾乎相同,因為這兩種資料結構儲存資料的基本思想相同。然而,不同之處在於資料只儲存在B+樹的葉子節點中,而B樹則不然。
插入操作
向B+樹插入資料從葉子節點開始。
步驟1 - 計算要新增到B+樹節點中的鍵的最大值和最小值。
步驟2 - 將元素逐個新增到葉子節點中,直到超過最大鍵數。
步驟3 - 將節點分成兩半,左子節點包含最小數量的鍵,其餘鍵儲存在右子節點中。
步驟4 - 但是,如果內部節點也超過最大鍵屬性,則將節點分成兩半,左子節點包含最小數量的鍵,其餘鍵儲存在右子節點中。但是,右子節點中最小的數字將成為父節點。
步驟5 - 如果葉子節點和內部節點都具有最大數量的鍵,則以類似的方式將它們都分割,並將右子節點中最小的鍵新增到父節點中。
示例
以下是各種程式語言中此操作的實現:
// C program for Bplus tree
#include <stdio.h>
#include <stdlib.h>
struct BplusTree {
int *d;
struct BplusTree **child_ptr;
int l;
int n;
};
struct BplusTree *r = NULL, *np = NULL, *x = NULL;
struct BplusTree* init() {
//to create nodes
int i;
np = (struct BplusTree*)malloc(sizeof(struct BplusTree));
np->d = (int*)malloc(6 * sizeof(int)); // order 6
np->child_ptr = (struct BplusTree**)malloc(7 * sizeof(struct BplusTree*));
np->l = 1;
np->n = 0;
for (i = 0; i < 7; i++) {
np->child_ptr[i] = NULL;
}
return np;
}
void traverse(struct BplusTree *p) {
//traverse tree
printf("\n");
int i;
for (i = 0; i < p->n; i++) {
if (p->l == 0) {
traverse(p->child_ptr[i]);
}
printf(" %d", p->d[i]);
}
if (p->l == 0) {
traverse(p->child_ptr[i]);
}
printf("\n");
}
void sort(int *p, int n) {
int i, j, t;
for (i = 0; i < n; i++) {
for (j = i; j <= n; j++) {
if (p[i] > p[j]) {
t = p[i];
p[i] = p[j];
p[j] = t;
}
}
}
}
int split_child(struct BplusTree *x, int i) {
int j, mid;
struct BplusTree *np1, *np3, *y;
np3 = init();
np3->l = 1;
if (i == -1) {
mid = x->d[2];
x->d[2] = 0;
x->n--;
np1 = init();
np1->l = 0;
x->l = 1;
for (j = 3; j < 6; j++) {
np3->d[j - 3] = x->d[j];
np3->child_ptr[j - 3] = x->child_ptr[j];
np3->n++;
x->d[j] = 0;
x->n--;
}
for (j = 0; j < 6; j++) {
x->child_ptr[j] = NULL;
}
np1->d[0] = mid;
np1->child_ptr[np1->n] = x;
np1->child_ptr[np1->n + 1] = np3;
np1->n++;
r = np1;
} else {
y = x->child_ptr[i];
mid = y->d[2];
y->d[2] = 0;
y->n--;
for (j = 3; j < 6; j++) {
np3->d[j - 3] = y->d[j];
np3->n++;
y->d[j] = 0;
y->n--;
}
x->child_ptr[i + 1] = y;
x->child_ptr[i + 1] = np3;
}
return mid;
}
void insert(int a) {
int i, t;
x = r;
if (x == NULL) {
r = init();
x = r;
} else {
if (x->l == 1 && x->n == 6) {
t = split_child(x, -1);
x = r;
for (i = 0; i < x->n; i++) {
if (a > x->d[i] && a < x->d[i + 1]) {
i++;
break;
} else if (a < x->d[0]) {
break;
} else {
continue;
}
}
x = x->child_ptr[i];
} else {
while (x->l == 0) {
for (i = 0; i < x->n; i++) {
if (a > x->d[i] && a < x->d[i + 1]) {
i++;
break;
} else if (a < x->d[0]) {
break;
} else {
continue;
}
}
if (x->child_ptr[i]->n == 6) {
t = split_child(x, i);
x->d[x->n] = t;
x->n++;
continue;
} else {
x = x->child_ptr[i];
}
}
}
}
x->d[x->n] = a;
sort(x->d, x->n);
x->n++;
}
int main() {
int i, n, t;
insert(10);
insert(20);
insert(30);
insert(40);
insert(50);
printf("Insertion Done");
printf("\nB+ tree:");
traverse(r);
return 0;
}
輸出
Insertion Done B+ tree: 10 20 30 40 50
#include<iostream>
using namespace std;
struct BplusTree {
int *d;
BplusTree **child_ptr;
bool l;
int n;
}
*r = NULL, *np = NULL, *x = NULL;
BplusTree* init() { //to create nodes
int i;
np = new BplusTree;
np->d = new int[6];//order 6
np->child_ptr = new BplusTree *[7];
np->l = true;
np->n = 0;
for (i = 0; i < 7; i++) {
np->child_ptr[i] = NULL;
}
return np;
}
void traverse(BplusTree *p) { //traverse tree
cout<<endl;
int i;
for (i = 0; i < p->n; i++) {
if (p->l == false) {
traverse(p->child_ptr[i]);
}
cout << " " << p->d[i];
}
if (p->l == false) {
traverse(p->child_ptr[i]);
}
cout<<endl;
}
void sort(int *p, int n) { //sort the tree
int i, j, t;
for (i = 0; i < n; i++) {
for (j = i; j <= n; j++) {
if (p[i] >p[j]) {
t = p[i];
p[i] = p[j];
p[j] = t;
}
}
}
}
int split_child(BplusTree *x, int i) {
int j, mid;
BplusTree *np1, *np3, *y;
np3 = init();
np3->l = true;
if (i == -1) {
mid = x->d[2];
x->d[2] = 0;
x->n--;
np1 = init();
np1->l = false;
x->l = true;
for (j = 3; j < 6; j++) {
np3->d[j - 3] = x->d[j];
np3->child_ptr[j - 3] = x->child_ptr[j];
np3->n++;
x->d[j] = 0;
x->n--;
}
for (j = 0; j < 6; j++) {
x->child_ptr[j] = NULL;
}
np1->d[0] = mid;
np1->child_ptr[np1->n] = x;
np1->child_ptr[np1->n + 1] = np3;
np1->n++;
r = np1;
} else {
y = x->child_ptr[i];
mid = y->d[2];
y->d[2] = 0;
y->n--;
for (j = 3; j <6 ; j++) {
np3->d[j - 3] = y->d[j];
np3->n++;
y->d[j] = 0;
y->n--;
}
x->child_ptr[i + 1] = y;
x->child_ptr[i + 1] = np3;
}
return mid;
}
void insert(int a) {
int i, t;
x = r;
if (x == NULL) {
r = init();
x = r;
} else {
if (x->l== true && x->n == 6) {
t = split_child(x, -1);
x = r;
for (i = 0; i < (x->n); i++) {
if ((a >x->d[i]) && (a < x->d[i + 1])) {
i++;
break;
} else if (a < x->d[0]) {
break;
} else {
continue;
}
}
x = x->child_ptr[i];
} else {
while (x->l == false) {
for (i = 0; i < (x->n); i++) {
if ((a >x->d[i]) && (a < x->d[i + 1])) {
i++;
break;
} else if (a < x->d[0]) {
break;
} else {
continue;
}
}
if ((x->child_ptr[i])->n == 6) {
t = split_child(x, i);
x->d[x->n] = t;
x->n++;
continue;
} else {
x = x->child_ptr[i];
}
}
}
}
x->d[x->n] = a;
sort(x->d, x->n);
x->n++;
}
int main() {
int i, n, t;
insert(10);
insert(20);
insert(30);
insert(40);
insert(50);
cout<<"Insertion Done";
cout<<"\nB+ tree:";
traverse(r);
}
輸出
Insertion Done B+ tree: 10 20 30 40 50
//Java program for Bplus code
import java.util.*;
class BplusTree {
int[] d;
BplusTree[] child_ptr;
boolean l;
int n;
}
public class Main {
static BplusTree r = null, np = null, x = null;
static BplusTree init() { // to create nodes
int i;
np = new BplusTree();
np.d = new int[6]; // order 6
np.child_ptr = new BplusTree[7];
np.l = true;
np.n = 0;
for (i = 0; i < 7; i++) {
np.child_ptr[i] = null;
}
return np;
}
static void traverse(BplusTree p) { // traverse tree
int i;
for (i = 0; i < p.n; i++) {
if (p.l == false) {
traverse(p.child_ptr[i]);
}
System.out.print(" " + p.d[i]);
}
if (p.l == false) {
traverse(p.child_ptr[i]);
}
System.out.println();
}
static void sort(int[] p, int n) { // sort the tree
int i, j, t;
for (i = 0; i < n; i++) {
for (j = i; j <= n; j++) {
if (p[i] > p[j]) {
t = p[i];
p[i] = p[j];
p[j] = t;
}
}
}
}
static int split_child(BplusTree x, int i) {
int j, mid;
BplusTree np1, np3, y;
np3 = init();
np3.l = true;
if (i == -1) {
mid = x.d[2];
x.d[2] = 0;
x.n--;
np1 = init();
np1.l = false;
x.l = true;
for (j = 3; j < 6; j++) {
np3.d[j - 3] = x.d[j];
np3.child_ptr[j - 3] = x.child_ptr[j];
np3.n++;
x.d[j] = 0;
x.n--;
}
for (j = 0; j < 6; j++) {
x.child_ptr[j] = null;
}
np1.d[0] = mid;
np1.child_ptr[np1.n] = x;
np1.child_ptr[np1.n + 1] = np3;
np1.n++;
r = np1;
} else {
y = x.child_ptr[i];
mid = y.d[2];
y.d[2] = 0;
y.n--;
for (j = 3; j < 6; j++) {
np3.d[j - 3] = y.d[j];
np3.n++;
y.d[j] = 0;
y.n--;
}
x.child_ptr[i + 1] = y;
x.child_ptr[i + 1] = np3;
}
return mid;
}
static void insert(int a) {
int i, t;
x = r;
if (x == null) {
r = init();
x = r;
} else {
if (x.l == true && x.n == 6) {
t = split_child(x, -1);
x = r;
for (i = 0; i < x.n; i++) {
if (a > x.d[i] && a < x.d[i + 1]) {
i++;
break;
} else if (a < x.d[0]) {
break;
} else {
continue;
}
}
x = x.child_ptr[i];
} else {
while (x.l == false) {
for (i = 0; i < x.n; i++) {
if (a > x.d[i] && a < x.d[i + 1]) {
i++;
break;
} else if (a < x.d[0]) {
break;
} else {
continue;
}
}
if (x.child_ptr[i].n == 6) {
t = split_child(x, i);
x.d[x.n] = t;
x.n++;
continue;
} else {
x = x.child_ptr[i];
}
}
}
}
x.d[x.n] = a;
sort(x.d, x.n);
x.n++;
}
public static void main(String[] args) {
int i, n, t;
insert(10);
insert(20);
insert(30);
insert(40);
insert(50);
System.out.print("Insertion Done");
System.out.println("\nB+ tree:");
traverse(r);
}
}
輸出
Insertion Done B+ tree: 10 20 30 40 50
#Python Program for Bplus tree
#to create nodes
class BplusTree:
def __init__(self):
self.d = [0] * 6 # order 6
self.child_ptr = [None] * 7
self.l = True
self.n = 0
def init():
np = BplusTree()
np.l = True
np.n = 0
return np
#traverse tree
def traverse(p):
for i in range(p.n):
if not p.l:
traverse(p.child_ptr[i])
print(" ", p.d[i], end="")
if not p.l:
traverse(p.child_ptr[p.n])
print()
#sort the tree
def sort(p, n):
for i in range(n):
for j in range(i, n + 1):
if p[i] > p[j]:
p[i], p[j] = p[j], p[i]
def split_child(x, i):
np3 = init()
np3.l = True
if i == -1:
mid = x.d[2]
x.d[2] = 0
x.n -= 1
np1 = init()
np1.l = False
x.l = True
for j in range(3, 6):
np3.d[j - 3] = x.d[j]
np3.child_ptr[j - 3] = x.child_ptr[j]
np3.n += 1
x.d[j] = 0
x.n -= 1
for j in range(6):
x.child_ptr[j] = None
np1.d[0] = mid
np1.child_ptr[np1.n] = x
np1.child_ptr[np1.n + 1] = np3
np1.n += 1
r = np1
else:
y = x.child_ptr[i]
mid = y.d[2]
y.d[2] = 0
y.n -= 1
for j in range(3, 6):
np3.d[j - 3] = y.d[j]
np3.n += 1
y.d[j] = 0
y.n -= 1
x.child_ptr[i + 1] = y
x.child_ptr[i + 1] = np3
return mid
def insert(a):
global r, x
x = r
if x is None:
r = init()
x = r
else:
if x.l and x.n == 6:
t = split_child(x, -1)
x = r
for i in range(x.n):
if a > x.d[i] and a < x.d[i + 1]:
i += 1
break
elif a < x.d[0]:
break
else:
continue
x = x.child_ptr[i]
else:
while not x.l:
for i in range(x.n):
if a > x.d[i] and a < x.d[i + 1]:
i += 1
break
elif a < x.d[0]:
break
else:
continue
if x.child_ptr[i].n == 6:
t = split_child(x, i)
x.d[x.n] = t
x.n += 1
continue
else:
x = x.child_ptr[i]
x.d[x.n] = a
sort(x.d, x.n)
x.n += 1
r = None
x = None
insert(10)
insert(20)
insert(30)
insert(40)
insert(50)
print("Insertion Done")
print("B+ tree:")
traverse(r)
輸出
Insertion Done B+ tree: 10 20 30 40 50