如何制作字符串打印

时间:2013-04-25 22:04:51

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

我无法弄清楚为什么当我运行程序时它不会打印字符串。相反,它会打印出数字。

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();

 }
}

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

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

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;
 }

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

public String getKey()
{
   return key;
}