The B-Tree of a M-order (M>2), is a balanced m-Path balanced search tree, which can be either empty tree or satisfy the nature:
1. Root node has at least two children
2. Each non-root node has [, M] Children
3. Each non-root node has a [ -1,m-1] keyword and is in ascending order
4. The value of the child node between Key[i] and key[i+1] is between Key[i], key[i+1]
5. All leaf nodes are on the same floor
PS: Is rounding up
#pragma oncetemplate<class k, int m = 3>struct btreenode{k _keys [m];// key word group btreenode<k, m>* _subs[m + 1];// child array size_t _size;// Number of keywords btreenode<k, m>* _parent;// father Btreenode (): _size (0), _parent (NULL) {for (size_t i = 0; i < m + 1; ++i) {_subs[i] = NULL ;}}}; template<class k, class v>struct pair{k _first; v _second; Pair (Const k& k = k (), const v& v = v ()): _first (K), _second (v) {}};template<class k, int m = 3>class btree{typedef Btreenode<k, m> node;public:btree (): _root (NULL) {}pair<node*, int> find (const k& key) {node* parent = null; node* cur = _root;while (cur) {int i = 0;while (i&nbSp;< cur->_size && cur->_keys[i] < key) {++i;} if (Cur->_keys[i] == key) {return pair<node*, int> (cur, i);} Parent = cur;cur = cur->_subs[i];} Return pair<node*, int> (parent, -1);} Bool insert (Const k& key) {if (_root == null) {_root = new node ; _root->_keys[0] = key;++_root->_size;return true;} Pair<node*, int> ret = find (Key);if (ret._second != -1) {return false;} k k = key; node* cur = ret._first; node* sub = null;// Inserts a kwhile (1) {_insertkey (cur, k, sub);if in the CUR node ( cur->_size < m) {return true;} Split int boundary = m / 2; Node* tmp = new node;size_t index = 0;size_t size = cur-> _size;//&nbSP; copy keyfor (int i = boundary + 1; i < size; ++i) {tmp- >_keys[index++] = cur->_keys[i];tmp->_size++;cur->_size--;} Copy child node index = 0;for (int i = boundary + 1; i <= size; ++i) {tmp->_subs[index] = cur->_subs[i];if (Tmp->_subs[index]) tmp-> _subs[index]->_parent = tmp;++index;} k = cur->_keys[boundary];cur->_size--;// no Father if (Cur->_parent == null) { _root = new node;_root->_keys[0] = k;_root->_subs[0] = cur;_root-> _subs[1] = tmp;_root->_size = 1;tmp->_parent = _root;cur->_parent = _root;return true;} Cur = cur->_parent;sub = tmp;}} Void _insertkey (node* cur, const k& k, node* sub) {int i = cur->_size - 1;while (i >= 0) {if (cur->_keys[i] > k) {cur->_keys[i + 1] = cur->_keys[i];cur->_subs[i + 2] = cur->_subs[i + 1 ];--i;} Else{break;}} cur->_keys[i + 1] = k;cur->_subs[i + 2] = sub;if (sub) {sub- >_parent = cur;} cur->_size++;} Void inorder () {_inorder (_root); Cout << endl;} Void _inorder (Node* root) {if (root == null) {return;} for (size_t i = 0; i < root->_size; ++i) {_InOrder (root->_subs [i]); cout << root->_keys[i] << " ";} _inorder (Root->_subs[root->_size]);} protected:node* _root;}; Void testbtree1 () {btree<int, 1024> t1;int a[] = { 53, 75, 139, 49, 145, 36, 101 };for (int i = 0; i < sizeof (a) / sizeof (a[0]); ++i) {t1. Insert (A[i]);} T1. Inorder ();}
This article is from the "Wood Man" blog, please make sure to keep this source http://10324228.blog.51cto.com/10314228/1771019
Btree (b-Tree)---C + +