Salve a tutti,
mi è stato assegnato il seguente esercizio:
Scrivere in java al di fuori della classe SimpTree un metodo polimorfo ricorsivo che ha come argomento un SimpTree T e restituisce un SimpTree T1 che ha la stessa struttura di T ma ogni radice in T1 è uguale alla prima foglia del corrispondente sottoalbero in T.
Qualcuno sa come fare?
Vi mando la classe SimpTree
codice:
public class SimpTree<E> {

private E root;
private SimpList<SimpTree<Integer>> subTreeList;

public SimpTree(E x, SimpList y){
root = x;
subTreeList = y;
}

public E root() {
return root;
}

public SimpList<SimpTree<Integer>> subTreeList(){
return subTreeList;
}

public boolean equals(Object t){
SimpTree t1 = (SimpTree)t;
return equals(t1);
}

public boolean equals(SimpTree t){
SimpList<SimpTree<Integer>> l = subTreeList;
SimpList<SimpTree> l1 = t.subTreeList;
boolean res = true; //res sarà il risultato
if(!root.equals(t.root()))
return false;
while(l!=SimpList.EMPTYLIST && l1!=SimpList.EMPTYLIST){
SimpTree t1 = l.head();
SimpTree t2 = l1.head();
if(!t1.equals(t2))
return false;
l = l.tail();
l1 = l1.tail();
}
return (l==SimpList.EMPTYLIST && l1==SimpList.EMPTYLIST);
}

public String toString(){
String res=""+root;
SimpList<SimpTree<Integer>> l = subTreeList;
while(l!=SimpList.EMPTYLIST){
res=res+"("+l.head()+")";
l = l.tail();
}
return res;
}

public SimpTree clone(){
SimpList<SimpTree> lr = SimpList.EMPTYLIST;
SimpList<SimpTree<Integer>> l = subTreeList;
while(l!=SimpList.EMPTYLIST){
lr = new SimpList(l.head().clone(), lr);
l = l.tail();
}
lr = lr.reverse();
return new SimpTree(root, lr);
}

}