目录
一、商品无限极分类
id | pid | path | name |
1 | 0 | 0-1 | 女装 |
2 | 0 | 0-2 | 男装 |
3 | 1 | 0-1-3 | 休闲女装 |
4 | 1 | 0-1-4 | 潮流女装 |
5 | 1 | 0-1-5 | 品牌女装 |
$list = [
['id' => 1, 'pid' => 0, 'path' => '0-1', 'name' => '女装'],
['id' => 2, 'pid' => 0, 'path' => '0-2', 'name' => '男装'],
['id' => 3, 'pid' => 1, 'path' => '0-1-3', 'name' => '休闲女装'],
['id' => 4, 'pid' => 1, 'path' => '0-1-4', 'name' => '潮流女装'],
['id' => 5, 'pid' => 1, 'path' => '0-1-5', 'name' => '品牌女装']
];
function getChild($list) {
$list2 = $list3 = [];
foreach($list as $val) $list2[$val['id']]=$val;
foreach($list2 as $val) $list2[$val['pid']]['submenu'][] = &$list2[$val['id']];
$list3 = isset($list2[0]['submenu']) ? $list2[0]['submenu'] : [];
return $list3 ;die;
}
echo json_encode( getChild($list) );
/*[
{
"id":1,
"pid":0,
"path":"0-1",
"name":"女装",
"submenu":[
{
"id":3,
"pid":1,
"path":"0-1-3",
"name":"休闲女装"
},
{
"id":4,
"pid":1,
"path":"0-1-4",
"name":"潮流女装"
},
{
"id":5,
"pid":1,
"path":"0-1-5",
"name":"品牌女装"
}
]
},
{
"id":2,
"pid":0,
"path":"0-2",
"name":"男装"
}
]*/
二、按照树状返回父级列表和子集列表
function find_child($ar, $id='self_id', $pid='parent_id') {
foreach($ar as $v) $t[$v[$id]] = $v;
foreach ($ar as $item){//$t亦可
if( isset($item[$pid]) ) {//数据中有pid=0
$t[$item[$pid]]['child'][] = &$t[$item[$id]];
}
}
return $t;
}
function find_parent($ar, $id='id', $pid='parent_id') {
foreach($ar as $v) $t[$v[$id]] = $v;
foreach($ar as $item) {
if( isset($t[$item[$pid]]) ) {
$t[$item[$id]]['parent'][] = &$t[$item[$pid]];
}
}
return $t;
/*方法2:
foreach($ar as $v) $t[$v[$id]] = $v;
foreach ($t as $item){
//这里$item[$pid]而不是isset($item[$pid])是去掉pid=0的,运行下就知道了
if( $item[$pid] && !isset($t[$item[$pid]]['parent'][$item[$pid]]) ) {
$t[$item[$id]]['parent'][$item[$pid]] = &$t[$item[$pid]];
}
}*/
}
function find_child_ids($all_data, $cur_id, &$res) {
$childs = $all_data[$cur_id]['child']??[];
if(!empty($childs)) {
foreach($childs as $child) {
$res[] = $child['id'];
}
foreach ($childs as $child) {
find_child_ids($all_data, $child['id'], $res);
}
}
}
function find_parent_ids($all_data, $cur_id, &$res) {
$parents = $all_data[$cur_id]['parent']??[];
if(!empty($parents)) {
foreach($parents as $parent) {
$res[] = $parent['id'];
}
foreach($parents as $parent) {
find_parent_ids($all_data, $parent['id'], $res);
}
}
}
$data = array(
array('id'=>1, 'pid'=>0, 'name'=>'祖父'),
array('id'=>2, 'pid'=>1, 'name'=>'父亲'),
array('id'=>3, 'pid'=>1, 'name'=>'叔伯'),
array('id'=>4, 'pid'=>2, 'name'=>'自己'),
array('id'=>5, 'pid'=>4, 'name'=>'儿子'),
);
$all_child = find_child($data, 'id', 'pid');
$all_parent = find_parent($data, 'id', 'pid');
$child_ids=[]; $cur_id="1";
find_child_ids($all_child, $cur_id, $child_ids);
$parent_ids=[]; $cur_id="5";
find_parent_ids($all_parent, $cur_id, $parent_ids);
echo json_encode([
'all_child' => $all_child,
'child_ids' => $child_ids,
'all_parent' => $all_parent,
'parent_ids' => $parent_ids
]);
/*{
"all_child":{
"0":{
"child":[
{
"id":1,
"pid":0,
"name":"祖父",
"child":[
{
"id":2,
"pid":1,
"name":"父亲",
"child":[
{
"id":4,
"pid":2,
"name":"自己",
"child":[
{
"id":5,
"pid":4,
"name":"儿子"
}
]
}
]
},
{
"id":3,
"pid":1,
"name":"叔伯"
}
]
}
]
},
"1":{
"id":1,
"pid":0,
"name":"祖父",
"child":[
{
"id":2,
"pid":1,
"name":"父亲",
"child":[
{
"id":4,
"pid":2,
"name":"自己",
"child":[
{
"id":5,
"pid":4,
"name":"儿子"
}
]
}
]
},
{
"id":3,
"pid":1,
"name":"叔伯"
}
]
},
"2":{
"id":2,
"pid":1,
"name":"父亲",
"child":[
{
"id":4,
"pid":2,
"name":"自己",
"child":[
{
"id":5,
"pid":4,
"name":"儿子"
}
]
}
]
},
"3":{
"id":3,
"pid":1,
"name":"叔伯"
},
"4":{
"id":4,
"pid":2,
"name":"自己",
"child":[
{
"id":5,
"pid":4,
"name":"儿子"
}
]
},
"5":{
"id":5,
"pid":4,
"name":"儿子"
}
},
"child_ids":[
2,
3,
4,
5
],
"all_parent":{
"1":{
"id":1,
"pid":0,
"name":"祖父"
},
"2":{
"id":2,
"pid":1,
"name":"父亲",
"parent":[
{
"id":1,
"pid":0,
"name":"祖父"
}
]
},
"3":{
"id":3,
"pid":1,
"name":"叔伯",
"parent":[
{
"id":1,
"pid":0,
"name":"祖父"
}
]
},
"4":{
"id":4,
"pid":2,
"name":"自己",
"parent":[
{
"id":2,
"pid":1,
"name":"父亲",
"parent":[
{
"id":1,
"pid":0,
"name":"祖父"
}
]
}
]
},
"5":{
"id":5,
"pid":4,
"name":"儿子",
"parent":[
{
"id":4,
"pid":2,
"name":"自己",
"parent":[
{
"id":2,
"pid":1,
"name":"父亲",
"parent":[
{
"id":1,
"pid":0,
"name":"祖父"
}
]
}
]
}
]
}
},
"parent_ids":[
4,
2,
1
]
}*/
三、判断两棵树是否相同
/*
思路:判断两棵树是否相同
def isSameTree(p, q):
if p == None and q == None:
return True
elif p and q :
return p.val == q.val and isSameTree(p.child,q.child)
else :
return False
*/
function isSameTree($a, $b) {
//step1:判断自己是否一样
if(empty($a) && empty($b)) return true;
if(empty($a) || empty($b)) {echo '1个null'.json_encode([$a, $b]);return false;}
if($a['id']!=$b['id'] || $a['name']!=$b['name'] || $a['pid']!=$b['pid']) {
echo '内容不一样'.json_encode([[$a['id'],$b['id']],[$a['name'],$b['name']],[$a['pid'],$b['pid']]]);
return false;
}
//step2:判断孩子是否一样
if( ( isset($a['child']) && !isset($a['child']) )
|| ( !isset($a['child']) && isset($a['child']) )
|| ( isset($a['child']) && isset($a['child']) && count($a['child'])!=count($b['child']) )
) {
echo '孩子不一样'.json_encode([$a['child']??[],$b['child']??[]]);
return false;
}
$achild = $bchild=[];
foreach($a['child'] as $v1) $achild[$v1['id']]=$v1;
foreach($b['child'] as $v2) $bchild[$v2['id']]=$v2;
foreach($achild as $id => $v) {
$res = isSameTree($v, $bchild[$id]);
if(!$res) return false;
}
//step3:走到最后,返回true
return true;
}
$a = '{
"id":1,
"pid":0,
"name":"祖父",
"child":[
{
"id":2,
"pid":1,
"name":"父亲",
"child":[
{
"id":4,
"pid":2,
"name":"自己",
"child":[
{
"id":5,
"pid":4,
"name":"儿子"
}
]
}
]
},
{
"id":3,
"pid":1,
"name":"叔伯"
}
]
}';
$b = '{
"id":1,
"pid":0,
"name":"祖父",
"child":[
{
"id":2,
"pid":1,
"name":"父亲",
"child":[
{
"id":4,
"pid":2,
"name":"自己2",
"child":[
{
"id":5,
"pid":4,
"name":"儿子"
},
{
"id":5,
"pid":4,
"name":"儿子2"
}
]
}
]
},
{
"id":3,
"pid":1,
"name":"叔伯"
}
]
}';
$a=json_decode($a, true); $b=json_decode($b, true);
$res = isSameTree($a, $b);
$res2 = $res==true ? ('true') : ('false');
echo $res2;die;
内容不一样[[4,4],["自己","自己22"],[2,2]]
false
四、计算树的高度
/* 思路
def maxDepth(root):
if not root: return 0
return max(maxDepth(root.left), maxDepth(root.right)) + 1
*/