#include "stdafx.h"
#include<iostream>
using namespace std;
#define T 3
struct B_tree_node{//B树的节点
bool is_leaf;
int n;
int *key;
struct B_tree_node *c;
B_tree_node(){
n = 0;
is_leaf = 1;
}
};
B_tree_node B_tree_create_node(){
B_tree_node tem;
tem.c = new B_tree_node[2*T];
tem.key = new int[2*T-1];
for(int i = 0;i < 2*T-1;++i)tem.c[i];
return tem;
}
void B_tree_split_child(B_tree_node &x,int i){
B_tree_node z = B_tree_create_node();
z.n = T-1;
z.is_leaf = x.c[i].is_leaf;
if(x.n == 0){
x.n = 1;
for(int j = 0;j <= T-2;++j){
z.key[j] = x.c[i].key[j+T];
if(!(x.c[i].is_leaf)){
z.c[j+1] = x.c[i].c[j+T+1];
}
}
if(!(x.c[i].is_leaf)){
z.c[0] = x.c[i].c[T];
}
x.key[0] = x.c[0].key[T-1];
x.c[i].n = T-1;
x.c[i+1] = z;
x.is_leaf = 0;
}else{
for(int j = x.n-1;j >= i;--j){
x.key[j+1] = x.key[j];
x.c[j+2] = x.c[j+1];
}
x.key[i] = x.c[i].key[T-1];
x.n = x.n + 1;
x.c[i].n = T-1;
for(int j = 0;j <= T-2;++j){
z.key[j] = x.c[i].key[j+T];
if(!(x.c[i].is_leaf)){
z.c[j+1] = x.c[i].c[j+T+1];
}
}
if(!(x.c[i].is_leaf)){
z.c[0] = x.c[i].c[T];
}
x.c[i+1] = z;
}
}
void B_tree_search(B_tree_node root,int target,B_tree_node &tem,int &i){
int j = 0;
while(j < root.n&&root.key[j] < target){
++j;
}
if(root.is_leaf){
if(root.key[j] != target){
cout<<"value not found!";
}else{
tem = root;
i = j;
}
}else{
if(root.key[j] == target){
tem = root;
i = j;
}else{
B_tree_search(root.c[j],target,tem,i);
}
}
}
void B_tree_insert_nonfull(B_tree_node &x,int k){
int i;
if(x.n)
i = x.n-1;
else
i = 0;
if(x.is_leaf){
if(x.n == 0){
x.n += 1;
x.key[0] = k;
}
else{
if(k < x.key[i]){
while(i>=0&&x.key[i] > k){
x.key[i+1] = x.key[i];
--i;
}
}
++i;
x.key[i] = k;
x.n = x.n+1;
}
}else{
while(i >= 0&&x.key[i] > k){
--i;
}
++i;
if(x.c[i].n == 2*T-1){
B_tree_split_child(x,i);
if(k > x.key[i]){
++i;
}
}
B_tree_insert_nonfull(x.c[i],k);
}
}
void B_tree_insert(B_tree_node &root,int k){
B_tree_node r = root;
int count = 0;
if(r.n == 2*T-1){
B_tree_node tem = B_tree_create_node();
tem.is_leaf = 0;
tem.n = 0;
tem.c[0] = r;
B_tree_split_child(tem,0);
B_tree_insert_nonfull(tem,k);
root = tem;
}
else{
B_tree_insert_nonfull(root,k);
}
}
void B_tree_print(B_tree_node root,int height){
if(height == 0){
cout<<root.n;
cout<<"[";
for(int i = 0;i <= root.n-1;++i){
cout<<root.key[i]<<",";
}
cout<<"]";
}else if(height > 0){
--height;
for(int i = 0;i <= root.n;++i){
B_tree_print(root.c[i],height);
}
}
}
void B_tree_print_m(int height,B_tree_node root){
for(int i = 0;i < height;++i){
B_tree_print(root,i);
cout<<endl;
cout<<endl;
}
}
void B_tree_merge_(B_tree_node ){
}
B_tree_node B_tree_merge(B_tree_node &m,B_tree_node y){
B_tree_node x = m;
if(x.is_leaf){
for(int i = 0;i <= y.n-1;++i){
x.key[x.n+i] = y.key[i];
}
x.n = x.n + y.n;
return x;
}else{
if(x.c[T-1].n > T-1){
x.key[T-1] = x.c[T-1].key[x.c[T-1].n-1];
x.c[T] = y.c[0];
x.n = 2*T-1;
x.c[T-1].n = x.c[T-1].n-1;
for(int i = 0;i < T-1;++i){
if(!x.is_leaf){
x.c[i+T+1] = y.c[i+1];
}
x.key[T+i] = y.key[i];
}
return x;
}else if(y.c[0].n > T-1){
x.key[T-1] = y.c[0].key[0];
x.n = 2*T-1;
for(int i = 0;i < y.c[0].n-2;++i){
if(!y.c[0].is_leaf){
y.c[0].c[i] = y.c[0].c[i+1];
}
y.c[0].key[i] = y.c[0].key[i+1];
}
y.c[0].c[T-2] = y.c[0].c[T-1];
y.c[0].n = y.c[0].n - 1;
x.c[T] = y.c[0];
for(int i = 0;i <= T-2;++i){
if(!y.c[0].is_leaf){
x.c[T+i+1] = y.c[i+1];
}
x.key[T+i] = y.key[i];
}
return x;
}
else{
x.c[T-1] = B_tree_merge(x.c[T-1],y.c[0]);
for(int i = 0;i <= T-2;++i){
if(!y.c[0].is_leaf){
x.c[T+i] = y.c[i+1];
}
x.key[T+i-1] = y.key[i];
}
x.n = 2*T-2;
return x;
}
}
}
void B_tree_delete(B_tree_node &x,int i){
if(x.is_leaf){
for(int j = i;j <= x.n-2;++j){
x.key[j] = x.key[j+1];
}
x.n = x.n - 1;
}
else{
if(x.c[i].n > T-1){
x.key[i] = x.c[i].key[x.c[i].n-1];
B_tree_delete(x.c[i],x.c[i].n - 1);
}
else if(x.c[i+1].n > T-1){
x.key[i] = x.c[i+1].key[0];
B_tree_delete(x.c[i+1],0);
}
else{
x.c[i] = B_tree_merge(x.c[i],x.c[i+1]);
for(int j = i;j <= x.n - 2;++j){
x.key[j] = x.key[j+1];
x.c[j+1] = x.c[j+2];
}
x.n = x.n-1;
}
}
}
inline int _tmain(int argc, _TCHAR* argv[]){
B_tree_node root = B_tree_create_node();
B_tree_node root1 = B_tree_create_node();
for(int i = 2;i <= 32;++i)B_tree_insert(root,i);
B_tree_node tem1 = B_tree_create_node();
int tem11;
B_tree_print_m(3,root);
B_tree_search(root,19,tem1,tem11);
B_tree_delete(tem1,tem11);
B_tree_print_m(3,root);
//cout<<tem1.n<<endl;
cout<<root.c[0].n;
//B_tree_print(root,1);
cout<<endl;
while(1);
return 0;
}