Write a public String toString() method to do an in-order traversal of the tree
ID: 3689172 • Letter: W
Question
Write a public String toString() method to do an in-order traversal of the tree to set up a string with the Items in increasing order; each Item should appear on a separate line. I already have the recursive method but I don't know how exactly to put it in the toString() method. (Using Java)
Item Class:
public class Item
{
private String number;
private String description;
private int quantity;
private double price;
public Item()
{
this.number = "";
this.description = "";
this.quantity = 0;
this.price = 0;
}
public Item (String number, String description, int quantity, double price)
{
this.number = number;
this.description = description;
this.quantity = quantity;
this.price = price;
}
public Item (Item i)
{
this.number = i.number;
this.description = i.description;
this.quantity = i.quantity;
this.price = i.price;
}
public String getNumber()
{
return number;
}
public String getDescription()
{
return description;
}
public int getQuantity()
{
return quantity;
}
public double getPrice()
{
return price;
}
public void setNumber(String number)
{
this.number = number;
}
public void setDescription(String description)
{
this.description = description;
}
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
public void setPrice(double price)
{
this.price = price;
}
public void setItem(String number, String description, int quantity, double price)
{
this.number = number;
this.description = description;
this.quantity = quantity;
this.price = price;
}
public String toString()
{
return String.format("%-5s %-20s %5d %7.2f", number, description, quantity, price);
}
public boolean equals (Item obj)
{
return this.number.equals(obj.number);
}
public int compareTo(Item obj)
{
return number.compareTo(obj.number);
}
}
InventoryTree Class:
public class InventoryTree
{
private class TreeNode
{
Item data;
TreeNode left;
TreeNode right;
public TreeNode()
{
data = null;
left = null;
right = null;
}
public TreeNode(Item value)
{
data = value;
left = null;
right = null;
}
}
private TreeNode root;
public InventoryTree()
{
root = null;
}
public InventoryTree (InventoryTree t)
{
if (t.root == null)
root = null;
else
{
root = new TreeNode();
TreeNode temp = t.root;
fillValues(root, temp);
}
}
private void fillValues(TreeNode parent, TreeNode temp)
{
parent.data = new Item(temp.data);
if (temp.left != null)
{
TreeNode current = new TreeNode();
parent.left = current;
fillValues (current, temp.left);
}
else
{
parent.left = null;
}
if (temp.right != null)
{
TreeNode current = new TreeNode();
parent.right = current;
fillValues (current, temp.right);
}
else
{
parent.right = null;
}
}
public Item findItem(String value)
{
TreeNode parent = find(value);
TreeNode current = getCurrent(parent, value);
if (current != null && current.data.getNumber().equals(value))
return current.data;
else
return null;
}
private TreeNode find(String value)
{
TreeNode current = root, parent = null;
while (current != null && !current.data.getNumber().equals(value))
{
if (current.data.getNumber().compareTo(value) < 0) //search on right link
{
parent = current;
current = current.right;
}
else //search on the left link
{
parent = current;
current = current.left;
}
}
return parent;
}
private TreeNode getCurrent(TreeNode parent, String value)
{
TreeNode current;
if (parent == null)
return root;
else //determine which child of parent is current
{
if (parent.data.getNumber().compareTo(value) < 0)
current = parent.right;
else
current = parent.left;
return current;
}
}
public boolean addItem(Item add)
{
TreeNode addNode = new TreeNode(add);
TreeNode parent = find(add.getNumber());
if (parent != null && parent.data.equals(add)) //if already exists
return false;
else if (parent == null) //if tree is empty
{
root = addNode;
return true;
}
else if (parent.data.getNumber().compareTo(add.getNumber()) < 0) //go on right link
{
parent.right = addNode;
addNode.left = null;
addNode.right = null;
return true;
}
else //go on left link
{
parent.left = addNode;
addNode.left = null;
addNode.right = null;
return true;
}
}
public boolean adjustQuantity(String value, int add)
{
TreeNode parent = find(value);
TreeNode current = getCurrent(parent, value);
if (current == null || !current.data.getNumber().equals(value))
return false;
else
{
int quantity = current.data.getQuantity() + add;
if (quantity < 0)
return false;
else
{
current.data.setQuantity(quantity);
return true;
}
}
}
public boolean changeUnitPrice (String value , double newPrice)
{
TreeNode parent = find(value);
TreeNode current = getCurrent(parent, value);
if (current == null || !current.data.getNumber().equals(value))
return false;
else
{
current.data.setPrice(newPrice);
return true;
}
}
private void inOrder(TreeNode current)
{
if (current != null)
{
inOrder(current.left);
System.out.println(current.data + " ");
inOrder(current.right);
}
}
public void inOrder() //Print in-order transversal of all Item values
{
inOrder(root);
}
}
Explanation / Answer
Add these two methods in InventoryTree class
private String inOrderString(TreeNode current, String s)
{
if (current.left != null)
s=inOrderString(current.left,s);
s=s+ current.data+" ";
if (current.right != null)
s=inOrderString(current.right,s);
return s.trim();
}
public String toString()
{
return inOrderString(root," ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.