Γεια σου Είμαι σήμερα κάνει το στάδιο των δοκιμών του έργου μου (Αλγόριθμος Visualization Tool). Έχω πάρει ένα πρόβλημα με τη διαγραφή μέθοδο της BST μου.
public boolean delete(String key) {
boolean deleted = true;
boolean finished=false;
BNode current = root;
BNode prev = null;
while (!finished) {
if (key.compareTo(current.key) > 0) {
prev = current;
current = current.right;
this.repaint();
}
else if (key.compareTo(current.key) < 0) {
prev = current;
current = current.left;
this.repaint();
}
else if (key.compareTo(current.key) == 0) {
finished=true;
this.repaint();
}
}
if (check(current) == 0) {
if(current==root)
{
root=null;
xPos=400;
yPos=60;
this.repaint();
}
else
{
if (current.key.compareTo(prev.key) > 0) {
prev.right = null;
this.repaint();
}
else if(current.key.compareTo(prev.key) < 0) {
prev.left = null;
this.repaint();
}
}
}
else if (check(current) == 1) {
if(current==root)
{
prev=current;
if (current.left != null) {
current=current.left;
prev.key=current.key;
prev.left = current.left;
this.repaint();
}
else {
current=current.right;
prev.key=current.key;
prev.right = current.right;
this.repaint();
}
}
else
{
if (current.key.compareTo(prev.key) > 0) {
if (current.left != null) {
prev.right = current.left;
this.repaint();
}
else {
prev.right = current.right;
this.repaint();
}
}
else if(current.key.compareTo(prev.key) < 0) {
if (current.left != null) {
prev.left = current.left;
this.repaint();
}
else {
prev.left = current.right;
this.repaint();
}
}
}
}
else if (check(current) == 2) {
BNode temp = inord(current);
if(current==root)
{
root.key=temp.key;
this.repaint();
}
else
{
if (current.key.compareTo(prev.key) > 0) {
prev.right.key = temp.key;
this.repaint();
}
else {
prev.left.key = temp.key;
this.repaint(0);
}
}
}
return deleted;}
Ο κώδικας για την ίδια BST κατηγορία είναι πολύ μεγαλύτερη. Τα πάντα λειτουργούν καλά εκτός από το ότι όταν προσπαθώ να διαγράψω ένα κόμβο χωρίς παιδί, έχω μια εξαίρεση nullpointer όταν χρησιμοποιώ για παράδειγμα, 9 και 10 ως είσοδος (προσπαθήστε να ντελ 10) ή 5 και 12 (προσπαθήστε να ντελ 12), αλλά ποτέ δεν αν χρήστη, για παράδειγμα 4 και 8 (προσπαθήστε να ντελ 8) ή 9, 6 και 5. νομίζω ότι το πρόβλημα είναι με compareTo.
int check(BNode a) {
int ret;
if ( (a.left != null) && (a.right != null)) {
ret = 2;
}
else if ( (a.left == null) && (a.right == null)) {
ret = 0;
}
else {
ret = 1;
}
return ret;}
Θα πρέπει πραγματικά να βοηθήσει με this.I μπορείτε να δημοσιεύσετε ολόκληρη την τάξη, αν χρειάζεται να .. Σας ευχαριστούμε!













