二进制树使用PHP + MySQL [英] Binary Tree Using PHP + MySQL

查看:160
本文介绍了二进制树使用PHP + MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP(CodeIgniter)和MySQL实现了一个网站的MLM树。我需要在数据库中的二叉树实现。下面的事情被考虑:


  1. 对于每个节点,左子树中的子节点/节点的最小值和右子树中的子节点/节点数称为一对。对于每个对,一个节点获得1个点 - 应存储在数据库中(节点代表用户)


  2. 当创建一个新节点可能许多节点对增加。因此,每当创建一个节点时,应该更新每个节点的点(在适用时增加一个)。


  3. 另一个约束是每天任何节点不能超过100分。


  4. 我还需要构建(在网页中显示)树。


  5. 数据库可能有100000个节点




    1. 我发现主要有4个模型用于实现MySQL,PHP中的精确数据


      1. 邻接

      2. 路径枚举

      3. 嵌套集

      4. 关闭表



        1. 所以我想找到一个解决方案,它将减少插入开销,并成功更新适用于所有节点的点。



          我已经尝试邻接列表解决方案。

            node(id,parentid,leftChildId,rightChildId,leftCount,rightCount)
          userStat(id,sdate, mlmIncome)



          每次插入一个节点时,我向上并继续增加子计数。对,然后我增加,也增加点。
          我用存储过程。



          我选择这个解决方案over Nested对于插入的每个节点,设置为:
          ,要为嵌套集更新的节点数总是多于邻接列表。



          是多于插入。嵌套集在构建树木方面更好。



          我在正确的方向上请帮助!



          Thnx in Advance!

          解决方案

          帮助您管理层次结构数据



          您的问题听起来最熟悉的可能是修改的预订树遍历


          I am implementing an MLM tree for a website using PHP (CodeIgniter) and MySQL. I need a binary tree implementation in the database. The followings things are to be considered:

          1. For each node , Minimum of the number of children/nodes in left subtree and the number of children/nodes in right subtree is called a pair. For each pair one nodes gets 1 point - which should be stored in database (nodes represent users)

          2. When a new node is created (wherever), it is possible that many of the nodes' pair is incremented. So whenever a node is created, every node's point should be updated(incremented by one when applicable)

          3. another constraint is each day any node can not have more than 100 points.

          4. I also need to construct (display in a webpage) the tree. Only 4-5 levels are to be shown.

          5. The database is likely to have 100000 nodes

          I have found mainly 4 models for implemmenting hieararchical data in MySQL,PHP

          1. Adjacency list
          2. Path enumeration
          3. Nested sets
          4. Closure table

          So I would like to find a solution that will reduce the insertion overhead and successfully update the points for all nodes applicable.

          I have tried the adjacency List solution.

          node ( id, parentid, leftChildId,rightChildId,leftCount,rightCount ) 
          userStat(id,sdate,pairs,mlmIncome)
          

          each time one node is inserted,I go upward and keep incrementing the child counts .If new pair is made then I increment that also and increment the point.. I am doing these with stored procedures.

          The reason why i chose this solution over Nested Set is : for each node inserted , the number of nodes to be updated for Nested Set Is always more than adjacency list.

          Though the rate of constructing tree is more than insertion . And nested set is better in constructing trees..

          Am i in the right direction?? Please help !

          Thnx in Advance!

          解决方案

          This blog might help you with managing hierarchy data

          The one that sounds most familiar to your question is possibly Modified Preorder Tree Traversal

          这篇关于二进制树使用PHP + MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆