Create Tree Data Structure Javascript Substring Function In Python