B树的每个节点需要保存过个值,就需要采用数组,这里使用的是MFC的CArray来保存。经过测试如果调用CArray的SetSize方法预先分配内存,int型100W数量级耗时900ms左右,性能足够我用的。下面直接附上源码。 // BTree.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "afxtempl.h" #include <iostream> using namespace std; #define NKEY 3 /* BTNode class */ class BTNode{ public: BTNode(); ~BTNode(); public: void SetLeaf(bool bleaf); BOOL IfLeaf(){ return m_leaf; } public: int m_num; //记录该节点有多少个元素 CArray<int ,int> m_keyAry; CArray<BTNode*,BTNode*> m_subTree; private: bool m_leaf; }; /* @px: 非满的内结点 @py: 满的子结点 py = px->m_subTree[ipos]; */ void BTree_Split_Child( BTNode* px, int ipos, BTNode* py); /* @proot: 指向根结点,该指针可能在函数中被修改,故采用指针的指针 */ void BTree_Insert( BTNode** proot, int key); void BTree_Insert_Nonfull( BTNode* pbt, int key); void LayerOrder(BTNode* proot); int _tmain(int argc, _TCHAR* argv[]) {