【问题标题】:Why won't my delete function delete the node out of the BST?为什么我的删除功能不会将节点从 BST 中删除?
【发布时间】:2013-10-18 22:59:00
【问题描述】:

我花了好几个小时试图弄清楚。我已经检查过,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);
      }
    }

【问题讨论】:

  • 你能提供一个简单的单元测试来演示这个问题吗?当您在 IDE 的调试器中单步执行代码时,您会看到什么?你真的需要这么多构造函数吗?
  • 欢迎来到 DrJava。工作目录是 C:\Users\Willy\Desktop\Assign2 > 运行 BST A - 1, B - 1, C - 1, D - 1, D A - 1, B - 1, C - 1, D - 1, >
  • 我添加它们以防万一我需要它们,但我打算稍后删除它们。
  • 这是我正在使用的 IDE 的名称。
  • 我假设 DrJava 有一个调试器。

标签: java binary-search-tree


【解决方案1】:

问题是您假设将n 设置为null 将删除该节点。考虑以下几点:

Object x = new Object();

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

这不会修改x。 Java 是按值传递的,其中o 是对某些Object 的引用。你当然可以通过o的方法修改o的内部结构:

o.setValue(1);

这是可行的,因为o 的值实际上是堆上的某个地址,没有被修改。您不能覆盖o 本身(例如,您不能将其设置为null 或new Object())。为了让您删除一个节点,您必须找到该节点的父节点并将其设置为左子或右子(无论您要删除哪个)并将其设置为空。此外,如果该节点有子节点,您必须确保它们不会仅仅因为它们的父节点被删除而被删除。

【讨论】:

  • 好吧,我想它可能是这样的,但我不想删除我所有的代码,然后才意识到这是别的东西。谢谢,我想我知道我现在需要做什么了。
猜你喜欢
  • 2019-04-03
  • 2020-05-23
  • 1970-01-01
  • 1970-01-01
  • 2021-04-06
  • 1970-01-01
  • 2021-10-23
  • 2014-05-26
相关资源
最近更新 更多