为什么我的删除功能不能将节点从BST中删除?

威勒·莫兰德(Willeh Moreland)

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

问题是您假设将设置nnull将删除该节点。考虑以下:

Object x = new Object();

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

这不会修改xJava是按值传递的,其中o是对some的引用Object您当然可以修改o通过o的方法的内部

o.setValue(1);

之所以起作用,o因为的值实际上是堆上的某个地址,没有被修改。您不能覆盖o自身(例如,不能将其设置为null或new Object())。为了删除节点,必须找到该节点的父节点,并将其设置为左子节点或右子节点(无论您要删除的是哪个子节点),并将其设置为null。另外,如果该节点有子节点,则必须确保不会仅由于删除其父节点而将其删除。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么列表删除功能不能删除空格?

为什么我的 Javascript 功能不能正常工作?

为什么我的功能不能放在桌子的一个单元格中?

为什么多层异步功能不能捕获节点最低级别抛出的错误?

VBA:联合功能不能删除不连续的列

为什么我的功能不能正常工作两次?

为什么我的Pandas的“应用”功能不能引用多个列?

为什么我的堆排序功能不能按预期工作?

为什么这个功能不能退出?

为什么RHR功能不能超载?

为什么这个功能不能正常工作?

为什么这个功能不能正常工作?

为什么Linux中的“系统”功能不能运行此shellscript?

在BST中删除节点功能

为什么不能将“ this”设置为null以删除Javascript中的树节点?

为什么我的Wordpress简码功能不能用于我的自定义帖子类型?

为什么我不能将这些环境变量永久删除到Ubuntu 18.04系统中?

为什么我的删除功能不起作用?

为什么Laravel中间件中的智能不能识别用户模型中的功能?

在Clojure中,为什么“某些”功能不能在集合上始终如一地工作?

为什么我在 R Shiny 中删除 UI 功能不起作用?

为什么不能将“撤消”功能重新分配为ctrl + y而不是ctrl + shift + z。为什么我不能删除默认的“ yank”命令

在NVI习语下,为什么虚拟功能不能公开?

为什么 query-ui 排序功能不能正常工作?

为什么lodash的“某些”功能不能按预期工作?

尽管监控值不断增加,为什么Keras提前停止功能不能停止训练?

为什么循环功能不能与空列表一起使用?

为什么某些Emacs功能不能通过`Mx`使用?

为什么此功能不能正确打开和关闭LED?