I'm building a web-app that needs to process nested geographical data to both display in a treeview, but also be searchable. The raw data looks something like this:
id:1, name:UK
id:2: name: South-East, parentId: 1
id:3: name: South-West, parentId:1
id:4: name: Berkshire, parentId: 2
id:5: name: Reading, parentId: 4
and I want it to look something like this:
id:1: name UK, children[
{id: 2, name: South-East, children:[
{id:4: name: Berkshire, children: [
{id:5: name: Reading}
]
},
{id:3: name: South-West}
]
so that each geographical location has a "children" array property, which contains all the sub-areas, each of which has another "children" array property, and so on. It would probably make sense to have a "parent" property as well, so I could navigate from any child item up to its parent.
I also need to be able to search the list - searching each branch of the tree may take some time, so perhaps I need to also keep the list in flat format.
I know how I could do this in JavaScript (possibly using jLinq for filtering, grouping and sorting), but I don't know how fast it would be. Has anyone already had a go at this in JavaScript or know of any general algorithms/patterns that solve this?