Esercitazione 1 1. Scrivere una classe Java BTreeNode per rappresentare nodi di alberi binari. Assumere che il contenuto informativo di ogni nodo sia rappresentato da un intero. 2. Fornire un implementazione ricorsiva della visita DFS di un albero binario. Realizzare un metodo main di prova, in cui viene creato a mano e visitato un albero binario (NB: visitare un nodo significa stamparne il contenuto informativo). 3. Servirsi dell implementazione del tipo Pila vista a lezione per realizzare una visita DFS iterativa. Confrontare l output con quello della versione ricorsiva. 4. Implementare tre metodi Java che, dato in input un albero binario, ne determinino rispettivamente il numero delle foglie, il numero dei nodi e la profondita. 5. Implementare un metodo che, dato in input un albero binario, determini se e completo.
Esercitazione 1 Possibile soluzione
Classe BTreeNode public class BTreeNode { int key; BTreeNode leftchild, rightchild;
Visita DFS ricorsiva public class BTree { BTreeNode root; //constructor... public BTree (BTreeNode r) { root=r; public void visitdfsric (BTreeNode n) { public void dfsricbtree () { visitdfsric (root); System.out.print ("\n"); if (n==null) return; //visits node... System.out.print (n.info+" "); //recursive calls... visitdfsric (n.leftchild); visitdfsric (n.rightchild);
Visita DFS ricorsiva: main di prova (1/2) public class ProvaDFSric { public static void main (String[] args) { (continua ) //creates nodes... BTreeNode a=new BTreeNode (); BTreeNode l=new BTreeNode (); BTreeNode b=new BTreeNode (); BTreeNode e=new BTreeNode (); BTreeNode r=new BTreeNode (); BTreeNode o=new BTreeNode (); //sets up tree... a.info='a'; l.info='l'; b.info='b'; e.info='e'; r.info='r'; o.info='o';
Visita DFS ricorsiva: main di prova (2/2) a.leftchild=l; a.rightchild=b; l.leftchild=e; l.rightchild=r; b.rightchild=o; BTree bintree=new BTree (a); //runs dfs... bintree.dfsricbtree ();
Implementazione del tipo Pila (1/3) public class MyStackNode { public class MyStack { public Object info; public MyStackNode next=null; private MyStackNode head; //constructor... public MyStack () { head=null; public boolean isempty () { return head==null; (continua )
Implementazione del tipo Pila (2/3) public Object top () { if (isempty ()) return null; else return head.info; public Object pop () { if (isempty ()) return null; Object el=head.info; head=head.next; return el; (continua )
Implementazione del tipo Pila (3/3) public void push (Object el) { MyStackNode mysn=new MyStackNode (); mysn.info=el; mysn.next=head; head=mysn;
Visita DFS iterativa public class BTree { BTreeNode root; //constructor... public BTree (BTreeNode r) { public void dfsiterbtree () { MyStack s=new MyStack (); root=r; s.push (root); while (!s.isempty ()) { BTreeNode u=(btreenode) s.pop (); if (u!=null) { //visits node... System.out.print (u.info+" "); s.push (u.rightchild); s.push (u.leftchild); System.out.print ("\n");
Visita DFS iterativa: main di prova (1/2) public class ProvaDFSIter { public static void main (String[] args) { (continua ) //creates nodes... BTreeNode a=new BTreeNode (); BTreeNode l=new BTreeNode (); BTreeNode b=new BTreeNode (); BTreeNode e=new BTreeNode (); BTreeNode r=new BTreeNode (); BTreeNode o=new BTreeNode (); //sets up tree... a.info='a'; l.info='l'; b.info='b'; e.info='e'; r.info='r'; o.info='o';
Visita DFS iterativa: main di prova (2/2) a.leftchild=l; a.rightchild=b; l.leftchild=e; l.rightchild=r; b.rightchild=o; BTree bintree=new BTree (a); //runs dfs... bintree.dfsiterbtree ();
Numero di foglie in un albero binario public class recmethods { public static int numleaves (BTreeNode r) { if (r==null) return 0; if (r.leftchild==null && r.rightchild==null) return 1; int left=numleaves (r.leftchild); int right=numleaves (r.rightchild); return left+right; (continua )
Numero di nodi in un albero binario public static int numnodes (BTreeNode r) { if (r==null) return 0; int left=numnodes (r.leftchild); int right=numnodes (r.rightchild); return left+right+1; (continua )
Profondita di un albero binario public static int finddepth (BTreeNode r) { if (r==null) return -1; int left=finddepth (r.leftchild); int right=finddepth (r.rightchild); if (left>right) return left+1; else return right+1; (continua )
Completezza di un albero binario public static boolean iscomplete (BTreeNode r) { int depth=finddepth (r); int leaves=numleaves (r); if (leaves==(int)math.pow ((double)2, (double)depth)) return true; else return false; (continua )
Main di prova public static void main (String[] args) { BTreeNode a=new BTreeNode(); BTreeNode b=new BTreeNode(); BTreeNode c=new BTreeNode(); BTreeNode d=new BTreeNode(); BTreeNode e=new BTreeNode(); BTreeNode f=new BTreeNode(); BTreeNode g=new BTreeNode(); BTreeNode h=new BTreeNode(); a.leftchild=b; a.rightchild=c; b.rightchild=d; b.leftchild=e; c.rightchild=f; c.leftchild=g; g.rightchild=h; System.out.println ("NumLeaves = + numleaves (a)); System.out.println ("NumNodes = + numnodes (a)); System.out.println ("Depth = + finddepth (a)); System.out.println ("iscomplete says: + iscomplete (a));