Adding Member functions to the TreeNode Class
public class TreeNode
{ // this is the interface that will be provided on AP exam
private Object value;
private TreeNode2 left;
private TreeNode2 right;
..... missing member functions
public int ctNodes()
{if (left == null && right ==null)
return 1;
else
if (left == null)
return 1 + right.ctNodes();
else
return 1 + left.ctNodes();
}
private Integer max(Integer a, Integer b)
{if (a.compareTo(b) > 0)
return a;
return b;
}
public Integer getBiggest() // unordered tree
{if (left == null && right == null)
return (Integer) value;
if (left == null)
return max((Integer)value,(right.getBiggest()));
if (right == null)
return max((Integer)value,(left.getBiggest()));
return max(((Integer)value),max(left.getBiggest(),right.getBiggest()));
}
public void print() print tree inorder
{
if (left != null)
left.print();
System.out.println(value);
if (right != null)
right.print();
}
public void changeEven() // change any even number to next odd
{int n = ((Integer)value).intValue();
if (n % 2==0)
value = new Integer(n+1);
if (left != null)
left.changeEven();
if (right != null)
right.changeEven();
}
public int ctOdd() // count odd
{int ct = 0;
int n = ((Integer)value).intValue();
if (n % 2 == 1)
ct = 1;
if (left == null && right == null)
return ct;
if (left == null)
return ct + right.ctOdd();
if (right == null)
return ct + left.ctOdd();
return ct + left.ctOdd() + right.ctOdd();
}