在計算機科學中,B樹(英語:B-tree)是一種自平衡的樹,可以保持數據有序。這種數據結構可以讓查找數據、順序訪問、插入數據及刪除的動做,都在對數時間內完成。B樹,歸納來講是一個通常化的二叉查找樹(binary search tree),能夠擁有多於2個子節點。與自平衡二叉查找樹不一樣,B樹爲系統大塊數據的讀寫操做作了優化。B樹減小定位記錄時所經歷的中間過程,從而加快存取速度。B樹這種數據結構能夠用來描述外部存儲。這種數據結構常被應用在數據庫和文件系統的實現上。html
B-Tree is a self-balanced search tree with multiple keys in every node and more than two children for every node.
B樹是一種自平衡的搜索樹,每個節點node都有多個keys,而且每一個節點有2個子節點或者多於2個子節點。node
A B+ tree is an N-ary tree with a variable but often large number of children per node. A B+ tree consists of a root, internal nodes and leaves.The root may be either a leaf or a node with two or more children
B+樹是一個n叉排序樹,一般每一個節點有多個孩子,一棵B+樹包含一個根節點、多個內部節點和葉子節點。根節點多是一個葉子節點,也多是一個包含兩個或兩個以上孩子節點的節點。mysql
B-Tree of Order m has the following properties...sql
Property #1
- All the leaf nodes must be at same level.Property #2
- All nodes except root must have at least [m/2]-1 keys and maximum of m-1 keys.Property #3
- All non leaf nodes except root (i.e. all internal nodes) must have at least m/2 children.Property #4
- If the root node is a non leaf node, then it must have at least 2 children.Property #5
- A non leaf node with n-1 keys must have n number of children.Property #6
- All the key values within a node must be in Ascending Order.In a B-Ttree, the search operation is similar to that of Binary Search Tree. In a Binary search tree, the search process starts from the root node and every time we make a 2-way decision (we go to either left subtree or right subtree). In B-Tree also search process starts from the root node but every time we make n-way decision where n is the total number of children that node has. In a B-Ttree, the search operation is performed with O(log n) time complexity. The search operation is performed as follows...數據庫
大體的意思就是查詢的條件A,和root節點值比較,若是相等則success,不相等則判斷是否小於該節點,小於則查詢左子樹,大於則查詢此時節點的下一個值,以此類推,直到此時節點最後一個值仍是小於A,則查詢右子樹,直到找到或者結束查找。數據結構
Insertion Operation in B-Tree
In a B-Tree, the new element must be added only at leaf node. That means, always the new keyValue is attached to leaf node only. The insertion operation is performed as follows...ide
參考8的連接,有圖有真相,這裏就摘錄一些重點文字條件吧。優化
k:刪除的值 x: k所在的節點 x.n: k所在節點的長度 t: k所在節點的層級
Here you can straightaway delete k from x.spa
If k is in the node x which is a leaf and x.n == (t-1).net
Find the immediate sibling y of x, the extreme key m of y, the parent p of x and the parent key l of k If y.n >= t: Move l into x Move m into p Delete k from x
Find the immediate sibling y of x, the extreme key m of y, the parent p of x and the parent key l of k If y.n == t-1: Merge x and y Move down l to the new node as the median key Delete k from the new node
If k is in the node x and x is an internal node (not a leaf)
Find the child node y that precedes k (the node which is on the left side of k) If y.n >= t: Find the key k’ in y which is the predecessor of k Delete k’ recursively. (Here k’ can be another internal node as well. So we have to delete it recursively in the same way) Replace k with k’ in x
Find the child node y that precedes k If y.n < t (or y.n == (t-1)): Find the child node z that follows k (the node which is on the right side of k) If z.n >= t: Find k’’ in z which is the successor of k Delete k’’ recursively Replace k with k’’ in x
Find the child node y that precedes k and the child node z that follows k If y.n == (t-1) AND z.n == (t-1): Merge k and z to y Free memory of node z Recursively delete k from y
B和B+樹的區別在於,B+樹的非葉子結點只包含key信息,不包含data,每一個節點的指針上限爲2t而不是2t+1.全部的葉子結點和相連的節點使用鏈表相連,便於區間查找和遍歷。
磁盤存儲和mysql的索引這一塊用的比較多,以空間換時間來提高查找速度。(圖片基本是從參考連接那邊拿過來的,站在前人的肩膀上。)