Logic for DB with Ternary tree structure - php

Currently I need to build a structure for a MLM company. The structure they wanted is something like a ternary tree structure, which have one parent node links to 3 child node.
But for the top most node (root), it can have more than 3 child nodes while all other node will stick to the limit of three. Currently my idea for the structure was storing one ancestor parent node (the parent of the parent node) and also 3 child node as a row of record in DB. The method of keeping track is to store the depth and also node number of the user currently in. The node number will start from 1, left to right on new depth.
But the problem arise when i tried to insert, as I have no optimize solution to identify the root user from other user when inserting to available node (unused node number) as I only keeping track the nodes of depth one, I will need to loop the query to select the next depth level to get the list of empty node (and the loop goes on if no available node found). Currently every info is stored in a single table, 1 to 1 relation. I'd like to know if there is any other effective way of doing this ?
Currently coding is done in php and MySql.

Related

Concerning parent/child array relationship

This is my database structure:
I would like to loop through all the child elements (those who's parent_id is set to the data_id within the same table).
Therefore those with "parent_id" as "0" are parents, and those with parent_id with a number in it is a child.
I would like help creating a function that will generate tables, with the table itself as the parent, and all the rows within it would be children of that parent. There is only one layer of depth in this project (one parent and one child layer always. ).
Can anyone help, or would want a more detailed description?
Thank you, and looking forward to a reply.
From my perspective, as when I look at your database, I assume that your parent will always be added before your child is (If I am wrong, please correct me). And because you said that you only have 1 parent and 1 child, I believe this is how your database would look like:
1st - parent
2nd - child of 1st
3rd - parent
4th - child of 3rd
If that's the case, one single loop can help you out with this pseudo code
//get the data from the database
//run through the loop
//check if the parent_id is 0
// if it is, create an element (a table) to be the container with the id as cited in your data_id
// if is is not, create an element (a row). Then append this element to the table with the same id as this parent_id

Building tree structure in a database-how to fill in the tree one child node at a time?

I'm building a tree in a MySQL database using an adjacency list so each row has a node_id and a parent_id.
The tree will be filled with data over time and I need to fill it in a certain way:
Each node has a max of three child nodes
Fill in each node in the current level one child at a time horizontally
After all nodes in the current level have one child, return to the beginning of the current level and fill in child node 2 for each node, then repeat with the last child
After the last node in the current level has three children, start filling in the next level in the same way
So basically I just need help with the algorithm that will determine the parent_id of the node I am inserting. I'd rather not have to traverse the tree with every insert as it will get quite large, but that's fine if it's the only way. I'm building the logic in PHP but pseudocode is fine. Thanks!

php:How to make hierarchy of 115 levels with the different level access privileges at each level.

How to make hierarchy of 115 levels with the different level access privileges at each level.
The scenario is I want to make a hierarchy(tree) with one root node and under that node there will be three sub nodes. Under each of these three sub nodes there will be three more sub nodes. In this manner the hierarchy extends till the 115th level. The restriction is that root node will have access privileges to view data of all the nodes coming under it till the end of hierarchy. The all other sub nodes within a tree will have access privileges to view the data of the sub nodes coming under them and the data of the subsequent sub nodes coming under them till the end of hierarchy.
You will probably want to reflect such a complex structure in a database, my proposed solution thus is language agnostic.
Create a table such as
Table Nodes: ID, ParentID, Name, Content
where ParentID is a foreign key referencing to the upper level. Root has ParentID = null.
Create another table
Table Node_Rights: NodeID, UserID
where you give access rights to the nodes.
Now you could evaulate the access rights of a user by looping through all the hierarchies, which would effectively take you a LOT of SQL queries. I think you should execute these queries once or each time the users/access rights have changed and save the results in another table. Such as:
Table Transivite_Node_Rights: NodeID, UserID
This table (could as well be in memory) is your cache to make such a large structure feasible.

Setting up a nested set hierarchy

Let's say I have a bunch of businesses. And each business can have multiple categories, subcategories and sub-subcategories (three levels). Let's say I set up a table according to the nested set model for my categories.
How do I now use this table and assign categories to each business? I understand I will need another table but what node gets assigned? Is it the lowest level node?
business_id category_id
And then what's the right way to retrieve all the categories for each business?
The way this generally works is that you assign the leaf or lowest-level-node. Then when you are querying to get the full hierarchy you traverse up the tree to the root. It is generally much easier (especially in MySQL) to traverse from leaf to root then vice versa.
Here is the best link I've found that describes how to accomplish this query for a tree of dynamic size (the link you've included assumes that the tree is always 3 levels deep)

How to create a dynamic tree like structure

I have the details of the members of my family in a database. I want to generate a web page with a family tree generated dynamically by reading from the table.
My table schema looks like this
id(int) name father(int) mother(int) spouse(int) dateOfBirth
where father, mother, and spouse are referencing the id column of the same table. The root node will have null for father and mother.
Given this data how can I go about dynamically generating the family tree. I am new at designing tables, so if this design is sub optimal kindly suggest another schema from which this objective can be achieved.
Any pointers on how to atleast get started would be highly appreciated.
Take a look at nested set model.
Your design looks ok, but with this design it's easier to insert nodes than to get them out of the table.
You can look at nested sets and implement that model. Nested sets are harder to update, but you can get the nodes of any subtree with a single query, so I think it matches your problem quite well (a family tree doesn't change too often :).
You would need some metadata, like relation type (Child, Sibling, Spouse) in addition to the nested sets parent-child relations, but I think you can add that easily.
This design is ok, but you would either select all the data and then build the tree on client side iteratively checking the returned array, or perform many subqueries, which is also not very good.
The best solution I know (for hierarchical structures, you've got spouses also) is storing the tree path in a string field. For example, you've got grandpa with id=1, children with id=2 and 3, 2 has children 4 and 5. Then, they have paths, respectively, "", "1", "1", "1,2", 1,2".
You can use this structure to retrieve the tree elements in order, using ORDER BY path clause.

Categories