Esempio n. 1
0
 /**
  * Assembles some number of elements from the top of the global stack into a new Tree, and
  * replaces those elements with the new Tree.
  *
  * <p><b>Caution:</b> The arguments must be consecutive integers 1..N, in any order, but with no
  * gaps; for example, makeTree(2,4,1,5) would cause problems (3 was omitted).
  *
  * @param rootIndex Which stack element (counting from 1) to use as the root of the new Tree.
  * @param childIndices Which stack elements to use as the children of the root.
  */
 void makeTree(int rootIndex, int... childIndices) {
   // Get root from stack
   Tree<Token> root = getStackItem(rootIndex);
   // Get other trees from stack and add them as children of root
   for (int i = 0; i < childIndices.length; i++) {
     root.addChild(getStackItem(childIndices[i]));
   }
   // Pop root and all children from stack
   for (int i = 0; i <= childIndices.length; i++) {
     stack.pop();
   }
   // Put the root back on the stack
   stack.push(root);
 }
Esempio n. 2
0
  public static void main(String args[]) {
    TreeNode<Integer> root = new TreeNode<Integer>();
    Tree tree = new Tree();
    //		tree.buildTree(root);
    int[] array = {2, 5, 3, 2, 5, 6};
    root = tree.createBST(array, 0, array.length - 1);
    tree.BFS(root);
    ArrayList<int[]> res = tree.findPath(root, 10);
    Iterator<int[]> itr = res.iterator();
    while (itr.hasNext()) {

      tree.printArray(itr.next());
    }
    // System.out.println(isBalanced(root));
  }
Esempio n. 3
0
  /**
   * Il metodo restituisce una collezione iterabile di tutti i nodi a profondità ( conto da root che
   * vale 0 fino alla fino=i)
   *
   * @param T l'albero sul quale operare
   * @param i la profondità
   * @return la collezione iterabile
   */
  public static <E> Iterable<Position<E>> atDepth(Tree<E> T, int i)
      throws InvalidPositionException {
    PositionList<Position<E>> lista = new NodePositionList<Position<E>>(); // lista vuota
    LinkedTree<E> albero = (LinkedTree<E>) T; // cast
    Iterable<Position<E>> temp; // lista iterabile

    if (i == 0) {
      lista.addLast(T.root());
    } else {
      temp = atDepth(T, i - 1);
      for (Position<E> v : temp) {
        PositionList<Position<E>> child = albero.checkPosition(v).getChildren();
        for (Position<E> ww : child) lista.addLast(ww);
      }
    }
    return lista;
  }