Advertisement
heavenriver

2012_07_24.java (ex. 2)

Dec 31st, 2012
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. // Esame del 24 luglio 2012, esercizio 2
  2. // Ricordarsi di specificare sempre, nei commenti al codice, l'utilità di ogni metodo.
  3.  
  4. class Node
  5.     {
  6.     E elem;
  7.     Node left;
  8.     Node right;
  9.     public Node();
  10.     public Node(E elem);
  11.     public E getElem();
  12.     public boolean hasLeft();
  13.     public boolean hasRight();
  14.     public Node left();
  15.     public Node right();
  16.     }
  17.  
  18. class BinTree
  19.     {
  20.     Node root;
  21.     public BinTree(E rootElem);
  22.     public Node insert(E elem);
  23.     public Node remove(E elem);
  24.     public int size();
  25.    
  26.     public boolean isCompleto() // O(n) perché esamina l'intero albero
  27.         {
  28.         return isCompleto(root);
  29.         }
  30.     public boolean isCompleto(Node n)
  31.         {
  32.         if(n == null) return true;
  33.         if((n.hasLeft() && !n.hasRight()) || (n.hasRight() && !n.hasLeft())) return false;
  34.         return isCompleto(n.left) && isCompleto(n.right);
  35.         }
  36.    
  37.     public Node maxCompleto() // O(n^2) perché esamina l'intero albero un massimo di n volte
  38.         {
  39.         if(isCompleto()) return root;
  40.         Node out = new Node();
  41.         maxCompleto(root, out);
  42.         return out;
  43.         }
  44.     public void maxCompleto(Node n, Node out)
  45.         {
  46.         if(n == null) return;
  47.         if(isCompleto(n.left))
  48.             {
  49.             out = n.left;
  50.             return;
  51.             }
  52.         if(isCompleto(n.right))
  53.             {
  54.             out = n.right;
  55.             return;
  56.             }
  57.         maxCompleto(n.left, out);
  58.         maxCompleto(n.right, out);
  59.         }
  60.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement