Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import eccezioni.EmptyTreeException;
- import position.Position;
- import binaryTree.BinaryTree;
- import binaryTree.LinkedBinaryTree;
- public class ExBinaryTree_12_2_15 {
- public static void main(String[] args) {
- LinkedBinaryTree<Integer> T2=new LinkedBinaryTree<>();
- T2.addRoot(4);
- T2.expandExternal(T2.root(), 2, 8);
- T2.insertLeft(T2.left(T2.root()), 1);
- T2.expandExternal(T2.right(T2.root()), 4, 4);
- T2.expandExternal(T2.right(T2.right(T2.root())), 2, 11);
- T2.insertLeft(T2.left(T2.right(T2.root())), 5);
- T2.expandExternal(T2.left(T2.left(T2.right(T2.root()))), 20, 40);
- System.out.print("\nII test: ");
- System.out
- .println("true o false "
- + f(T2));
- }
- /*
- * La funzione deve effettuare una visita ricorsiva di T e restituire true
- * se e solo se ciascun nodo interno ha due figli e questi contengono
- * entrambi lo stesso elemento. • Se T è vuoto la funzione deve lanciare
- * l’eccezione EmptyTreeException. • La funzione non deve invocare funzioni
- * che usano/restituiscono collezioni/iteratori di nodi dell’albero. Nel
- * caso in cui non venga soddisfatto questo requisito la funzione sarà
- * valutata al massimo 10 punti.
- */
- public static <E> boolean f(BinaryTree<E> T) {
- if(T.isEmpty())
- throw new EmptyTreeException("");
- return fun(T,T.root());
- }
- private static<E> boolean fun(BinaryTree<E>T, Position<E>v){
- if(T.isInternal(v)){
- for(Position<E> w: T.children(v)){
- fun(T,w);
- if(T.hasLeft(v) && T.hasRight(v)){
- E left=T.left(v).element();
- E right=T.right(v).element();
- if(left==right){
- return true;
- }
- }
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement