Exemplo n.º 1
0
  public static void main(String[] arg) {
    MyTree<Integer> tree1 = new MyTree<>(2);
    for (int i = 0; i < 5; i++) {
      tree1.set(i, i + 1);
    }

    MyTree<Integer> tree2 = new MyTree<>(2);
    for (int i = 0; i < 3; i++) {
      tree2.set(i, -i);
    }

    tree1.hang(tree2, 5, 0);

    tree1.print();

    System.out.println();

    // 2nd argument is a lambda BiFunction(int position, E value){}
    tree1.postorderTraversal(
        0,
        (position, value) -> {
          System.out.println("[" + position + "]=" + value);
          return null;
        });
  }
Exemplo n.º 2
0
 /**
  * Hang subtree with root with index p of tree T to this tree in the i-th index
  *
  * @param T - tree to hang
  * @param i - index, where we want to hang tree T (root of T will be on that index)
  * @param p - index of element which we want to hang to this tree
  */
 public void hang(MyTree<E> T, int i, int p) {
   set(i, T.get(p));
   for (int j = 0; j < degree; j++) {
     if (T.get(jthChild(p, j)) != null) {
       hang(T, jthChild(i, j), jthChild(p, j));
     }
   }
 }