Advertisement
davegimo

franc2

Mar 9th, 2021
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. public class Albero {
  2.  
  3.         public int val;
  4.         public Albero sinistro;
  5.         public Albero destro;
  6.  
  7.         public Albero(int a) {
  8.             val = a;
  9.         }
  10.  
  11.         public int getVal() {
  12.             return this.val;
  13.         }
  14.  
  15.         public Albero getSinistro() {
  16.             return this.sinistro;
  17.         }
  18.  
  19.         public Albero getDestro() {
  20.             return this.destro;
  21.         }
  22.  
  23.  
  24.  
  25.     public void stampa() {
  26.  
  27.         System.out.println(this.val);
  28.  
  29.         if (this.getSinistro() != null) {
  30.             this.sinistro.stampa();
  31.         }
  32.  
  33.  
  34.         if (this.getDestro() != null) {
  35.             this.destro.stampa();
  36.         }
  37.     }
  38.    
  39.    
  40.     public static int somma(Albero a) {
  41.        
  42.         int tot = a.val;
  43.        
  44.         if (a.sinistro == null && a.destro == null) { //passo base
  45.             return tot;
  46.         }
  47.        
  48.         //passo ricorsivo
  49.         if (a.sinistro != null) {
  50.             tot += somma(a.sinistro);
  51.         }
  52.        
  53.         if (a.destro != null) {
  54.             tot += somma(a.destro);
  55.         }
  56.        
  57.         return tot;
  58.        
  59.        
  60.     }
  61.    
  62.    
  63.     public static int conta(Albero a) {
  64.        
  65.         int c = 1;
  66.        
  67.         if (a.sinistro == null && a.destro == null) {
  68.             return c;
  69.         }
  70.        
  71.         if (a.sinistro != null) {
  72.             c += conta(a.sinistro);
  73.         }
  74.        
  75.         if (a.destro != null) {
  76.             c += conta(a.destro);
  77.         }
  78.        
  79.         return c;
  80.        
  81.        
  82.        
  83.     }
  84.    
  85.     public static int contaK(Albero a, int k) {
  86.        
  87.         int c = 0;
  88.        
  89.         if (a.val > k) {
  90.             c += 1;
  91.         }
  92.        
  93.         if (a.sinistro == null && a.destro == null) {
  94.             return c;
  95.         }
  96.        
  97.         if (a.sinistro != null) {
  98.             c += contaK(a.sinistro,k);
  99.         }
  100.        
  101.         if (a.destro != null) {
  102.             c += contaK(a.destro,k);
  103.         }
  104.        
  105.         return c;
  106.        
  107.        
  108.        
  109.     }
  110.  
  111.    
  112.  
  113.     public static void main(String[] args) {
  114.        
  115.        
  116.         Albero a = new Albero(2);
  117.         Albero b = new Albero(22);
  118.         Albero c = new Albero(12);
  119.         Albero d = new Albero(10);
  120.         Albero e = new Albero(1);
  121.         Albero f = new Albero(5);
  122.  
  123.         a.sinistro = b;
  124.         a.destro = c;
  125.  
  126.         b.sinistro = d;
  127.         b.destro = e;
  128.  
  129.         c.sinistro = f;
  130.  
  131.        
  132.         int x = contaK(a,10);
  133.         System.out.println(x);
  134.  
  135.  
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement