【问题标题】:How to make String Print如何制作字符串打印
【发布时间】:2013-04-25 22:04:50
【问题描述】:

我不明白为什么当我运行程序时它不打印字符串。相反,它会打印出数字。

public class Coulter_BST_String
{
public static void main(String[] args) 
 {

    String [] input = new String[] { "Matthew", "Ann", "Mary", "Sara", "Kara", "Anthony", "Tom"
    BinarySearchTree bst = new BinarySearchTree();  

    for (int i = 0;i < input.length; i++)
    {
        bst.insert(input[i]);
    }

    System.out.println("Preorder Traversal:");
    bst.preorderTraversal();

    System.out.println( "\nInorder Traversal:");
    bst.inorderTraversal();

    System.out.println("\nPostorder Traversal:");
    bst.postorderTraversal();

 }
}

【问题讨论】:

  • 请提供您的 BinarySearchTree 的源代码。没有这个就很难理解发生了什么。例如,我不知道是否正在返回一个字符串数组、一个字符串,或者该方法是否由此返回 void。此外,您可能想用 Java 标记它。

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


【解决方案1】:

假设bst.preorderTraversa()返回一个字符串:

System.out.println("Preorder Traversal: " + bst.preorderTraversal());

您只需要在打印中包含遍历。

【讨论】:

    【解决方案2】:

    假设树与您提出的其他问题相似。我不确定它们是否相关或尝试相同,您有:

    public class BinarySearchTree_String 
    {
     private Node root;
    
     public void insert(int key)
    {
        insert(new Node(key, null, null));
    }
    
    public void insert(Node z) 
    {
    
      Node y = null;
      Node x = root;
    
    
    while (x != null) 
          {
            y = x;
    int name = word.compareTo(x.key);       
    if (name < 0) 
    {
                x = x.getLeftChild();
            } 
          else 
    {
              x = x.getRightChild();
          }
        }
    
        //make y the parent of z     
          z.setParent(y);
    
        //checking if there is something inside of y and where to set the vaule that is stroed in y        
          if (y == null) 
          {
            root = z;
        } 
          //if y is not empty compare again to find which child it must be 
          else if 
          (z.getKey().equals(y.getKey()))
          {
            y.setLeftChild(z);
        }
          else
          {
            y.setRightChild(z);
        }
    }
    
    
      public void preorderTraversal() 
    {
        preorderTraversal(root);
    }
    
    public void preorderTraversal(Node node)
      {
              if (node != null)
            {
            //preorder method         
      System.out.print(node.getKey() + " ");
            preorderTraversal(node.getLeftChild());
            preorderTraversal(node.getRightChild());            
        }
    }
    
    
     public void inorderTraversal() 
        {
          inorderTraversal(root);
        }
    
    private void inorderTraversal(Node node)
      {
        if (node != null)
         {
           //the inorder
               inorderTraversal(node.getLeftChild());
            System.out.print(node.getKey() + " ");
            inorderTraversal(node.getRightChild());
        }
    }
    
    //to make root show b/c it is hidden  
     public void postorderTraversal() 
     {
        postorderTraversal(root);
    }
    
    private void postorderTraversal(Node node) 
     {
        if (node != null)
           {
           //post order 
               postorderTraversal(node.getLeftChild());
            postorderTraversal(node.getRightChild());
            System.out.print(node.getKey() + " ");
        }
       }
     }
    

     public class Node_String
     {
      private String key;
      private Node parent;
      private Node leftChild;
      private Node rightChild;
      public String value = " ";
    
     public Node(String key, Node leftChild, Node rightChild)
     {
              this.setKey(key);
              this.setLeftChild(leftChild);
              this.setRightChild(rightChild);
           }
    
       public void setKey(String key) 
       {
          this.key = key;
       }
    
       public int getKey() 
       {
          return key;
       }
    
       public void setParent(Node parent) 
       {
           this.parent = parent;
       }
    
       public Node getParent() 
       {
          return parent;
       }
    
       public void setLeftChild(Node leftChild) 
       {
           this.leftChild = leftChild;
       }
    
        public Node getLeftChild() 
       {
            return leftChild;
       }
    
       public void setRightChild(Node rightChild) 
       {
           this.rightChild = rightChild;
       }
    
       public Node getRightChild() 
        {
            return rightChild;
        }
    }
    

    你会看到:

      public int getKey() 
     {
        return key;
     }
    

    当您的键是字符串时,您将返回一个整数。您应该将其更改为:

    public String getKey()
    {
       return key;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-31
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多