बाइनरी खोजें ट्री दृश्य जावा में

वोट
1

हाय मैं वर्तमान में अपने प्रोजेक्ट (एल्गोरिथ्म विज़ुअलाइज़ेशन उपकरण) के परीक्षण के चरण कर रहा हूं। मैं अपने 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;}

मैं वास्तव में पूरी कक्षा पोस्ट कर सकते हैं, तो करने की आवश्यकता .. धन्यवाद है.मुझे के साथ मदद की जरूरत है!

24/03/2011 को 17:42
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


1 जवाब

वोट
0

बस कुछ नोट्स:

  1. आप जांच करने के लिए अशक्त पारित, तो आप एक एनपीई वहाँ पाने चाहते हैं।
  2. if( check(current) == 0) आदि -> आप एक बार की जाँच करनी चाहिए और फिर अगर (या यहां तक ​​कि एक स्विच) पर अमल

2 के लिए उदाहरण .:

 int result = check(current);
 switch(result) {
  case 0:
    //do whatever is appropriate
    break;
  case 1:
    //do whatever is appropriate
    break;
  case 2:
    //do whatever is appropriate
    break;
  default:
    //should never happen, either leave it or throw an exception if it ever happens
}

संपादित करें: असल में //, इस संपादित भूल जाते हैं, तो बस इस ऐसा नहीं होना चाहिए देखा था, लेकिन यह अभी भी एक अच्छा शैली नहीं है

आप भी अपने कोड में इस तरह की बातें हैं:

if (current.left != null) {
    current=current.left;
    prev.key=current.key;
    prev.left = current.left;
    this.repaint();
}
else {
    current=current.right; //this might be null
 ...
}

यदि current.leftरिक्त है और current.rightरिक्त है, currentबाद में अशक्त हो जाएगा।

24/03/2011 को 18:04
का स्रोत उपयोगकर्ता

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more