I have to print elements
of a tree
based on the levels of the tree
. I have to use recursion
to complete this objective, however; I am only returning the root data
. I apparently disregard the children
.
/**
* A binary tree in which each node has two children.
*/
public class BinaryTree {
private Node root;
/**
* Constructs an empty tree.
*/
public BinaryTree() {
root = null;
}
/**
* Constructs a tree with one node and no children.
*
* @param rootData the data for the root
*/
public BinaryTree(Object rootData) {
//
root = new Node();
root.data = rootData;
root.left = null;
root.right = null;
}
/**
* Constructs a binary tree.
*
* @param rootData the data for the root
* @param left the left subtree
* @param right the right subtree
*/
public BinaryTree(Object rootData, BinaryTree left, BinaryTree right) {
//
root = new Node();
root.data = rootData;
root.left = left.root;
root.right = right.root;
}
class Node {
public Object data;
public Node left;
public Node right;
public String printTree(int level) {
String strVal = "";
strVal += root.data;
if (root.left != null) {
strVal += this.left.printTree(level + 1);
}
return strVal;
}
}
/**
* Returns the height of the subtree whose root is the given node.
*
* @param n a node or null
* @return the height of the subtree, or 0 if n is null
*/
private static int height(Node n) {
//
if (n == null) {
return 0;
} else {
return 1 + Math.max(height(n.left), height(n.right));
}
}
/**
* Returns the height of this tree.
*
* @return the height
*/
public Object data() {
//
return root.data;
}
/**
* Gets the left subtree of this tree
*
* @return the left child of the root
*/
public BinaryTree left() {
//
BinaryTree result = new BinaryTree();
result.root = root.left;
return result;
}
/**
* Gets the right subtree of this tree
*
* @return the right child of the root
*/
public BinaryTree right() {
//
BinaryTree result = new BinaryTree();
result.root = root.right;
return result;
}
/**
* Sets a new right child
*
* @param child the new right child
*/
public void setRight(BinaryTree child) {
//
BinaryTree result = new BinaryTree();
result.root = root.right;
result = child;
}
/**
* Sets a new left child
*
* @param child the new left child
*/
public void setLeft(BinaryTree child) {
//
BinaryTree result = new BinaryTree();
result.root = root.left;
result = child;
}
public void setData(Object data) {
//
root.data = data;
}
public boolean isLeaf() {
//
if (root.left == null && root.right == null) {
return true;
} else {
return false;
}
}
public String printTree() {
//
return root.printTree(0);
}
}
I don't understand why I'm only returning the root.
Your values for left
and right
are never being set, they will always be null
Also a better way would be like this
private String strVal = ""; // StringBuffer would be better
String result = printTree (root);
public String printTree(Node n) {
if (node.left != null) {
strVal += node.toString();
printTree (node.left);
}
if (node.right != null) {
strVal += node.toString();
printTree (node.right);
}
return strVal;
}
The above method uses the traditional way of passing the node to the recursive method.