overhauled tree-iteration based on frequency