Lezio Lab Java 1

annuncio pubblicitario
Laboratorio Java 1
Albero Binario
GT 7.3
1
Esercitazione
Sono dati :
interfacce di Positio<E>, Tree<E>, e BinaryTree<E>;
il file TestLinkedBinaryTree.java
e i file .class che implementano le interfacce
www.dei.unipd.it/corsi/fi2ae
Sezione riservata del sito :
account: studentefi2ae
passwd: deifi2ae
2
1
Position – Interface
public interface Position<E> {
/** Returns the element stored at this position. */
public E element ( ) ;
}
3
Tree – Interface semplificata
(1)
public interface Tree<E> {
public int size(); /** Returns the number of nodes in the tree. */
public boolean isEmpty(); /** Returns whether the tree is empty. */
/** Replaces the element stored at a given node. */
public E replace (Position<E> v, E e)
throws InvalidPositionException;
/** Returns the root of the tree. */
public Position<E> root() throws EmptyTreeException;
/** Returns the parent of a given node. */
public Position<E> parent(Position<E> v)
throws InvalidPositionException, BoundaryViolationException;
4
2
Tree – Interface semplificata
(2)
/** Returns an Iterable of the children of a given node. */
public Iterable<Position<E>> children(Position<E> v)
throws InvalidPositionException;
/** Returns whether a given node is internal. */
public boolean isInternal(Position<E> v)
throws InvalidPositionException;
/** Returns whether a given node is external. */
public boolean isExternal(Position<E> v)
throws InvalidPositionException;
/** Returns whether a given node is the root of the tree. */
public boolean isRoot(Position<E> v)
throws InvalidPositionException;
} //fine tree interface
5
BinaryTree Interface
public interface BinaryTree<E> extends Tree<E> {
public Position<E> left (Position<E> v)
throws InvalidPositionException, BoundaryViolationException;
public Position<E> right (Position<E> v)
throws InvalidPositionException, BoundaryViolationException;
public boolean hasLeft (Position<E> v) throws InvalidPositionException;
public boolean hasRight (Position<E> v) throws InvalidPositionException;
/** Expand an external node into an internal node with two external
node children */
public void expandExternal(Position<E> v, E left, E right);
}
6
3
TestLinkedBinaryTree.java
import java.util.*;
public class TestLinkedBinaryTree {
public static void main( String argv[] ) throws Exception {
BinaryTree<Integer> T = new LinkedBinaryTree<Integer>();
Position<Integer> p1,p2,p3,p4,p5,p6,p7,p8,p9;
p1 = T.root();
T.expandExternal(p1,null,null);
T.replace(p1,new Integer(1));
p2 = T.left(p1);
T.expandExternal(p2,null,null);
T.replace(p2,new Integer(2));
// . . . . . . . . . . .
//INSERIRE QUI IL CODICE RICHIESTO
p1
1
1
p1
p2
p1
2
} //main
} //class
7
Albero utilizzato nell’esempio
8
4
Come esercitazione:
Trovare la Position<E> r della radice dell'albero e stampare il
valore associato a tale posizione
Inserire un ciclo che a partire dalla posizione della radice
dell'albero determini la posizione x del nodo interno più a
sinistra e ne stampi il contenuto
Determinare la posizione z dello zio di x e stamparne il
contenuto
Scrivere un metodo statico con signature :
public static <E> Position<E> leftMost( BinaryTree<E> T,
Position<E> p )
che determina la posizione del nodo interno più a sinistra del
sottoalbero di T con radice p. Verificare il corretto
funzionamento del metodo applicandolo ai nodi r, z, x
9
continua
Scrive un metodo
public static <E> int depth(BinaryTree<E> T, Position<E> p)
{ /*. . .*/ } che determina la profondità del sottoalbero p di T
Scrive un metodo
public static <E> int height(BinaryTree<E> T, Position<E> p)
{ /*. . .*/} che determina la altezza del sottoalbero p di T
Scrivere un metodo statico
public static <E> Position search( BinaryTree<E> T,
Position <E> p, Integer n ) { /*. . .*/ }
che ritorna la posizione del nodo (nel sottoalbero p di T) che
contiene il valore n, se esiste, altrimenti ritorna null.
10
5
Esempio uso metodi statici
import java.util.*;
public class TestLinkedBinaryTree {
public static <E> Position<E> leftMost( BinaryTree<E> T,
Position<E> p) {
// CODICE leftMost
}
public static void main( String argv[] ) throws Exception {
BinaryTree<Integer> T = new LinkedBinaryTree<Integer>();
// . . . . . . . . . . .
Position<Integer> px = leftMost( T, x ); // es. uso metodo statico
} //main
} //class
11
6
Scarica