I recently used html5 sumary/detail to implement a treeview and it was the best experience ever solving such problem on the web. Very simple and easily customizable via css, and if you render it on the server, you don't need javascript at all.
I find it amusing how things have changed. We've almost come full circle.
https://pbs.twimg.com/media/ENnKCr2U8AAw4Bf?format=jpg&name=...
https://codepen.io/dsheiko/pen/MvEpXm
This is a bit fancier than necessary and that makes it not so good as an example for learning, but hopefully it will be enough to give you the idea.
edit: this one is a little bit better to understand the basics: https://medium.com/metaphorical-web/javascript-treeview-cont...
In short, summary/detail has the native hability to collapse the details and you can explore that to implement a tree by nesting them.
https://css-tricks.com/quick-reminder-that-details-summary-i...