The tree has the following characteristics:
Example
I am trying to represent a category structure such as the following:
Desktop and Mobile Applications
Desktop and Mobile Applications->Android Apps
Desktop and Mobile Applications->Android Apps->Games
Desktop and Mobile Applications->Android Apps->Games->Action
Desktop and Mobile Applications->Games
Desktop and Mobile Applications->Games->Action
Desktop and Mobile Applications->Games->Adventure
Desktop Applications
Desktop Applications->Games
Desktop Applications->Games->Action
Desktop Applications->Games->Adventure
IPhone Applications
Desktop Applications->Games
Desktop Applications->Games->Action
Desktop Applications->Games->Adventure
Tried using the Nested Set Algorithm and I end up with multiple "Games" categories with different categoryIDs and at different depths.
Any help with this will be much appreciated.
You could use adjacency lists (your current idea), nested sets, or even (with appropriate database support) arrays of node ids to represent the path from the root. Which representation you choose depends on what you need to do to your data. @Kim, why array? If the graph is a tree, each node has at most one parent.
To show the schema, we can use the DESC command. This gives the description about the table structure.
MongoDB allows various ways to use tree data structures to model large hierarchical or nested data relationships. Presents a data model that organizes documents in a tree-like structure by storing references to "parent" nodes in "child" nodes.
The simplest way to serialize a tree is to give each node a parent_id column that contains the ID of the parent node. Any modification of the tree (like adding a node or changing a node's parent) only affect a single row in the table, so changes are fast. The number of queries grows with the depth of your tree.
The simple way is to structure a table like:
Categories
CategoryID
ParentID
Name
Your data would look like:
1, 0, 'Desktop and Mobile Apps'
2, 1, 'Android Apps'
3, 2, 'Games'
4, 3, 'Action'
5, 1, 'Games'
6, 5, 'Action'
7, 5, 'Adventure'
8, 0, 'Desktop Apps'
9, 8, 'Games'
You would query it like:
select * from Categories where ParentId = 1
which would return Android Apps and Games. To get the sub categories of games you would do select * from Categories where ParentId = 5
which would return action and adventure.
update In order to associate a single item with multiple categories you will want one additional table:
xref_CategoriesItems
CategoryId
ItemId
This would allow any single item to be associated with multiple categories. Let's say you have a desktop app that needs to appear with both Desktop Apps > Games and Desktop and Mobile Apps > Games.
Your table would have the following data for item 1:
3, 1
9, 1
When seeing what items were in a specific category you would do the following:
select I.*
from items I
inner join xref_CategoriesItems XCI on (XCI.ItemId = I.ItemID)
WHERE (XCI.Category = @CategoryId)
To see which categories a specific item falls under:
select C.*
from categories C
inner join xref_CategoriesItems XCI on (XCI.CategoryId = C.CategoryId)
where (XCI.ItemId = @ItemId)
The query for all items under a specific category is a little more complex if you need all of the child records. Basically you need to do a recursive join the xref_categories with the categories to get the children. I don't remember how to express that in MySQL's version of sql; however the following might be good to know: Using MySQL query to traverse rows to make a recursive tree
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With