- packagecom.eshore.sweetop.exdataframe;
- publicclassBTree{
- privateBNoderoot;
- privateintmin;
- publicBTree(){
- BNodenode=newBNode();
- node.setLeaf(true);
- node.setCount(0);
- diskWrite(node);
- root=node;
- }
- publicvoidinsert(intk){
- BNoder=root;
- if(r.getCount()==(2*min-1)){
- BNodenode=newBNode();
- root=node;
- node.setLeaf(false);
- node.setCount(0);
- node.getChildren().add(0,root);
- split(node,0,r);
- insertNonFull(node,k);
- }else{
- insertNonFull(r,k);
- }
- }
- publicvoidsplit(BNodenodex,inti,BNodenodey){
- BNodenodez=newBNode();
- nodez.setLeaf(nodey.getLeaf());
- nodez.setCount(min-1);
- for(intj=0;j<min-1;j++){
- nodez.getKeys().add(j,nodey.getKeys().get(min+j));
- }
- if(!nodey.getLeaf()){
- for(intj=0;j<min;j++){
- nodez.getChildren().add(j,nodey.getChildren().get(min+j));
- }
- }
- nodey.setCount(min-1);
- for(intj=nodex.getCount();j>i;j--){
- if(j==nodex.getCount()){
- nodex.getChildren().add(j+1,nodex.getChildren().get(j));
- }else{
- nodex.getChildren().set(j+1,nodex.getChildren().get(j));
- }
- }
- nodex.getChildren().set(i+1,nodez);
- for(intj=nodex.getCount()-1;j>i;j++){
- if(j==nodex.getCount()-1){
- nodex.getKeys().add(j+1,nodex.getKeys().get(j));
- }else{
- nodex.getKeys().set(j+1,nodex.getKeys().get(j));
- }
- }
- nodex.getKeys().set(i,nodey.getKeys().get(min-1));
- nodex.setCount(nodex.getCount()+1);
- diskWrite(nodey);
- diskWrite(nodez);
- diskWrite(nodex);
- }
- publicvoidinsertNonFull(BNodenode,intk){
- inti=node.getCount()-1;
- if(node.getLeaf()){
- while(i>=0&&k<=node.getKeys().get(i)){
- if(i==node.getCount()){
- node.getKeys().add(i+1,node.getKeys().get(i));
- }else{
- node.getKeys().add(i+1,node.getKeys().get(i));
- }
- i--;
- }
- if(i==node.getCount()-1){
- node.getKeys().add(i+1,Integer.valueOf(i));
- }else{
- node.getKeys().set(i+1,Integer.valueOf(i));
- }
- node.setCount(node.getCount()+1);
- diskWrite(node);
- }else{
- while(i>=0&&k<=node.getKeys().get(i)){
- i--;
- }
- i++;
- diskRead(node.getChildren().get(i));
- if(node.getChildren().get(i).getCount()==(2*min-1)){
- split(node,i,node.getChildren().get(i));
- if(Integer.valueOf(k)>node.getKeys().get(i)){
- i++;
- }
- }
- insertNonFull(node.getChildren().get(i),k);
- }
- }
- publicvoiddiskWrite(BNodenode){
- }
- publicBNodediskRead(BNodebt){
- returnnull;
- }
- publicstaticvoidmain(String[]args){
- BTreebt=newBTree();
- bt.insert(3);
- }
- }