Die Überschneidung von 2 Binärbäumen löst einen Stapelüberlauffehler aus
Ich versuche, zwei Binärbäume zu kreuzen und einen neuen Binärbaum mit den Knoten zu erstellen, die gleich sind. Im Folgenden wird jedoch ein stackOverflow-Fehler angezeigt. Kann mir jemand helfen?
private OrderedSet<E> resultIntersect = new OrderedSet<E>();
public OrderedSet<E> intersection(OrderedSet<E> other) {
OrderedSet<E> result = new OrderedSet<E>();
if (other.root == null || root == null)
return result;
else if (height() == 0 && other.height() == 0
&& other.root.data.equals(root.data)) {
result.insert(root.data);
return result;
} else {
intersection(other, root, other.root);
result = resultIntersect;
}
return result;
}
private void intersection(OrderedSet<E> other, TreeNode root1,
TreeNode root2) {
if (root1 == root2) {
resultIntersect.insert(root1.data);
}
if (root1 == null || root2 == null) {
return;
}
intersection(other, root1.left, root2.left);
intersection(other, root1.right, root2.right);
}
Bearbeiten
Ich habe das Gefühl, dass dies dem näher kommt, was ich tun muss, aber ich erhalte trotzdem den Fehler.
private OrderedSet<E> resultIntersect = new OrderedSet<E>();
public OrderedSet<E> intersection(OrderedSet<E> other) {
OrderedSet<E> result = new OrderedSet<E>();
result = resultIntersect;
return result;
}
private void intersection(OrderedSet<E> other, TreeNode t) {
if (other.contains(t.data)) {
resultIntersect.insert(t.data);
}
if(t.left != null)
intersection(other, t.left);
if(t.right != null)
intersection(other, t.right);
}