A modified version of tree algorithm in Python