I looked up this basic format for a tree structure in javascript:
function Tree(parent, child, data) { this.parent = parent; this.children = child || [];
Have a look at this approach on how to create tree structures from SQL queries:
http://blog.tcs.de/creating-trees-from-sql-queries-in-javascript/