aggiunti algoritmi di visitazione albero in ampiezza, profondità (pre-ordine, post-ordine) e visita albero binario in ordine simmetrico
This commit is contained in:
@@ -50,4 +50,35 @@ public abstract class AbstractBinaryTree<E> extends AbstractTree<E> implements B
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggiunge a snapshot le posizioni del sottoalbero avente radice position.
|
||||
* @param position
|
||||
* @param snapshot
|
||||
*/
|
||||
private void inorderSubtree(Position<E> position, List<Position<E>> snapshot) {
|
||||
if(left(position) != null) inorderSubtree(left(position), snapshot);
|
||||
snapshot.add(position);
|
||||
if(right(position) != null) inorderSubtree(right(position), snapshot);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restituisce una lista delle posizioni dell'albero, in ordine simmetrico
|
||||
* @return lista delle posizioni in ordine simmetrico
|
||||
*/
|
||||
protected Iterable<Position<E>> inorder() {
|
||||
List<Position<E>> snapshot = new ArrayList<>();
|
||||
if(!isEmpty()) inorderSubtree(root(), snapshot);
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Restituisce una lista delle posizioni dell'albero
|
||||
* @return lista delle posizioni.
|
||||
*/
|
||||
@Override
|
||||
public Iterable<Position<E>> positions() {
|
||||
return inorder();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
package it.algoritmi.adt.tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import it.algoritmi.adt.Position;
|
||||
import it.algoritmi.adt.Queue;
|
||||
import it.algoritmi.adt.Tree;
|
||||
import it.algoritmi.adt.queue.LinkedQueue;
|
||||
|
||||
/** Una classe di base astratta che implementa in parte l'interfaccia Tree. */
|
||||
public abstract class AbstractTree<E> implements Tree<E> {
|
||||
@@ -62,4 +67,74 @@ public abstract class AbstractTree<E> implements Tree<E> {
|
||||
return h;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggiunge a snapshot le posizioni del sottoalbero avente radice position.
|
||||
* @param position
|
||||
* @param snapshot
|
||||
*/
|
||||
private void preorderSubtree(Position<E> position, List<Position<E>> snapshot) {
|
||||
snapshot.add(position); // in pre-ordine, aggiungiamo position prima dei sottoalberi
|
||||
for(Position<E> c : children(position))
|
||||
preorderSubtree(c, snapshot);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restituisce una lista delle posizioni dell'albero, in pre-ordine.
|
||||
* @return lista delle posizioni in pre-ordine
|
||||
*/
|
||||
protected Iterable<Position<E>> preorder() {
|
||||
List<Position<E>> snapshot = new ArrayList<>();
|
||||
if(!isEmpty()) preorderSubtree(root(), snapshot); // riempe ricorsivamente snapshot
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggiunge a snapshot le posizioni del sottoalbero avente radice position.
|
||||
* @param position
|
||||
* @param snapshot
|
||||
*/
|
||||
private void postorderSubtree(Position<E> position, List<Position<E>> snapshot) {
|
||||
for(Position<E> c : children(position))
|
||||
postorderSubtree(c, snapshot);
|
||||
snapshot.add(position);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restituisce una lista delle posizioni dell'albero, in post-ordine
|
||||
* @return lista delle posizioni in post-ordine
|
||||
*/
|
||||
protected Iterable<Position<E>> postorder() {
|
||||
List<Position<E>> snapshot = new ArrayList<>();
|
||||
if(!isEmpty()) postorderSubtree(root(), snapshot);
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restituisce una lista delle posizioni dell'albero, attraversato in ampiezza.
|
||||
* @return lista delle posizioni in ampiezza
|
||||
*/
|
||||
protected Iterable<Position<E>> breadthFirst() {
|
||||
List<Position<E>> snapshot = new ArrayList<>();
|
||||
if(!isEmpty()) {
|
||||
Queue<Position<E>> fringe = new LinkedQueue<>();
|
||||
fringe.enqueue(root()); // inizia con la radice
|
||||
while(!fringe.isEmpty()) {
|
||||
Position<E> position = fringe.dequeue(); // estrae dall'inizio della coda
|
||||
snapshot.add(position); // aggiunge questa posizione
|
||||
for(Position<E> c : children(position))
|
||||
fringe.enqueue(c); // aggiunge i figli in fondo alla coda
|
||||
}
|
||||
}
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restituisce una lista delle posizioni dell'albero
|
||||
* @return lista delle posizioni.
|
||||
*/
|
||||
@Override
|
||||
public Iterable<Position<E>> positions() {
|
||||
return preorder();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,120 +0,0 @@
|
||||
package it.algoritmi.adt.tree;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import it.algoritmi.adt.Position;
|
||||
|
||||
public class ArrayBinaryTree<E> extends AbstractBinaryTree<E> {
|
||||
|
||||
//----------------- classe Node annidata -------------------------
|
||||
protected static class Node<E> implements Position<E> {
|
||||
private E element;
|
||||
private Node<E> parent;
|
||||
private Node<E> left;
|
||||
private Node<E> right;
|
||||
|
||||
public Node(E element, Node<E> above, Node<E> leftChild, Node<E> rightChild) {
|
||||
this.element = element;
|
||||
this.parent = above;
|
||||
this.left = leftChild;
|
||||
this.right = rightChild;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E getElement() {
|
||||
return element;
|
||||
}
|
||||
|
||||
public void setElement(E element) {
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
public Node<E> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(Node<E> parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public Node<E> getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public void setLeft(Node<E> left) {
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
public Node<E> getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public void setRight(Node<E> right) {
|
||||
this.right = right;
|
||||
}
|
||||
}
|
||||
//----------------- fine della classe Node annidata -------------------
|
||||
|
||||
public static final int CAPACITY = 16;
|
||||
|
||||
private Node<E>[] data;
|
||||
private int size;
|
||||
|
||||
public ArrayBinaryTree() {
|
||||
this(CAPACITY);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ArrayBinaryTree(int capacity) {
|
||||
data = (Node<E>[]) new Object[capacity];
|
||||
size = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> left(Position<E> position) throws IllegalArgumentException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> right(Position<E> position) throws IllegalArgumentException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> root() {
|
||||
return data[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> parent(Position<E> position) throws IllegalArgumentException {
|
||||
if(isRoot(position)) return null;
|
||||
int index = depth(position);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restituisce il numero di nodi presenti
|
||||
* nell'albero.
|
||||
* @return numero di nodi dell'albero
|
||||
*/
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Position<E>> positions() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
package it.algoritmi.adt.tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import it.algoritmi.adt.Position;
|
||||
|
||||
@@ -241,36 +239,6 @@ public class LinkedBinaryTree<E> extends AbstractBinaryTree<E> {
|
||||
node.setParent(node);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggiunge a snapshot le posizioni del sottoalbero avente radice position.
|
||||
* @param position
|
||||
* @param snapshot
|
||||
*/
|
||||
private void preorderSubtree(Position<E> position, List<Position<E>> snapshot) {
|
||||
snapshot.add(position); // in pre-ordine, aggiungiamo position prima dei sottoalberi
|
||||
for(Position<E> c : children(position))
|
||||
preorderSubtree(c, snapshot);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restituisce una lista delle posizioni dell'albero, in pre-ordine.
|
||||
* @return lista delle posizioni in pre-ordine
|
||||
*/
|
||||
protected Iterable<Position<E>> preorder() {
|
||||
List<Position<E>> snapshot = new ArrayList<>();
|
||||
if(!isEmpty()) preorderSubtree(root(), snapshot); // riempe ricorsivamente snapshot
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restituisce una lista delle posizioni dell'albero
|
||||
* @return lista delle posizioni.
|
||||
*/
|
||||
@Override
|
||||
public Iterable<Position<E>> positions() {
|
||||
return preorder();
|
||||
}
|
||||
|
||||
//------------------ classe ElementIterator annidata ----------
|
||||
/** Adatta l'iterazione prodotta da positions() per restituire elementi. */
|
||||
|
||||
Reference in New Issue
Block a user