Reshuffling containers, one of the daily operations at a container terminal, is time consuming and increases a ship’s berthing time. We propose a decision-tree heuristic to minimise the expected number of reshuffles when arriving containers should be stacked in a block of containers with an arbitrary number of piles. The heuristic algorithm uses the optimal solutions of a stochastic dynamic programming model. Since the total number of states of the dynamic programming model increases exponentially, the model can only solve small-scale problems in a reasonable time. To solve large-scale problems, the heuristic uses the results of the exact model for small-scale problems to generate generalised decision trees. These trees can be used to solve problems with a realistic number of piles. The numerical experiments show the effectiveness of the algorithm. For small-scale problems, the trees can quickly make optimal decisions. For large-scale problems, the decision-tree heuristic significantly outperforms stacking policies commonly used in practice. Using the decision trees, we can compare the performance of a shared-stacking policy, which allows containers of multiple ships to be stacked on top of each other, with a dedicated-stacking policy. Shared-stacking appears to outperform dedicated-stacking.