I can not figure out why when I run the program it is does not print strings. Instead, it will print out numbers.
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();
}
}
Assuming bst.preorderTraversa()
returns a string:
System.out.println("Preorder Traversal: " + bst.preorderTraversal());
You just need to include the traversal within the print.
Assuming that the tree is similar to the other question you asked. I am not sure if they are releated or attempts at the same, you have:
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() + " ");
}
}
}
and
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;
}
}
You will see:
public int getKey()
{
return key;
}
when your key is a String you are returning an integer. You should change this to:
public String getKey()
{
return key;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.