Advertisement
davegimo

Esercizio6

Aug 23rd, 2019
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. public class Albero {
  2.  
  3.     public static boolean foglieContenute(Albero a, Albero b, Albero c) {
  4.  
  5.         int foglieA = sommaFoglie(a);
  6.  
  7.         int nodiB = sommaNodi(b);
  8.         int nodiC = sommaNodi(c);
  9.        
  10.         return (foglieA == nodiB + nodiC);
  11.     }
  12.  
  13.     public static int sommaNodi(Albero k) {
  14.  
  15.        int conta = k.val();
  16.  
  17.        if (k.sinistro() != null) {
  18.             conta += sommaNodi(k.sinistro());
  19.        }
  20.  
  21.        if (k.destro() != null) {
  22.         conta += sommaNodi(k.destro());
  23.         }
  24.  
  25.         return conta;
  26.     }
  27.  
  28.  
  29.     public static int sommaFoglie(Albero k) {
  30.  
  31.         if (k.sinistro() == null && k.destro() == null) { // E' FOGLIA!!
  32.             return k.val();
  33.         }
  34.  
  35.         int conta = 0;
  36.  
  37.         if (k.sinistro() != null) {
  38.             conta += sommaFoglie(k.sinistro());
  39.        }
  40.  
  41.        if (k.destro() != null) {
  42.         conta += sommaFoglie(k.destro());
  43.         }
  44.  
  45.         return conta;
  46.    
  47.     }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement