I'm going to make web application with d3js. It will have tree map about something and able to add/delete by user. I wonder that how to save tree data on database. data could be
var treeData = [
{
"name": "Top Level",
"parent": "null",
"children": [
{
"name": "Level 2: A",
"parent": "Top Level",
"children": [
{
"name": "Son of A",
"parent": "Level 2: A"
},
{
"name": "Daughter of A",
"parent": "Level 2: A"
}
]
},
{
"name": "Level 2: B",
"parent": "Top Level"
}
]
}
];
or
source,target
A1,A2
A2,A3
A2,A4
actually it doesn't matter which structure I use because the data can save itself on my mysql database as "TEXT". But I want to analyze tree data on database such as
select * from treemap where secondChildren = 'stackoverflow';
select * from treemap where root = 'c++' and lastChildren = 'java';
since my treemap will be dynamically made by user, I cannot make fields as much as children nodes. any idea do you guys have? :)
As Neo M Hacker said, neo4j is great for this sort of thing.
You tagged with mySQL, though, so if you're stuck with an relational database, the best solution I've seen is to have an "ancestors" column which stores a delineated list of IDs of ancestors up the tree. That way you can find whole subtrees or ancestor lists at once. In ruby the ancestry
gem is a great example of how to go about this: