[英]Tree data structure in php
in my application user starts a new tree or get added under a child-user and keep on adding users in branches in such a way- 在我的应用程序中,用户启动一个新树或在一个子用户下添加,并继续以这种方式在分支机构中添加用户 -
>
there are 10 level of tree type structure. >
有10级树型结构。
>
root node contain 1
user and each node(user) can have max 5
child-user in this way tree will be like level 0 = 1 user
, >
根节点包含1
用户,每个节点(用户)可以有最多 5
个子用户,这样树就像level 0 = 1 user
,
level 1 = 5 user
, level 1 = 5 user
,
level 2 = 25 user
, level 2 = 25 user
,
level 3 = 125 user
and so on. level 3 = 125 user
,依此类推。
I created one MySQL table having columns like- 我创建了一个MySQL表,其中包含以下列
User_id
, level
, super_id
, child1_id
, child2_id
, child3_id
, child4_id
, child5_id
User_id
, level
, super_id
, child1_id
, child2_id
, child3_id
, child4_id
, child5_id
my question is How can I get all child-user( child to child also
) of a particular user at any level do I need to add some more columns in my table?? 我的问题是如何在任何级别获得特定用户的所有子用户(
child to child also
)我是否需要在我的表中添加更多列?
You might find it interesting to read my presentation from last week's PHP TEK-X conference: 您可能会发现从上周的PHP TEK-X会议中阅读我的演示文稿很有意思:
This talk describes alternative solutions in SQL, including: 本演讲描述了SQL中的替代解决方案,包括:
Also see my answer to this Stack Overflow question: What is the most efficient/elegant way to parse a flat table into a tree? 另请参阅我对此Stack Overflow问题的回答: 将平面表解析为树的最有效/优雅方法是什么?
You should have a look at Nested Sets . 你应该看一下嵌套集 。
In PHP, you could use Doctrine Nested Sets , that will make your life much easier. 在PHP中,您可以使用Doctrine嵌套集 ,这将使您的生活更轻松。
This is the complete code i used to build a binary tree datastructure and its corresponding operations: 这是我用来构建二叉树数据结构及其相应操作的完整代码:
<?php
class Node
{
public $data;
public $leftChild;
public $rightChild;
public function __construct($data)
{
$this->data=$data;
$this->leftChild=null;
$this->rightChild=null;
}
public function disp_data()
{
echo $this->data;
}
}//end class Node
class BinaryTree
{
public $root;
//public $s;
public function __construct()
{
$this->root=null;
//$this->s=file_get_contents('store');
}
//function to display the tree
public function display()
{
$this->display_tree($this->root);
}
public function display_tree($local_root)
{
if($local_root==null)
return;
$this->display_tree($local_root->leftChild);
echo $local_root->data."<br/>";
$this->display_tree($local_root->rightChild);
}
// function to insert a new node
public function insert($key)
{
$newnode=new Node($key);
if($this->root==null)
{
$this->root=$newnode;
return;
}
else
{
$parent=$this->root;
$current=$this->root;
while(true)
{
$parent=$current;
//$this->find_order($key,$current->data);
if($key==($this->find_order($key,$current->data)))
{
$current=$current->leftChild;
if($current==null)
{
$parent->leftChild=$newnode;
return;
}//end if2
}//end if1
else
{
$current=$current->rightChild;
if($current==null)
{
$parent->rightChild=$newnode;
return;
} //end if1
} //end else
}//end while loop
}//end else
} //end insert function
//function to search a particular Node
public function find($key)
{
$current=$this->root;
while($current->data!=$key)
{
if($key==$this->find_order($key,$current->data))
{
$current=$current->leftChild;
}
else
{
$current=$current->rightChild;
}
if($current==null)
return(null);
}
return($current->data);
}// end the function to search
public function delete1($key)
{
$current=$this->root;
$parent=$this->root;
$isLeftChild=true;
while($current->data!=$key)
{
$parent=$current;
if($key==($this->find_order($key,$current->data)))
{
$current=$current->leftChild;
$isLeftChild=true;
}
else
{
$current=$current->rightChild;
$isLeftChild=false;
}
if($current==null)
return(null);
}//end while loop
echo "<br/><br/>Node to delete:".$current->data;
//to delete a leaf node
if($current->leftChild==null&&$current->rightChild==null)
{
if($current==$this->root)
$this->root=null;
else if($isLeftChild==true)
{
$parent->leftChild=null;
}
else
{
$parent->rightChild=null;
}
return($current);
}//end if1
//to delete a node having a leftChild
else if($current->rightChild==null)
{
if($current==$this->root)
$this->root=$current->leftChild;
else if($isLeftChild==true)
{
$parent->leftChild=$current->leftChild;
}
else
{
$parent->rightChild=$current->leftChild;
}
return($current);
}//end else if1
//to delete a node having a rightChild
else if($current->leftChild==null)
{
if($current==$this->root)
$this->root=$current->rightChild;
else if($isLeftChild==true)
{
$parent->leftChild=$current->rightChild;
}
else
{
$parent->rightChild=$current->rightChild;
}
return($current);
}
//to delete a node having both childs
else
{
$successor=$this->get_successor($current);
if($current==$this->root)
{
$this->root=$successor;
}
else if($isLeftChild==true)
{
$parent->leftChild=$successor;
}
else
{
$parent->rightChild=$successor;
}
$successor->leftChild=$current->leftChild;
return($current);
}
}//end the function to delete a node
//Function to find the successor node
public function get_successor($delNode)
{
$succParent=$delNode;
$successor=$delNode;
$temp=$delNode->rightChild;
while($temp!=null)
{
$succParent=$successor;
$successor=$temp;
$temp=$temp->leftChild;
}
if($successor!=$delNode->rightChild)
{
$succParent->leftChild=$successor->rightChild;
$successor->rightChild=$delNode->rightChild;
}
return($successor);
}
//function to find the order of two strings
public function find_order($str1,$str2)
{
$str1=strtolower($str1);
$str2=strtolower($str2);
$i=0;
$j=0;
$p1=$str1[i];
$p2=$str2[j];
while(true)
{
if(ord($p1)<ord($p2)||($p1==''&&$p2==''))
{
return($str1);
}
else
{
if(ord($p1)==ord($p2))
{
$p1=$str1[++$i];
$p2=$str2[++$j];
continue;
}
return($str2);
}
}//end while
} //end function find string order
public function is_empty()
{
if($this->root==null)
return(true);
else
return(false);
}
}//end class BinaryTree
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.