簡體   English   中英

為什么刪除功能不能將節點從BST中刪除?

[英]Why won't my delete function delete the node out of the BST?

我花了數小時試圖弄清楚。 我已經檢查過,並且delete函數確實找到了該節點,但是當我嘗試通過將其設置為null或等於子節點來刪除它時,當我第二次將其打印出來時,它根本不會改變樹。 誰能幫助我弄清楚我做錯了什么,或者至少可以指導我解決該問題需要做些什么?

  class BST {
      Node root; 

      void BST () {
        root = new Node("B");
        insert (root, "A");
        insert (root, "D");
        insert (root, "C"); 
        inOrder (root);

        System.out.println (" ");
        delete (root, "D");
        //root.LEFT = null;
        inOrder (root);
      }

      void insert (Node n, String newKEY) {
        if (n.KEY.compareTo(newKEY) > 0) {

          if (n.LEFT == null) n.LEFT = new Node(newKEY);
          else if (n.LEFT != null && n.LEFT.KEY.compareTo(newKEY) < 0) n.LEFT = new Node(n.LEFT, newKEY, null);
          else insert (n.LEFT, newKEY);
        }

        if (n.KEY.compareTo(newKEY) < 0) {

          if (n.RIGHT == null) n.RIGHT = new Node(newKEY);
          else if (n.RIGHT != null && n.RIGHT.KEY.compareTo(newKEY) > 0) n.RIGHT = new Node(null, newKEY, n.RIGHT);
          else insert (n.RIGHT, newKEY);
        }

        else if (n.KEY.compareTo(newKEY) == 0) n.C++;    
      }

      void delete (Node n, String s) {
        // Visit, check if proper node, if so then delete
        if (n.KEY.compareTo(s) == 0) {
          System.out.println (n.KEY);
          // Deleting a node with no children
          if (n.LEFT == null && n.RIGHT == null) n = null; 

          //  Deleting a node with only left child
          else if (n.RIGHT == null) n = n.LEFT;

          //  Deleting a node with only right child
          else if (n.LEFT == null) n = n.RIGHT; 

          //  Deleting a node with two children
          else deleteNode_Two_Children (n, s);  
        } 
        // Left
        else if (n.KEY.compareTo(s) > 0) delete (n.LEFT, s);
        // Right 
        else if (n.KEY.compareTo(s) < 0) delete (n.RIGHT, s);  

      }

      boolean find (Node n, String s) {
        if (n.KEY.compareTo(s) > 0) {

          if (n.LEFT == null) return false;
          else if (n.LEFT != null && n.LEFT.KEY.compareTo(s) < 0) return false;
          else find (n.LEFT, s);
        }

        if (n.KEY.compareTo(s) < 0) {

          if (n.RIGHT == null) return false;
          else if (n.RIGHT != null && n.RIGHT.KEY.compareTo(s) > 0) return false;
          else find (n.RIGHT, s);
        }

        else if (n.KEY.compareTo(s) == 0) return true;   

        return false;
      }

      void deleteNode_Two_Children (Node n, String st) {
        Node s = getSuccessor(n);
        n = new Node (n.LEFT, s.KEY, s.C, n.RIGHT);
        delete (s, st);

      }

      Node getSuccessor (Node n) {
        Node temp = new Node(); 
        while (n.LEFT != null) {
          temp = n.LEFT; 
          n    = temp;
        }
        return temp; 
      }

      void inOrder (Node n) {   
        // Left
        if (n.LEFT != null) inOrder (n.LEFT);

        // Visit
        System.out.print (n.KEY + " - " + n.C + ", "); 

        // Right 
        if (n.RIGHT != null) inOrder (n.RIGHT);     
      }

      public static void main(String args[]){ 
        BST t = new BST();
        t.BST();
      }  
    }


    class Node {
      String       KEY;
      int          C;  
      Node         LEFT;
      Node         RIGHT;

      Node (String key) {
        KEY     =    key;  
        C       =    1;
        LEFT    =     null;
        RIGHT   =     null;   
      }

      Node (Node L, String key, Node R) {
        LEFT    =     L;
        RIGHT   =     R;
        KEY     =     key;  
        C       =     1;
      }

      Node (Node L, String key, int c, Node R) {
        LEFT    =     L;
        RIGHT   =     R;
        KEY     =     key;  
        C       =     c;
      }

      Node () {
        KEY     =    null;  
        C       =    0;
        LEFT    =     null;
        RIGHT   =     null;   
      }



      // If 'this' is less than 'other', a negative number will be returned, 
      // 0 if equal
      // Positive number if 'this' is greater. 
      int compare (Node other) {
        return this.KEY.compareTo(other.KEY);
      }

      boolean equals (Node other) {
        return this.KEY.equals(other.KEY);
      }
    }

問題是您假設將n設置為null將刪除該節點。 考慮以下:

Object x = new Object();

public void someMethod(Object o) {
    o = null;
}

這不會修改x Java是按值傳遞,其中o是對某些Object的引用。 您當然可以通過o的方法來修改o的內部:

o.setValue(1);

之所以可行,是因為o的值實際上是堆上的某個地址,不會被修改。 您不能覆蓋o本身(例如,你不能將它設置為空或一個new Object() 為了刪除節點,您必須找到該節點的父節點,並將其設置為左或右子節點(無論您要刪除哪個節點)並將其設置為null。 另外,如果該節點有子節點,則必須確保不會僅由於刪除其父節點而將其刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM