MKS tries algorithm