【问题标题】:Building a binary search tree of strings in java在java中构建字符串的二叉搜索树
【发布时间】:2020-02-27 13:54:50
【问题描述】:

我正在尝试构建一个字符串树,但我似乎遇到了一些我不知道如何解决的问题。

   public static TreeNode buildTree(TreeNode t, String s)
   {
      int size = s.length();
      while(size > 0)
      {
         String current = s.substring(0,1);
         insert(t, current);
         s = s.substring(1);
         size--;
      }
      System.out.println("This is the tree:");
      System.out.println(t);
      return t;
   }
     	/**************************
   	Recursive algorithm to build a BST: if the node is null, insert the 
   	new node. Else, if the item is less, set the left node and recur to 
   	the left. Else, if the item is greater, set the right node and recur 
   	to the right.   
   	*****************************/
   private static TreeNode insert(TreeNode t, String s)
   {  
      if(t == null)
      {
         t = new TreeNode(s, null, null); 
         return t;
      }
      else
      {
         String s1 = (String) t.getValue();
         String s2 = s;
         //this line below is problematic
         if(s2.compareTo(s1) == -1 || s2.compareTo(s1) == 0) //s2 comes before s1
         {
            t.setLeft(new TreeNode(s2));
            insert(t.getLeft(), s);
         }
         else
         {
            t.setRight(new TreeNode(s2));
            insert(t.getRight(), s); 
         }
      }
      return t;
   }

这是 TreeNode 类:

class TreeNode 
{
   private Object value; 
   private TreeNode left, right;
   
   public TreeNode(Object initValue)
   { 
      value = initValue; 
      left = null; 
      right = null; 
   }
   
   public TreeNode(Object initValue, TreeNode initLeft, TreeNode initRight)
   { 
      value = initValue; 
      left = initLeft; 
      right = initRight; 
   }
   
   public Object getValue()
   { 
      return value; 
   }
   
   public TreeNode getLeft() 
   { 
      return left; 
   }
   
   public TreeNode getRight() 
   { 
      return right; 
   }
   
   public void setValue(Object theNewValue) 
   { 
      value = theNewValue; 
   }
   
   public void setLeft(TreeNode theNewLeft) 
   { 
      left = theNewLeft;
   }
   
   public void setRight(TreeNode theNewRight)
   { 
      right = theNewRight;
   }
}

当我从 buildTree 方法调用 insert 方法时,我尝试 t = new TreeNode(s);或 t= new TreeNode(s, null, null) 当 t==null 最初时,树在返回 buildTree 时保持为 null。但是,它似乎在 insert 方法中更新了树。我该如何解决这个问题?

另外,我在 insert 方法中的 compareTo 似乎有问题,因为它经常返回这个:

线程“主”java.lang.StackOverflowError 中的异常

任何帮助将不胜感激,因为我已经坚持了很长一段时间!

【问题讨论】:

    标签: java string tree binary-search-tree treenode


    【解决方案1】:

    那是因为在Java中参数是通过引用传递的,所以当你这样做时

    t = new TreeNode(s, null, null);
    

    您正在为 t 分配一个新的引用,而不是为收到的引用分配一个“值”

    既然你要退货,它应该可以工作

    t = insert(t, current);
    

    关于错误,您必须在某些情况下产生无限循环的插入调用,您应该能够在调试时检测到它

    【讨论】:

    • 我的 if(t==null) 部分有问题,所以像你一样递归并不能解决问题,而是由于无限递归而遇到运行时错误,我相信(恰好是这样)
    【解决方案2】:
    1. 建议设计一个Class:TreeBuilder来构建你的字符串的树然后返回它的头(一棵树必须有一个headNode)。将递归方法隔离到另一个util类:Builder
    class TreeBuilder{
        private TreeNode head = null;                 // head Node
    
        public TreeNode buildTree(String source) {
            for (int i = 0; i < source.length(); i++) {
                String current = String.valueOf(source.charAt(i));
                Builder.makeTree(head, current);
            }
            return this.head;
        }
    }
    

    2.Util类构建器

    class Builder{
        public static void makeTree(TreeNode head,String current) {
            if (head == null) {                 // make head first
                head = new TreeNode(current);
            }else {                             
                recurse(head, current);         // make body left
            }
        }
    
        public static void recurse(TreeNode temp, String current){
            if ( current.compareTo(temp.getValue()) < 1) {  // current <= tempNode
                if (temp.getLeft() != null) {               // tempNode has a smaller leftNode than itself
                    recurse(temp.getLeft(), current);       // recurse left
                }else {
                    temp.setLeft(new TreeNode(current));    // current smaller than tempNode
                }
            }else {                                         // current > tempNode
                if (temp.getRight() != null) {              // tempNode has a bigger rightNode than itselft
                    recurse(temp.getRight(), current);      // resurse right
                }else {
                    temp.setRight(new TreeNode(current));   // current bigger than tempNode
                }
            }
        }
    }
    

    构建树的关键是将当前字符与 headNode 进行比较,直到出现一个叶子节点或内部节点,在你的问题块中遗漏了。

    • 如果current小于tempNode,你应该递归比较tempNode的leftNode[如果存在一个leftNode]。因为leftNode小于tempNode,你需要检查current和leftNode之间哪个最小,以此类推)

    • 另外,如果当前大于 tempNode,你也应该递归右移

    【讨论】:

    • 我想我已经尝试了你在这里告诉我的内容,但最后我仍然得到一个空树......
    • 专注于recuse方法,调试一下。我的代码有一些打字错误,刚刚修改,你可以再试一次
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    相关资源
    最近更新 更多