```java
package ds.impl;
import ds.Container;
public class BTree<K extends Comparable<K>, V> implements Container {
private static final int M = 4;
private Node root;
private int height;
private int size;
private static class Node {
private int m;
private final Entry[] children = new Entry[M];
public Node(int k) {
m = k;
}
}
private static class Entry {
private Comparable key;
private final Object value;
private Node next;
public Entry(Comparable key, Object value, Node next) {
this.key = key;
this.value = value;
this.next = next;
}
}
public BTree() {
root = new Node(0);
}
@Override
public int size() {
return size;
}
public int height() {
return height;
}
public V get(K key) {
if (key == null) {
throw new IllegalArgumentException();
}
return search(root, key, height);
}
private V search(Node node, K key, int height) {
Entry[] children = node.children;
if (height == 0) {
for (int i = 0; i < node.m; i++) {
if (eq(key, children[i].key)) {
return (V) children[i].value;
}
}
} else {
for (int i = 0; i < node.m; i++) {
if (i + 1 == node.m || less(key, children[i + 1].key)) {
return search(children[i].next, key, height - 1);
}
}
}
return null;
}
private boolean eq(Comparable k1, Comparable k2) {
return k1.compareTo(k2) == 0;
}
private boolean less(Comparable k1, Comparable k2) {
return k1.compareTo(k2) < 0;
}
public void put(K key, V value) {
if (key == null) {
throw new IllegalArgumentException();
}
Node u = insert(root, key, value, this.height);
size++;
if (u == null) {
return;
}
Node t = new Node(2);
t.children[0] = new Entry(root.children[0].key, null, root);
t.children[1] = new Entry(u.children[0].key, null, u);
root = t;
height++;
}
private Node insert(Node node, K key, V value, int height) {
int i;
Entry entry = new Entry(key, value, null);
if (height == 0) {
for (i = 0; i < node.m; i++) {
if (less(key, node.children[i].key)) {
break;
}
}
} else {
for (i = 0; i < node.m; i++) {
if ((i + 1 == node.m) || less(key, node.children[i + 1].key)) {
Node next = node.children[i++].next;
Node u = insert(next, key, value, height - 1);
if (u == null) {
return null;
}
entry.key = u.children[0].key;
entry.next = u;
break;
}
}
}
for (int j = node.m; j > i; j--) {
node.children[j] = node.children[j - 1];
}
node.children[i] = entry;
node.m++;
if (node.m < M) {
return null;
} else {
return split(node);
}
}
private Node split(Node node) {
int m = M / 2;
Node result = new Node(m);
node.m = m;
for (int j = 0; j < m; j++) {
result.children[j] = node.children[m + j];
}
return result;
}
@Override
public String toString() {
return toString(root, height, "") + "\n";
}
private String toString(Node node, int height, String indent) {
StringBuilder s = new StringBuilder();
Entry[] children = node.children;
if (height == 0) {
for (int j = 0; j < node.m; j++) {
s.append(indent)
.append(children[j].key)
.append('=')
.append(children[j].value)
.append('\n');
}
} else {
for (int j = 0; j < node.m; j++) {
if (j > 0) {
s.append(indent)
.append('(')
.append(children[j].key)
.append(')')
.append('\n');
}
s.append(toString(children[j].next, height - 1, indent + "#"));
}
}
return s.toString();
}
public static void main(String[] args) {
BTree<String, String> st = new BTree<>();
st.put("www.cs.princeton.edu", "128.112.136.12");
st.put("www.cs.princeton.edu", "128.112.136.11");
st.put("www.princeton.edu", "128.112.128.15");
st.put("www.yale.edu", "130.132.143.21");
st.put("www.simpsons.com", "209.052.165.60");
st.put("www.apple.com", "17.112.152.32");
st.put("www.amazon.com", "207.171.182.16");
st.put("www.ebay.com", "66.135.192.87");
st.put("www.cnn.com", "64.236.16.20");
st.put("www.google.com", "216.239.41.99");
st.put("www.nytimes.com", "199.239.136.200");
st.put("www.microsoft.com", "207.126.99.140");
st.put("www.dell.com", "143.166.224.230");
st.put("www.slashdot.org", "66.35.250.151");
st.put("www.espn.com", "199.181.135.201");
st.put("www.weather.com", "63.111.66.11");
st.put("www.yahoo.com", "216.109.118.65");
System.out.println("cs.princeton.edu: " + st.get("www.cs.princeton.edu"));
System.out.println("hardvardsucks.com: " + st.get("www.harvardsucks.com"));
System.out.println("simpsons.com: " + st.get("www.simpsons.com"));
System.out.println("apple.com: " + st.get("www.apple.com"));
System.out.println("ebay.com: " + st.get("www.ebay.com"));
System.out.println("dell.com: " + st.get("www.dell.com"));
System.out.println();
System.out.println("size: " + st.size());
System.out.println("height: " + st.height());
System.out.println(st);
System.out.println();
}
}
```
- 1 设计接口
- 1.1 容器接口Container
- 1.2 背包接口Bag
- 1.3 栈接口Stack
- 1.4 队列接口Queue
- 1.5 Union-Find算法接口UF
- 2 实现接口
- 2.1 结点类Node
- 2.2 数组迭代器ArrayIterator
- 2.3 链表迭代器ListIterator
- 2.4 背包(Bag)的实现
- 2.4.1 能动态调整数组大小的Bag
- 2.4.2 链式Bag的实现
- 2.5 栈(Stack)的实现
- 2.5.1 能动态调整数组大小的Stack
- 2.5.2 链式Stack的实现
- 2.6 队列(Queue)的实现
- 2.6.1 能动态调整数组大小的Queue
- 2.6.2 链式Queue的实现
- 2.7 Union-Find算法的实现
- 2.7.1 DefaultUF
- 2.7.2 QuickFindUF
- 2.7.3 QuickUnionUF
- 2.7.4 WeightedQuickUnionUF
- 2.8 测试
- 2.8.1 测试Stack
- 2.8.2 测试Union-Find
- 3 排序算法
- 3.1 定义排序工具的类结构
- 3.2 选择排序
- 3.3 插入排序
- 3.4 希尔排序
- 3.5 归并排序
- 3.5.1 归并排序的合并方法
- 3.5.2 自顶向下的归并排序
- 3.5.3 自底向上的归并排序
- 3.6 快速排序
- 3.6.1 常规快速排序
- 3.6.2 排序前先洗牌
- 3.6.3 快速排序的改进方法-小数据量转成插入排序
- 3.6.4 快速排序的改进方法-三向切分
- 3.7 堆排序
- 3.8 最终的排序工具
- 4 搜索
- 4.1 二分搜索(binarySearch)
- 4.2 优先队列(MaxPriorityQueue)
- 4.3 二叉查找树(BST)
- 4.4 红黑二叉查找树(RedBlackBST)
- 4.5 B-树(BTree)
- 5 图
- 5.1 无向图(Graph)
- 5.2 有向图(Digraph)
- 6 贪心
- Dijkstra算法-单元最短路径
- 7 动态规划
- 7.1 最长公共子序列问题
- 7.2 0-1背包问题
- 7.3 加工顺序问题
- 8 搜索法
- 8.1 图的着色问题
- 8.2 深度优先搜索
- 8.3 回溯法
- 8.3.1 回溯法的算法框架
- 8.3.2 子集树
- 8.3.3 排列树
- 8.3.4 满m叉树(组合树)
- 8.4 广度优先搜索
- 8.5 分支限界法
- 9 随机化算法
- 9.1 数值随机化算法
- 9.2 蒙特卡罗算法
- 9.3 拉斯维加斯算法
- 9.4 舍伍德算法
- 10 数论算法
- 10.1 Stein求最大公约数
- 10.2 矩阵求斐波那切数列
- LeetCode刷题笔记