Advertisement
davegimo

Untitled

Apr 20th, 2019
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | None | 0 0
  1. public static boolean livelloK (Albero A, int k) {
  2.  
  3.     int sk = sommaK(A,1,k);
  4.     int sf = sommaFoglia(A);
  5.  
  6.     if (sf == sk) {
  7.         return true;
  8.     }
  9.     else {
  10.         false;
  11.     }
  12. }
  13.  
  14. public static int sommaK(Albero A, int livelloCorrente, int k) {
  15.     if (livelloCorrente == k) {
  16.         return A.val();
  17.     }
  18.     int conta = 0;
  19.  
  20.     if (A.sinistro() != null) {
  21.         conta += sommaK(A.sinistro(), livelloCorrente + 1, k);
  22.     }
  23.  
  24.     if (A.destro() != null) {
  25.         conta += sommaK(A.destro(), livelloCorrente + 1, k);
  26.     }
  27.  
  28.     return conta;
  29. }
  30.  
  31. public static int sommaFoglia(Albero A) {
  32.  
  33.     if (A.sinistro() == null && A.destro() == null) { // PASSO BASE : E' FOGLIA!
  34.         return A.val();
  35.     }
  36.  
  37.     int conta = 0;
  38.  
  39.     if (A.sinistro() != null) {
  40.         conta += sommaFoglia(A.sinistro());
  41.     }
  42.  
  43.     if (A.destro() != null) {
  44.         conta += sommaFoglia(A.destro());
  45.     }
  46.  
  47.     return conta;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement