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; }); }
/** * Postorder traversal algorithm * * @param i - start index * @param action - BiFunction with 1st argument -- current position, 2nd argument -- value of the * tree in this position */ public void postorderTraversal(int i, BiFunction action) { for (int j = 0; j < degree; j++) { int index = jthChild(i, j); if (get(index) != null) { postorderTraversal(index, action); } } // perform visit // System.out.println("tree[" + i + "] \t=\t" + tree[i]); action.apply(i, tree[i]); }