हाय मैं वर्तमान में अपने प्रोजेक्ट (एल्गोरिथ्म विज़ुअलाइज़ेशन उपकरण) के परीक्षण के चरण कर रहा हूं। मैं अपने 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 के लिए मैं उपयोगकर्ता या 9, 6 और 5. (डेल 8 करने की कोशिश) मुझे लगता है कि समस्या 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;}
मैं वास्तव में पूरी कक्षा पोस्ट कर सकते हैं, तो करने की आवश्यकता .. धन्यवाद है.मुझे के साथ मदद की जरूरत है!













