my insert code is only correct when trying to add less than 10 nodes but if i try to add more than 10 nodes i get stack over flow error. im trying to make each node have a maximum of 10 children. this is what i came up with.
public class GeneralTree {
public GTN root;
int max=10;
public GeneralTree(GTN root){
this.root = root;
}
public GeneralTree(){}
public void insert(GTN node){
if(root== null){
root = node;
}
else
insertR(root,node);
}
private void insertR(GTN Root,GTN node){
if(root.children.isEmpty() || root.children.size()<max){
root.children.add(node);
}
else {
for(int k = 0;k<max;k++){
insertR(root.children.get(k),node);
}
}
}
my node class.
public class GTN {
public GTN parent;
public Comparable<String> key;
public ArrayList<GTN> children;
public GTN(Comparable key){
this.parent = null;
this.key=key;
this.children = new ArrayList<GTN>();
}
I think that your problem originates here:
private void insertR(GTN Root,GTN node){
Look at it closely
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.