Advertisement
davegimo

IMPLEMENTAZIONE ALBERO BINARIO

Sep 1st, 2019
1,186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 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 void main(String[] args) {
  41.        
  42.        
  43.         Albero a = new Albero(2);
  44.         Albero b = new Albero(22);
  45.         Albero c = new Albero(12);
  46.         Albero d = new Albero(10);
  47.         Albero e = new Albero(1);
  48.         Albero f = new Albero(5);
  49.  
  50.         a.sinistro = b;
  51.         a.destro = c;
  52.  
  53.         b.sinistro = d;
  54.         b.destro = e;
  55.  
  56.         c.sinistro = f;
  57.  
  58.         a.stampa();
  59.  
  60.  
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement