Guten Tag,
ich möchte Verwandtschaftsbeziehungen darstellen bzw. später einen Baum. Habe nun etwas getestet ob es funktioniert und sitze nun schon längerer Zeit vor einer Fehlerausgabe welche ich nicht lösen kann.
Der Fehler taucht in dieser Zeile auf:
Vielleicht habe ich etwas mit dem 2D - Array durcheinandergebracht. Wenn ich schreiben würde
hätte dann dieses Array Platz für 10 Elemente oder nicht?
Ich hoffe es kann mir jemand helfen!
Danke im Voraus!
ich möchte Verwandtschaftsbeziehungen darstellen bzw. später einen Baum. Habe nun etwas getestet ob es funktioniert und sitze nun schon längerer Zeit vor einer Fehlerausgabe welche ich nicht lösen kann.
Der Fehler taucht in dieser Zeile auf:
Java:
hauptebenen[1][i].einfügen(hauptebenen[1], hauptebenen[2],anzparentnode[i]);
Vielleicht habe ich etwas mit dem 2D - Array durcheinandergebracht. Wenn ich schreiben würde
Java:
hauptebenen[1]=new node [10];
Java:
package algorithmen;
import java.util.Arrays;
class Node {
private String data;
private Node[] children = new Node[0];
private Node parent = null;
public Node(String data) {
this(data, null);
}
public Node(String data, Node child) {
this.data = data;
if (child != null) {
this.addChild(child);
}
}
public void addChild(Node node) {
node.setParent(this);
Node[] newChildren = copyArrayWithOneExtra(children);
newChildren[newChildren.length - 1] = node;
this.children = newChildren;
}
public void printChildren() {
Node[] children = getChildren();
Arrays.stream(children)
.forEach(Node::printNode);
}
public void printNode() {
int ancestorCount = this.getAncestorCount();
while (ancestorCount > 0) {
System.out.print("\t");
ancestorCount--;
}
System.out.println(this.getData());
}
public void printNextGeneration() {
Arrays.stream(children)
.peek(Node::printChildren)
.forEach(Node::printNextGeneration);
}
public static int zufall()
{
int Max=5;
int Min=2;
int x = Min + (int)(Math.random() * ((Max - Min) + 1));
return x;
}
public void einfügen(Node []elter,Node[] kind,int anzkinder)
{
//System.out.println("Anzahl Knoten"+elter.length);
String[] zahlen= {"eins","zwei","drei","vier","fünf"};
int zaehler=0;
for(int k=0;k<anzkinder;k++) {
kind [k]=new Node(zahlen[k]);
elter[0].addChild(kind[k]);
System.out.println("Anzahl Kinderknoten"+ anzkinder);// Ermittlung der Anzahl der Kinderknoten
zaehler++;
}
System.out.println(zaehler+"abgeschlossen");
}
public static int[] getzufall(int laenge)
{
int anzahl=laenge;
int[] anzkinder =new int [anzahl];
for(int i=0;i<anzahl;i++)
anzkinder[i]=zufall();
return anzkinder;
}
public static int [] kopierarray(int[] a)
{
int summe=0;
for(int i=0;i<a.length;i++)
summe+=a[i];
int neu[]=new int [summe];
return neu;
}
public static int summearray(int []a)
{
int summe=0;
for(int i=0;i<a.length;i++)
summe+=a[i];
return summe;
}
public static void main(String[] args) {
int tiefe=5;
Node [][]hauptebenen= new Node[tiefe][];
System.out.println("Die Tiefe beträgt:"+" "+tiefe);
Node[] root = new Node[1];
root[0]=new Node("1");
int[] anzparentnode=new int [1];
anzparentnode=getzufall(root.length);
hauptebenen[0]=new Node[summearray(anzparentnode)];
System.out.println(anzparentnode[0]);
root[0].einfügen(root, hauptebenen[0],anzparentnode[0]);
anzparentnode=kopierarray(anzparentnode);
anzparentnode=getzufall(anzparentnode.length);
hauptebenen[1]=new Node[summearray(anzparentnode)];
for(int i=0;i<anzparentnode.length;i++)
hauptebenen[0][i].einfügen(hauptebenen[0], hauptebenen[1],anzparentnode[i]);
anzparentnode=kopierarray(anzparentnode);
anzparentnode=getzufall(anzparentnode.length);
hauptebenen[2]=new Node[summearray(anzparentnode)];
for(int i=0;i<anzparentnode.length;i++)
hauptebenen[1][i].einfügen(hauptebenen[1], hauptebenen[2],anzparentnode[i]);
root[0].printChildren();
System.out.println(root.length);
hauptebenen[0][0].printChildren();
System.out.println(hauptebenen[0].length);
hauptebenen[1][0].printChildren();
System.out.println(hauptebenen[1].length);
}
private String getData() {
return this.data;
}
private Node[] getChildren() {
return this.children;
}
private int getChildCount() {
return children.length;
}
private void setParent(Node parent) {
this.parent = parent;
}
private Node getParent() {
return parent;
}
private int getAncestorCount() {
Node parent = getParent();
int ancestorCount = 0;
while (parent != null) {
ancestorCount++;
parent = parent.getParent();
}
return ancestorCount;
}
private static Node[] copyArrayWithOneExtra(Node[] children) {
Node[] newChildren = new Node[children.length + 1];
System.arraycopy(children, 0, newChildren, 0, children.length);
return newChildren;
}
}
Ich hoffe es kann mir jemand helfen!
Danke im Voraus!