特点:
- 一次加载所有数据 array(id, pid), array(id, pid)
- 通过一次循环构建树型结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
/** * Class BTree * @package j\tree\tests * @property array $tree */ class BTree { protected $source; public $index = []; public $idKey = 'id'; public $pidKey = 'pid'; public $childKey = 'child'; /** * BTree constructor. * @param $source */ public function __construct($source){ $this->source = $source; } public function __get($name){ if($name == 'tree'){ return $this->tree = $this->buildTree(); } else { return null; } } protected function buildTree(){ $keyId = $this->idKey; $keyPid = $this->pidKey; $keyChild = $this->childKey; $tree = array(); $childMap =& $this->index; foreach($this->source as $item) { $item[$keyChild] = array(); //给每个节点附加一个child项 $key = $item[$keyId]; if($item[$keyPid] == 0) { if(isset($childMap[$key])){ // 已设置当前子类映射 $childMap[$key] += $item; $tree[$key] =& $childMap[$key]; } else { $tree[$key] = $item; $childMap[$key] =& $tree[$key]; } }else { $pid = $item[$keyPid]; if(isset($childMap[$key])){ // 已设置当前子类映射 $childMap[$key] += $item; $childMap[$pid][$keyChild][$key] =& $childMap[$key]; } else { $childMap[$pid][$keyChild][$key] = $item; $childMap[$key] =& $childMap[$pid][$keyChild][$key]; } } } return $tree; } function getTree(){ return $this->tree; } /** * @param $id * @return array */ function getParent($id){ return $this->index[$id]; } function getParents($id, $self = false, $maxLevel = 6) { $keyId = $this->idKey; $keyPid = $this->pidKey; $item = $this->index[$id]; $parents = $self ? [$id => $item] : []; $i = 0; while(isset($this->index[$item[$keyPid]])){ $item = $parents[$item[$keyId]] = $this->index[$item[$keyPid]]; if($i++ > $maxLevel){ break; } } return array_reverse($parents, true); } function getParentIds($id, $self = false, $maxLevel = 6){ return array_keys($this->getParents($id, $self, $maxLevel)); } function parentId($id){ return $this->index[$id][$this->pidKey]; } /** * @param $id * @return array */ function getChild($id){ return $this->index[$id][$this->childKey]; } function getChildIdentifying($id, $self = false, $recursive = true){ $ids = $self ? [$id] : array(); $items = $this->index[$id][$this->childKey]; if($recursive){ while($item = array_pop($items)){ $ids[] = $item[$this->idKey]; if(count($item[$this->childKey]) > 0){ foreach($item[$this->childKey] as $_item){ array_push($items, $_item); } } } } else { $ids = array_merge($ids, array_keys($items[$this->childKey])); } return $ids; // array_walk_recursive($this->tree[$id]['child'], function($item, $key) use(& $ids){ // if($key == 'id'){ // $ids[] = $item; // } // }); } }; // b+树 // 测试数据 $data = array( array('id' => 1, 'pid' => 0), array('id' => 4, 'pid' => 2), array('id' => 3, 'pid' => 2), array('id' => 2, 'pid' => 1), ); $t = new BTree($data); $tree = $t->getTree(); assert_expr(count($tree) == 1, 'ROOT NODE TEST'); assert_expr(count($tree[1]['child']) == 1, 'Child test1'); assert_expr(count($tree[1]['child'][2]['child']) == 2, 'Child test1'); assert_expr(count($t->getChildIdentifying(1)) == 3, 'Child ids'); assert_expr(count($t->getChildIdentifying(2)) == 2, 'Child ids'); assert_expr(count($t->getChildIdentifying(3)) == 0, 'Child ids'); assert_expr(count($t->getChildIdentifying(4)) == 0, 'Child ids'); |