B树
1 #include<iostream> 2 using namespace std; 3 template<class K,int M=3> 4 struct BTreeNode 5 { 6 K _keys[M]; 7 BTreeNode<K, M>* _subs[M+1]; 8 size_t size; 9 BTreeNode<K, M>* _parent; 10 BTreeNode() :size(0), _parent(NULL) 11 { 12 for (size_t i = 0 ; i < M + 1; ++i) 13 { 14 _subs[i] = NULL; 15 } 16 } 17 }; 18 19 template<class K, class V> 20 struct Pair 21 { 22 K _first; 23 V _second; 24 Pair(const K&k = K(), const V&v = V()) 25 :_first(k), _second(v) 26 {} 27 }; 28 29 template<class K,int M=3> 30 class BTree 31 { 32 typedef BTreeNode<K, M> Node; 33 public: 34 BTree() :_root(NULL) 35 {} 36 Pair<Node*, int> Find(const K&key) 37 { 38 Node* parent = NULL; 39 Node* cur = _root; 40 while (cur) 41 { 42 int i = 0; 43 while (i<cur->size&&cur->_keys[i]<key) 44 { 45 ++i; 46 } 47 if (cur->_keys[i] == key) 48 { 49 return Pair<Node*, int>(cur, i); 50 } 51 parent = cur; 52 cur = cur->_subs[i]; 53 } 54 return Pair<Node*, int>(parent, -1); 55 } 56 57 bool Insert(const K&key) 58 { 59 if (_root == NULL) 60 { 61 _root = new Node; 62 _root->_keys[0] = key; 63 ++_root->size; 64 return true; 65 } 66 67 Pair<Node*, int> ret = Find(key); 68 if (ret._second != -1) 69 { 70 return false; 71 } 72 73 K k = key; 74 Node* cur = ret._first; 75 Node* sub = NULL; 76 _InsertKey(cur, k, sub); 77 while (1) 78 { 79 if (cur->size < M) 80 { 81 return true; 82 } 83 int boundary = M / 2; 84 k = cur->_keys[boundary]; 85 Node *tmp = new Node; 86 size_t size = cur->size; 87 size_t index = 0; 88 for (int i = boundary + 1; i < size; ++i) 89 { 90 tmp->_keys[index++] = cur->_keys[i]; 91 tmp->size++; 92 cur->size--; 93 } 94 95 96 index = 0; 97 for (int i = boundary + 1; i <= size; ++i) 98 { 99 tmp->_subs[index] = cur->_subs[i]; 100 if (tmp->_subs[index]) 101 { 102 tmp->_subs[index]->_parent = tmp; 103 } 104 ++index; 105 } 106 107 K k = cur->_keys[boundary]; 108 cur->size--; 109 if (cur->_parent == NULL) 110 { 111 _root = new Node; 112 _root->_keys[0] = k; 113 _root->_subs[0] = cur; 114 _root->_subs[1] = tmp; 115 _root->size = 1; 116 tmp->_parent = _root; 117 cur->_parent = _root; 118 return true; 119 } 120 else 121 { 122 cur= cur->_parent; 123 _InsertKey(cur, k, sub); 124 ret = Find(k); 125 int i = ret._second + 1; 126 cur->_subs[i] = tmp; 127 tmp->_parent = cur; 128 } 129 } 130 } 131 132 void Inorder() 133 { 134 _Inorder(_root); 135 cout << endl; 136 } 137 138 void _Inorder(Node* root) 139 { 140 if (root == NULL) 141 { 142 return; 143 } 144 for (size_t i = 0; i < root->size; ++i) 145 { 146 _Inorder(root->_subs[i]); 147 cout << root->_keys[i] << " "; 148 } 149 _Inorder(root->_subs[root->size]); 150 } 151 152 void _InsertKey(Node* cur, const K&k, Node* sub) 153 { 154 int i = cur->size - 1; 155 while (i>=0) 156 { 157 if (cur->_keys[i] > k) 158 { 159 cur->_keys[i + 1] = cur->_keys[i]; 160 cur->_subs[i + 2] = cur->_subs[i+1]; 161 --i; 162 } 163 else 164 { 165 break; 166 } 167 } 168 cur->_keys[i + 1] = k; 169 cur->_subs[i + 2] = sub; 170 if (sub) 171 { 172 sub->_parent = cur; 173 } 174 cur->size++; 175 } 176 protected: 177 Node* _root; 178 }; 179 void TestBTree() 180 { 181 BTree<int> t; 182 int a[] = { 53, 75, 139, 49, 145, 36, 10 }; 183 for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i) 184 { 185 t.Insert(a[i]); 186 } 187 t.Inorder(); 188 } 189 void TestBTree1() 190 { 191 BTree<int> t1; 192 int a[] = { 63, 75, 139, 49, 36, 10, 65, 78, 99,132,28,76,156,198}; 193 for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i) 194 { 195 t1.Insert(a[i]); 196 } 197 t1.Inorder(); 198 } 199 200 int main() 201 { 202 TestBTree1(); 203 system("pause"); 204 return 0; 205 }