Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. package test;
  2.  
  3. import eccezioni.EmptyTreeException;
  4. import position.Position;
  5. import binaryTree.BinaryTree;
  6. import binaryTree.LinkedBinaryTree;
  7.  
  8. public class ExBinaryTree_12_2_15 {
  9.  
  10. public static void main(String[] args) {
  11. LinkedBinaryTree<Integer> T2=new LinkedBinaryTree<>();
  12. T2.addRoot(4);
  13. T2.expandExternal(T2.root(), 2, 8);
  14. T2.insertLeft(T2.left(T2.root()), 1);
  15. T2.expandExternal(T2.right(T2.root()), 4, 4);
  16. T2.expandExternal(T2.right(T2.right(T2.root())), 2, 11);
  17.  
  18. T2.insertLeft(T2.left(T2.right(T2.root())), 5);
  19. T2.expandExternal(T2.left(T2.left(T2.right(T2.root()))), 20, 40);
  20. System.out.print("\nII test: ");
  21. System.out
  22. .println("true o false "
  23. + f(T2));
  24.  
  25. }
  26.  
  27. /*
  28. * La funzione deve effettuare una visita ricorsiva di T e restituire true
  29. * se e solo se ciascun nodo interno ha due figli e questi contengono
  30. * entrambi lo stesso elemento. • Se T è vuoto la funzione deve lanciare
  31. * l’eccezione EmptyTreeException. • La funzione non deve invocare funzioni
  32. * che usano/restituiscono collezioni/iteratori di nodi dell’albero. Nel
  33. * caso in cui non venga soddisfatto questo requisito la funzione sarà
  34. * valutata al massimo 10 punti.
  35. */
  36. public static <E> boolean f(BinaryTree<E> T) {
  37. if(T.isEmpty())
  38. throw new EmptyTreeException("");
  39. return fun(T,T.root());
  40. }
  41. private static<E> boolean fun(BinaryTree<E>T, Position<E>v){
  42.  
  43. if(T.isInternal(v)){
  44. for(Position<E> w: T.children(v)){
  45. fun(T,w);
  46. if(T.hasLeft(v) && T.hasRight(v)){
  47. E left=T.left(v).element();
  48. E right=T.right(v).element();
  49. if(left==right){
  50. return true;
  51. }
  52.  
  53. }
  54. }
  55. }
  56. return false;
  57.  
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement