e ho visto che non sono riuscito a mettere il codice, scusa la mia ignoranza ora faccio come mi hai detto e spero che sia leggibile. grazie
codice:
public class BinaryTree
{
private BinaryNode root;
public static class BinaryNode
{
private int dato;
private BinaryNode left = null;
private BinaryNode right = null;
public BinaryNode(int dato, BinaryNode left, BinaryNode right)
{
this.dato = dato;
this.left = left;
this.right = right;
}
public BinaryNode(int dato)
{
this(dato, null, null);
}
public BinaryNode(int dato, int left, int right)
{
this(dato, new BinaryNode(left), new BinaryNode(right));
}
public void setDato(int val)
{
dato = val;
}
public int getDato()
{
return dato;
}
public BinaryNode getLeftChild()
{
return left;
}
public void setLeftChild(BinaryNode child)
{
left = child;
}
public BinaryNode getRightChild()
{
return right;
}
public void setRightChild(BinaryNode child)
{
right = child;
}
public int contadati(){
int dati = 0 ;
if(left != null && right != null ||
left == null && right == null)
{
dati = 1 ;
}
if(right != null)
{
dati += right.contadati() ;
}
if(left != null)
{
dati += left.contadati() ;
}
return dati ;
}
public int[] copiadati(){
final int MAX = 100 ;
int[] a = new int[MAX] ;
int i = 0 ;
if(left != null && right != null ||
left == null && right == null)
{
a[i] = dato ;
i = 1 ;
}
if(right != null)
{
right.copiadati() ;
}
if(left != null)
{
left.copiadati() ;
}
return a ;
}
}
public BinaryTree()
{
root = null;
}
public BinaryTree(BinaryNode root)
{
this.root = root;
}
public void setRoot(BinaryNode node)
{
root = node;
}
public BinaryNode getRoot()
{
return root;
}
public void printTree()
{
if (root == null)
System.out.println("albero vuoto");
else
BTreePrinter.printTree(this);
}
public int aggiornaNodiConNumeroDiFoglia() {
if(root == null)
return 0 ;
return root.contadati() ;
}
public int[] copiadati(){
if(root == null)
return null ;
return root.copiadati() ;
}
}