/*
* BMT.h
*
* Created on: Nov 2, 2015
* Author: chris
*/
#ifndef BMT_H_
#define BMT_H_
#include<iostream>
#define m 3
struct Record{
//empty.
};
typedef int KeyType;
typedef struct BMTNode{
int keynum;
BMTNode * parent;
KeyType key[m+1];
BMTNode * ptr[m+1];
BMTNode(): keynum(0), parent(NULL) {
for(int i = 0; i <= m; ++i) {
key[i] = 0;
ptr[i] = NULL;
}//endfor
}//endctor
}*BMT;
bool BMTSearchKey(BMT & T, KeyType K, BMTNode*& recNode, int &recIdx);
bool BMTInsertKey(BMT & T, KeyType K);
bool BMTDeleteKey(BMT & T, KeyType K);
void BMTDestroy(BMT & T);
void BMTWalkThrough(BMT & T);
#endif /* BMT_H_ */
/*
* BMT.cpp
*
* Created on: Nov 2, 2015
* Author: chris
*/
#include"BMT.h"
#include<iostream>
#include<cstdlib>
using namespace std;
int BMTSearch(BMT & T, KeyType K)
{
int i = 0;
for(int j = 1; j <= T->keynum; ++j)
if(T->key[j] <= K) i = j;
return i;
}
bool BMTSearchKey(BMT & T, KeyType K, BMTNode*& recNode, int &recIdx)
{
recIdx = 0;
recNode = NULL;
BMTNode * curNode = T;
while(curNode) {
// search
recIdx = BMTSearch(curNode, K);
if (recIdx > 0 && curNode->key[recIdx] == K) {
recNode = curNode;
return true; //found
} else {
recNode = curNode; // q: the parent of p
curNode = curNode->ptr[recIdx]; // p->key[i]: the last key lt K
} //endif
} // endw
return false;
}
void BMTInsert(BMT & T, int i, KeyType K, BMTNode* rhs)
{
// T: current Tree Node;
// i: points to the last elem that's le K;
// K: the key to insert.
// insert K to T->key[i+1] and rhs to T->ptr[i+1],
// move backward
for(int j = T->keynum; j >= i+1; --j) {
T->key[j+1] = T->key[j];
T->ptr[j+1] = T->ptr[j];
}//endfor j
T->key[i+1] = K;
T->ptr[i+1] = rhs;
++T->keynum;
}
bool BMTSplit(BMT & T, int s, BMT& rhs, KeyType& midK)
{
// split T at s, with the right hand side stored in rhs
// return key[s] by K.
rhs = new BMTNode;
if(!rhs) return false;
rhs->parent = T->parent;
// take out the mid node.
midK = T->key[s];
T->key[s]