Trees implementation in JavaScript