btreeinsert-java

btreeinsert-java,java,b-tree,Java,B Tree,我已经用java为B树编写了一个插入代码,但是我的树没有正确初始化,我不知道问题出在哪里 例如-输入:ABCDGHKMRWZ 想要的产出- D / \ / \ / \ B HM / \ /|\ / \ / | \ A C G K RWZ D是B和HM的父母,HM和B是D的孩子,依此类推 但我收到了以下输出: D

我已经用java为B树编写了一个插入代码,但是我的树没有正确初始化,我不知道问题出在哪里

例如-输入:ABCDGHKMRWZ

想要的产出-

        D
      /   \
     /     \        
    /       \
   B        HM
  / \       /|\
 /   \     / | \
A     C   G  K  RWZ
D是B和HM的父母,HM和B是D的孩子,依此类推

但我收到了以下输出:

          D
         / \
        /   \
       /     \
      /       \        
     /         \
    B           HM
   /|\ \        /|\
  / | \ \      / | \    
 A  C G  K    G  K  RWZ
当K和G是B和HM的子代时,B是G的父代,HM是K的父代

这是我的密码:

public void insert(String key){
    //this method finds the node to be inserted as 
    //it goes through this starting at root node.
    BTreeNode r = this.root;

    //if it is full
    if(r.count == 2*order - 1)
    {
        //create a new empty node
        BTreeNode s = new BTreeNode(order,null);

        s.leaf = false;
        s.count = 0;
        s.child[0] = r;
        this.root = s; 

        split(s,0,r); //split root

        nonfullInsert(s, key); //call insert method
    }
    else
        nonfullInsert(r,key); //if its not full just insert it
}

public void split(BTreeNode s, int i, BTreeNode r){
    BTreeNode z = new BTreeNode(this.order,null);
    z.leaf = r.leaf; //set boolean to be the same as y

    for(int j = 0; j < this.order - 1; j++){
        z.key[j] = r.key[j+this.order];
        z.count++;
    }

    // if not leaf we have to reassign child nodes.
    if(!r.leaf){
        for(int k = 0; k < this.order; k++){
            z.child[k] = r.child[k+this.order];
        }
    }

    r.count = this.order - 1; //new size of y

    // rearranging the child nodes
    for(int j = s.count ; j> i ; j--){
        s.child[j+1] = s.child[j]; //shift children of x
    }
    s.child[i+1] = z;

    for(int j = s.count; j> i; j--){
        s.key[j + 1] = s.key[j]; // shift keys
    }

    //push the value up into the root.
    s.key[i] = r.key[this.order-1];
    r.key[this.order-1 ] = "";

    for(int j = 0; j < this.order - 1; j++)
        r.key[j + this.order] = ""; //'delete' old values

    s.count++;  //increase count of keys in s
    r.parent=s;
    z.parent=s;
}

public void nonfullInsert(BTreeNode s, String key){
    int i = s.count; //i is number of keys in node x

    if(s.leaf){
        while(i >= 1 && key.compareTo(s.key[i-1]) < 0){
            //shift values to make room for the new one
            s.key[i] = s.key[i-1]; 
            i--;
        }

        s.key[i] = key; //assign the value to the node
        s.count++;
    }

    else{
        int j = 0;
        while(j < s.count  && key.compareTo(s.key[j]) > 0)          
            j++;

        if(s.child[j].count == order*2 - 1){
            split(s,j,s.child[j]);

            if(key.compareTo(s.key[j]) > 0){
                j++;
            }
        }
        nonfullInsert(s.child[j],key);
    }
}
public void插入(字符串键){
//此方法查找要作为插入的节点
//它从根节点开始经历这个过程。
b绿色节点r=this.root;
//如果满了
if(r.count==2*顺序-1)
{
//创建一个新的空节点
BTreeNode s=新的BTreeNode(顺序,空);
s、 叶=假;
s、 计数=0;
s、 child[0]=r;
this.root=s;
拆分(s,0,r);//拆分根
nonfullInsert(s,key);//调用insert方法
}
其他的
nonfullInsert(r,key);//如果不是满的,就插入它
}
公共无效拆分(B绿色节点s、int i、B绿色节点r){
BTreeNode z=新的BTreeNode(this.order,null);
z、 leaf=r.leaf;//将布尔值设置为与y相同
对于(int j=0;ji;j--){
s、 child[j+1]=s.child[j];//移位x的子项
}
s、 子[i+1]=z;
对于(int j=s.count;j>i;j--){
s、 键[j+1]=s.key[j];//移位键
}
//将该值向上推到根中。
s、 key[i]=r.key[this.order-1];
r、 键[this.order-1]=“”;
对于(int j=0;j=1&&key.compareTo(s.key[i-1])<0){
//移动值以为新值腾出空间
s、 密钥[i]=s.key[i-1];
我--;
}
s、 key[i]=key;//将值分配给节点
s、 计数++;
}
否则{
int j=0;
而(j0)
j++;
如果(s.child[j].count==顺序*2-1){
拆分(s,j,s.child[j]);
如果(键比较)(s键[j])>0{
j++;
}
}
非完整插入(s.child[j],键);
}
}

非常感谢你的帮助

尝试通过IDE中的调试器运行此功能?当然!如果不先自己调试的话,我是不会发帖的!!然后,你应该能够找到G插入节点B的点。。。如果不是,我们需要一个运行此代码的方法,而不是树类的3个上下文外方法