B樹定義
B 樹又叫平衡多路查找樹。一棵m階的B 樹 (m叉樹)的特性如下:
- 根節點至少有兩個孩子
- 每個非根節點至少有M/2(上取整)個孩子,至多有M個孩子。
- 每個非根節點至少有M/2-1(上取整)個關鍵字,至多有M-1個關鍵字。並以升序排列。
- key[i]和key[i+1]之間的孩子節點的值介於key[i]和key[i+1]之間。
- 所有的葉子節點都在同一層。
注意:B-樹,即為B樹。
B樹Java實現
/** * 一顆B樹的簡單實現。 * * @param <K> - 鍵類型 * @param <V> - 值類型 */ @SuppressWarnings("all") public class BTree<K, V> { private static Log logger = LogFactory.getLog(BTree.class); /** * B樹節點中的鍵值對。 * <p/> * B樹的節點中存儲的是鍵值對。 * 通過鍵訪問值。 * * @param <K> - 鍵類型 * @param <V> - 值類型 */ private static class Entry<K, V> { private K key; private V value; public Entry(K k, V v) { this.key = k; this.value = v; } public K getKey() { return key; } public V getValue() { return value; } public void setValue(V value) { this.value = value; } @Override public String toString() { return key + ":" + value; } } /** * 在B樹節點中搜索給定鍵值的返回結果。 * <p/> * 該結果有兩部分組成。第一部分表示此次查找是否成功, * 如果查找成功,第二部分表示給定鍵值在B樹節點中的位置, * 如果查找失敗,第二部分表示給定鍵值應該插入的位置。 */ private static class SearchResult<V> { private boolean exist; private int index; private V value; public SearchResult(boolean exist, int index) { this.exist = exist; this.index = index; } public SearchResult(boolean exist, int index, V value) { this(exist, index); this.value = value; } public boolean isExist() { return exist; } public int getIndex() { return index; } public V getValue() { return value; } } /** * B樹中的節點。 * <p> * TODO 需要考慮並發情況下的存取。 */ private static class BTreeNode<K, V> { /** * 節點的項,按鍵非降序存放 */ private List<Entry<K, V>> entrys; /** * 內節點的子節點 */ private List<BTreeNode<K, V>> children; /** * 是否為葉子節點 */ private boolean leaf; /** * 鍵的比較函數對象 */ private Comparator<K> kComparator; private BTreeNode() { entrys = new ArrayList<Entry<K, V>>(); children = new ArrayList<BTreeNode<K, V>>(); leaf = false; } public BTreeNode(Comparator<K> kComparator) { this(); this.kComparator = kComparator; } public boolean isLeaf() { return leaf; } public void setLeaf(boolean leaf) { this.leaf = leaf; } /** * 返回項的個數。如果是非葉子節點,根據B樹的定義, * 該節點的子節點個數為({@link #size()} + 1)。 * * @return 關鍵字的個數 */ public int size() { return entrys.size(); } int compare(K key1, K key2) { return kComparator == null ? ((Comparable<K>) key1).compareTo(key2) : kComparator.compare(key1, key2); } /** * 在節點中查找給定的鍵。 * 如果節點中存在給定的鍵,則返回一個<code>SearchResult</code>, * 標識此次查找成功,給定的鍵在節點中的索引和給定的鍵關聯的值; * 如果不存在,則返回<code>SearchResult</code>, * 標識此次查找失敗,給定的鍵應該插入的位置,該鍵的關聯值為null。 * <p/> * 如果查找失敗,返回結果中的索引域為[0, {@link #size()}]; * 如果查找成功,返回結果中的索引域為[0, {@link #size()} - 1] * <p/> * 這是一個二分查找算法,可以保證時間復雜度為O(log(t))。 * * @param key - 給定的鍵值 * @return - 查找結果 */ public SearchResult<V> searchKey(K key) { int low = 0; int high = entrys.size() - 1; int mid = 0; while (low <= high) { mid = (low + high) / 2; // 先這么寫吧,BTree實現中,l+h不可能溢出 Entry<K, V> entry = entrys.get(mid); if (compare(entry.getKey(), key) == 0) // entrys.get(mid).getKey() == key break; else if (compare(entry.getKey(), key) > 0) // entrys.get(mid).getKey() > key high = mid - 1; else // entry.get(mid).getKey() < key low = mid + 1; } boolean result = false; int index = 0; V value = null; if (low <= high) // 說明查找成功 { result = true; index = mid; // index表示元素所在的位置 value = entrys.get(index).getValue(); } else { result = false; index = low; // index表示元素應該插入的位置 } return new SearchResult<V>(result, index, value); } /** * 將給定的項追加到節點的末尾, * 你需要自己確保調用該方法之后,節點中的項還是 * 按照關鍵字以非降序存放。 * * @param entry - 給定的項 */ public void addEntry(Entry<K, V> entry) { entrys.add(entry); } /** * 刪除給定索引的<code>entry</code>。 * <p/> * 你需要自己保證給定的索引是合法的。 * * @param index - 給定的索引 */ public Entry<K, V> removeEntry(int index) { return entrys.remove(index); } /** * 得到節點中給定索引的項。 * <p/> * 你需要自己保證給定的索引是合法的。 * * @param index - 給定的索引 * @return 節點中給定索引的項 */ public Entry<K, V> entryAt(int index) { return entrys.get(index); } /** * 如果節點中存在給定的鍵,則更新其關聯的值。 * 否則插入。 * * @param entry - 給定的項 * @return null,如果節點之前不存在給定的鍵,否則返回給定鍵之前關聯的值 */ public V putEntry(Entry<K, V> entry) { SearchResult<V> result = searchKey(entry.getKey()); if (result.isExist()) { V oldValue = entrys.get(result.getIndex()).getValue(); entrys.get(result.getIndex()).setValue(entry.getValue()); return oldValue; } else { insertEntry(entry, result.getIndex()); return null; } } /** * 在該節點中插入給定的項, * 該方法保證插入之后,其鍵值還是以非降序存放。 * <p/> * 不過該方法的時間復雜度為O(t)。 * <p/> * <b>注意:</b>B樹中不允許鍵值重復。 * * @param entry - 給定的鍵值 * @return true,如果插入成功,false,如果插入失敗 */ public boolean insertEntry(Entry<K, V> entry) { SearchResult<V> result = searchKey(entry.getKey()); if (result.isExist()) return false; else { insertEntry(entry, result.getIndex()); return true; } } /** * 在該節點中給定索引的位置插入給定的項, * 你需要自己保證項插入了正確的位置。 * * @param entry - 給定的鍵值 * @param index - 給定的索引 */ public void insertEntry(Entry<K, V> entry, int index) { /* * 通過新建一個ArrayList來實現插入真的很惡心,先這樣吧 * 要是有類似C中的reallocate就好了。 */ List<Entry<K, V>> newEntrys = new ArrayList<Entry<K, V>>(); int i = 0; // index = 0或者index = keys.size()都沒有問題 for (; i < index; ++i) newEntrys.add(entrys.get(i)); newEntrys.add(entry); for (; i < entrys.size(); ++i) newEntrys.add(entrys.get(i)); entrys.clear(); entrys = newEntrys; } /** * 返回節點中給定索引的子節點。 * <p/> * 你需要自己保證給定的索引是合法的。 * * @param index - 給定的索引 * @return 給定索引對應的子節點 */ public BTreeNode<K, V> childAt(int index) { if (isLeaf()) throw new UnsupportedOperationException("Leaf node doesn't have children."); return children.get(index); } /** * 將給定的子節點追加到該節點的末尾。 * * @param child - 給定的子節點 */ public void addChild(BTreeNode<K, V> child) { children.add(child); } /** * 刪除該節點中給定索引位置的子節點。 * </p> * 你需要自己保證給定的索引是合法的。 * * @param index - 給定的索引 */ public void removeChild(int index) { children.remove(index); } /** * 將給定的子節點插入到該節點中給定索引 * 的位置。 * * @param child - 給定的子節點 * @param index - 子節點帶插入的位置 */ public void insertChild(BTreeNode<K, V> child, int index) { List<BTreeNode<K, V>> newChildren = new ArrayList<BTreeNode<K, V>>(); int i = 0; for (; i < index; ++i) newChildren.add(children.get(i)); newChildren.add(child); for (; i < children.size(); ++i) newChildren.add(children.get(i)); children = newChildren; } } private static final int DEFAULT_T = 2; /** * B樹的根節點 */ private BTreeNode<K, V> root; /** * 根據B樹的定義,B樹的每個非根節點的關鍵字數n滿足(t - 1) <= n <= (2t - 1) */ private int t = DEFAULT_T; /** * 非根節點中最小的鍵值數 */ private int minKeySize = t - 1; /** * 非根節點中最大的鍵值數 */ private int maxKeySize = 2 * t - 1; /** * 鍵的比較函數對象 */ private Comparator<K> kComparator; /** * 構造一顆B樹,鍵值采用采用自然排序方式 */ public BTree() { root = new BTreeNode<K, V>(); root.setLeaf(true); } public BTree(int t) { this(); this.t = t; minKeySize = t - 1; maxKeySize = 2 * t - 1; } /** * 以給定的鍵值比較函數對象構造一顆B樹。 * * @param kComparator - 鍵值的比較函數對象 */ public BTree(Comparator<K> kComparator) { root = new BTreeNode<K, V>(kComparator); root.setLeaf(true); this.kComparator = kComparator; } public BTree(Comparator<K> kComparator, int t) { this(kComparator); this.t = t; minKeySize = t - 1; maxKeySize = 2 * t - 1; } @SuppressWarnings("unchecked") int compare(K key1, K key2) { return kComparator == null ? ((Comparable<K>) key1).compareTo(key2) : kComparator.compare(key1, key2); } /** * 搜索給定的鍵。 * * @param key - 給定的鍵值 * @return 鍵關聯的值,如果存在,否則null */ public V search(K key) { return search(root, key); } /** * 在以給定節點為根的子樹中,遞歸搜索 * 給定的<code>key</code> * * @param node - 子樹的根節點 * @param key - 給定的鍵值 * @return 鍵關聯的值,如果存在,否則null */ private V search(BTreeNode<K, V> node, K key) { SearchResult<V> result = node.searchKey(key); if (result.isExist()) return result.getValue(); else { if (node.isLeaf()) return null; else search(node.childAt(result.getIndex()), key); } return null; } /** * 分裂一個滿子節點<code>childNode</code>。 * <p/> * 你需要自己保證給定的子節點是滿節點。 * * @param parentNode - 父節點 * @param childNode - 滿子節點 * @param index - 滿子節點在父節點中的索引 */ private void splitNode(BTreeNode<K, V> parentNode, BTreeNode<K, V> childNode, int index) { assert childNode.size() == maxKeySize; BTreeNode<K, V> siblingNode = new BTreeNode<K, V>(kComparator); siblingNode.setLeaf(childNode.isLeaf()); // 將滿子節點中索引為[t, 2t - 2]的(t - 1)個項插入新的節點中 for (int i = 0; i < minKeySize; ++i) siblingNode.addEntry(childNode.entryAt(t + i)); // 提取滿子節點中的中間項,其索引為(t - 1) Entry<K, V> entry = childNode.entryAt(t - 1); // 刪除滿子節點中索引為[t - 1, 2t - 2]的t個項 for (int i = maxKeySize - 1; i >= t - 1; --i) childNode.removeEntry(i); if (!childNode.isLeaf()) // 如果滿子節點不是葉節點,則還需要處理其子節點 { // 將滿子節點中索引為[t, 2t - 1]的t個子節點插入新的節點中 for (int i = 0; i < minKeySize + 1; ++i) siblingNode.addChild(childNode.childAt(t + i)); // 刪除滿子節點中索引為[t, 2t - 1]的t個子節點 for (int i = maxKeySize; i >= t; --i) childNode.removeChild(i); } // 將entry插入父節點 parentNode.insertEntry(entry, index); // 將新節點插入父節點 parentNode.insertChild(siblingNode, index + 1); } /** * 在一個非滿節點中插入給定的項。 * * @param node - 非滿節點 * @param entry - 給定的項 * @return true,如果B樹中不存在給定的項,否則false */ private boolean insertNotFull(BTreeNode<K, V> node, Entry<K, V> entry) { assert node.size() < maxKeySize; if (node.isLeaf()) // 如果是葉子節點,直接插入 return node.insertEntry(entry); else { /* 找到entry在給定節點應該插入的位置,那么entry應該插入 * 該位置對應的子樹中 */ SearchResult<V> result = node.searchKey(entry.getKey()); // 如果存在,則直接返回失敗 if (result.isExist()) return false; BTreeNode<K, V> childNode = node.childAt(result.getIndex()); if (childNode.size() == 2 * t - 1) // 如果子節點是滿節點 { // 則先分裂 splitNode(node, childNode, result.getIndex()); /* 如果給定entry的鍵大於分裂之后新生成項的鍵,則需要插入該新項的右邊, * 否則左邊。 */ if (compare(entry.getKey(), node.entryAt(result.getIndex()).getKey()) > 0) childNode = node.childAt(result.getIndex() + 1); } return insertNotFull(childNode, entry); } } /** * 在B樹中插入給定的鍵值對。 * * @param key - 鍵 * @param value - 值 * @return true,如果B樹中不存在給定的項,否則false */ public boolean insert(K key, V value) { if (root.size() == maxKeySize) // 如果根節點滿了,則B樹長高 { BTreeNode<K, V> newRoot = new BTreeNode<K, V>(kComparator); newRoot.setLeaf(false); newRoot.addChild(root); splitNode(newRoot, root, 0); root = newRoot; } return insertNotFull(root, new Entry<K, V>(key, value)); } /** * 如果存在給定的鍵,則更新鍵關聯的值, * 否則插入給定的項。 * * @param node - 非滿節點 * @param entry - 給定的項 * @return true,如果B樹中不存在給定的項,否則false */ private V putNotFull(BTreeNode<K, V> node, Entry<K, V> entry) { assert node.size() < maxKeySize; if (node.isLeaf()) // 如果是葉子節點,直接插入 return node.putEntry(entry); else { /* 找到entry在給定節點應該插入的位置,那么entry應該插入 * 該位置對應的子樹中 */ SearchResult<V> result = node.searchKey(entry.getKey()); // 如果存在,則更新 if (result.isExist()) return node.putEntry(entry); BTreeNode<K, V> childNode = node.childAt(result.getIndex()); if (childNode.size() == 2 * t - 1) // 如果子節點是滿節點 { // 則先分裂 splitNode(node, childNode, result.getIndex()); /* 如果給定entry的鍵大於分裂之后新生成項的鍵,則需要插入該新項的右邊, * 否則左邊。 */ if (compare(entry.getKey(), node.entryAt(result.getIndex()).getKey()) > 0) childNode = node.childAt(result.getIndex() + 1); } return putNotFull(childNode, entry); } } /** * 如果B樹中存在給定的鍵,則更新值。 * 否則插入。 * * @param key - 鍵 * @param value - 值 * @return 如果B樹中存在給定的鍵,則返回之前的值,否則null */ public V put(K key, V value) { if (root.size() == maxKeySize) // 如果根節點滿了,則B樹長高 { BTreeNode<K, V> newRoot = new BTreeNode<K, V>(kComparator); newRoot.setLeaf(false); newRoot.addChild(root); splitNode(newRoot, root, 0); root = newRoot; } return putNotFull(root, new Entry<K, V>(key, value)); } /** * 從B樹中刪除一個與給定鍵關聯的項。 * * @param key - 給定的鍵 * @return 如果B樹中存在給定鍵關聯的項,則返回刪除的項,否則null */ public Entry<K, V> delete(K key) { return delete(root, key); } /** * 從以給定<code>node</code>為根的子樹中刪除與給定鍵關聯的項。 * <p/> * 刪除的實現思想請參考《算法導論》第二版的第18章。 * * @param node - 給定的節點 * @param key - 給定的鍵 * @return 如果B樹中存在給定鍵關聯的項,則返回刪除的項,否則null */ private Entry<K, V> delete(BTreeNode<K, V> node, K key) { // 該過程需要保證,對非根節點執行刪除操作時,其關鍵字個數至少為t。 assert node.size() >= t || node == root; SearchResult<V> result = node.searchKey(key); /* * 因為這是查找成功的情況,0 <= result.getIndex() <= (node.size() - 1), * 因此(result.getIndex() + 1)不會溢出。 */ if (result.isExist()) { // 1.如果關鍵字在節點node中,並且是葉節點,則直接刪除。 if (node.isLeaf()) return node.removeEntry(result.getIndex()); else { // 2.a 如果節點node中前於key的子節點包含至少t個項 BTreeNode<K, V> leftChildNode = node.childAt(result.getIndex()); if (leftChildNode.size() >= t) { // 使用leftChildNode中的最后一個項代替node中需要刪除的項 node.removeEntry(result.getIndex()); node.insertEntry(leftChildNode.entryAt(leftChildNode.size() - 1), result.getIndex()); // 遞歸刪除左子節點中的最后一個項 return delete(leftChildNode, leftChildNode.entryAt(leftChildNode.size() - 1).getKey()); } else { // 2.b 如果節點node中后於key的子節點包含至少t個關鍵字 BTreeNode<K, V> rightChildNode = node.childAt(result.getIndex() + 1); if (rightChildNode.size() >= t) { // 使用rightChildNode中的第一個項代替node中需要刪除的項 node.removeEntry(result.getIndex()); node.insertEntry(rightChildNode.entryAt(0), result.getIndex()); // 遞歸刪除右子節點中的第一個項 return delete(rightChildNode, rightChildNode.entryAt(0).getKey()); } else // 2.c 前於key和后於key的子節點都只包含t-1個項 { Entry<K, V> deletedEntry = node.removeEntry(result.getIndex()); node.removeChild(result.getIndex() + 1); // 將node中與key關聯的項和rightChildNode中的項合並進leftChildNode leftChildNode.addEntry(deletedEntry); for (int i = 0; i < rightChildNode.size(); ++i) leftChildNode.addEntry(rightChildNode.entryAt(i)); // 將rightChildNode中的子節點合並進leftChildNode,如果有的話 if (!rightChildNode.isLeaf()) { for (int i = 0; i <= rightChildNode.size(); ++i) leftChildNode.addChild(rightChildNode.childAt(i)); } return delete(leftChildNode, key); } } } } else { /* * 因為這是查找失敗的情況,0 <= result.getIndex() <= node.size(), * 因此(result.getIndex() + 1)會溢出。 */ if (node.isLeaf()) // 如果關鍵字不在節點node中,並且是葉節點,則什么都不做,因為該關鍵字不在該B樹中 { logger.info("The key: " + key + " isn't in this BTree."); return null; } BTreeNode<K, V> childNode = node.childAt(result.getIndex()); if (childNode.size() >= t) // // 如果子節點有不少於t個項,則遞歸刪除 return delete(childNode, key); else // 3 { // 先查找右邊的兄弟節點 BTreeNode<K, V> siblingNode = null; int siblingIndex = -1; if (result.getIndex() < node.size()) // 存在右兄弟節點 { if (node.childAt(result.getIndex() + 1).size() >= t) { siblingNode = node.childAt(result.getIndex() + 1); siblingIndex = result.getIndex() + 1; } } // 如果右邊的兄弟節點不符合條件,則試試左邊的兄弟節點 if (siblingNode == null) { if (result.getIndex() > 0) // 存在左兄弟節點 { if (node.childAt(result.getIndex() - 1).size() >= t) { siblingNode = node.childAt(result.getIndex() - 1); siblingIndex = result.getIndex() - 1; } } } // 3.a 有一個相鄰兄弟節點至少包含t個項 if (siblingNode != null) { if (siblingIndex < result.getIndex()) // 左兄弟節點滿足條件 { childNode.insertEntry(node.entryAt(siblingIndex), 0); node.removeEntry(siblingIndex); node.insertEntry(siblingNode.entryAt(siblingNode.size() - 1), siblingIndex); siblingNode.removeEntry(siblingNode.size() - 1); // 將左兄弟節點的最后一個孩子移到childNode if (!siblingNode.isLeaf()) { childNode.insertChild(siblingNode.childAt(siblingNode.size()), 0); siblingNode.removeChild(siblingNode.size()); } } else // 右兄弟節點滿足條件 { childNode.insertEntry(node.entryAt(result.getIndex()), childNode.size() - 1); node.removeEntry(result.getIndex()); node.insertEntry(siblingNode.entryAt(0), result.getIndex()); siblingNode.removeEntry(0); // 將右兄弟節點的第一個孩子移到childNode // childNode.insertChild(siblingNode.childAt(0), childNode.size() + 1); if (!siblingNode.isLeaf()) { childNode.addChild(siblingNode.childAt(0)); siblingNode.removeChild(0); } } return delete(childNode, key); } else // 3.b 如果其相鄰左右節點都包含t-1個項 { if (result.getIndex() < node.size()) // 存在右兄弟,直接在后面追加 { BTreeNode<K, V> rightSiblingNode = node.childAt(result.getIndex() + 1); childNode.addEntry(node.entryAt(result.getIndex())); node.removeEntry(result.getIndex()); node.removeChild(result.getIndex() + 1); for (int i = 0; i < rightSiblingNode.size(); ++i) childNode.addEntry(rightSiblingNode.entryAt(i)); if (!rightSiblingNode.isLeaf()) { for (int i = 0; i <= rightSiblingNode.size(); ++i) childNode.addChild(rightSiblingNode.childAt(i)); } } else // 存在左節點,在前面插入 { BTreeNode<K, V> leftSiblingNode = node.childAt(result.getIndex() - 1); childNode.insertEntry(node.entryAt(result.getIndex() - 1), 0); node.removeEntry(result.getIndex() - 1); node.removeChild(result.getIndex() - 1); for (int i = leftSiblingNode.size() - 1; i >= 0; --i) childNode.insertEntry(leftSiblingNode.entryAt(i), 0); if (!leftSiblingNode.isLeaf()) { for (int i = leftSiblingNode.size(); i >= 0; --i) childNode.insertChild(leftSiblingNode.childAt(i), 0); } } // 如果node是root並且node不包含任何項了 if (node == root && node.size() == 0) root = childNode; return delete(childNode, key); } } } } /** * 一個簡單的層次遍歷B樹實現,用於輸出B樹。 */ public void output() { Queue<BTreeNode<K, V>> queue = new LinkedList<BTreeNode<K, V>>(); queue.offer(root); while (!queue.isEmpty()) { BTreeNode<K, V> node = queue.poll(); for (int i = 0; i < node.size(); ++i) System.out.print(node.entryAt(i) + " "); System.out.println(); if (!node.isLeaf()) { for (int i = 0; i <= node.size(); ++i) queue.offer(node.childAt(i)); } } } public static void main(String[] args) { Random random = new Random(); BTree<Integer, Integer> btree = new BTree<Integer, Integer>(2); List<Integer> save = new ArrayList<Integer>(); for (int i = 0; i < 10; ++i) { int r = random.nextInt(100); save.add(r); System.out.print(r + " "); btree.insert(r, r); } System.out.println("----------------------"); btree.output(); System.out.println("----------------------"); btree.delete(save.get(0)); btree.output(); } }