Hallo ich habe folgende Klasse und möchte in meiner arrayStorage Methode ein Array mit allen werten im Baum returnen.
Weiß jemand wo mein Fehler ist oder welche Verbesserungen ich einbauen kann?
Weiß jemand wo mein Fehler ist oder welche Verbesserungen ich einbauen kann?
Code:
class Node {
public int v;
public Tree l, r;
public Node(int v) {
this.v = v;
this.l = new Tree();
this.r = new Tree();
}
}
class Tree {
public Node root = null;
public boolean isEmpty() {
return root == null;
}
public void insert(int x) {
if (isEmpty())
root = new Node(x);
else if (x < root.v)
root.l.insert(x);
else
root.r.insert(x);
}
public String toString() {
if (isEmpty())
return "";
else
return "" + root.r + root.l + ";" + root.v;
}
/**
*
* @return Anzahl der Knoten im Baum
*/
public int countNode() {
if (this.root == null) {
return 0;
} else {
int count = 1;
count += root.l.countNode();
count += root.r.countNode();
return count;
}
}
/**
*
* @return alle Knoten im Baum in einem Array
*/
public int[] arrayStorage() {
int[] a = new int[countNode()];
int count = 1;
if(root != null) {
root.l.arrayStorage();
root.r.arrayStorage();
a[count] = root.v;
count++;
return a;
}
else {
a = null;
return a;
}
}
}