generalised trie traversal