/** * $sourceArr 原来的数组 * $key 主键 * $parentKey 与主键关联的父主键 * $childrenKey 生成的孩子的键名 * */ function arrayToTree($sourceArr, $key, $parentKey, $childrenKey) { $tempSrcArr = array(); foreach ($sourceArr as $v) { $tempSrcArr[$v[$key]] = $v; } $i = 0; $count = count($sourceArr); for($i = ($count - 1); $i >=0; $i--) { if (isset($tempSrcArr[$sourceArr[$i][$parentKey]])) { $tArr = array_pop($tempSrcArr); $tempSrcArr[$tArr[$parentKey]][$childrenKey] = (isset($tempSrcArr[$tArr[$parentKey]][$childrenKey]) && is_array($tempSrcArr[$tArr[$parentKey]][$childrenKey])) ? $tempSrcArr[$tArr[$parentKey]][$childrenKey] : array(); array_push ($tempSrcArr[$tArr[$parentKey]][$childrenKey], $tArr); } } return $tempSrcArr; }
/** * 将数组转换成树 * 例子:将 array( array('id'=>1,'parentId' => 0,'name'=> 'name1') ,array('id'=>2,'parentId' => 0,'name'=> 'name2') ,array('id'=>4,'parentId' => 1,'name'=> 'name1_4') ,array('id'=>15,'parentId' => 1,'name'=> 'name1_5') );转换成 * Array( [1] => Array([id] => 1 [parentId] => 0 [name] => name1 [children] => Array( [0] => Array([id] => 15,[parentId] => 1,[name] => name1_5) [1] => Array([id] => 4,[parentId] => 1,[name] => name1_4) ) ) [2] => Array([id] => 2,[parentId] => 0,[name] => name2) ) * @param array $sourceArr 要转换的数组 * @param string $key 数组中确认父子的key,例子中为“id” * @param string $parentKey 数组中父key,例子中为“parentId” * @param type $childrenKey 要在树节点上索引子节点的key,例子中为“children” * @return array 返回生成的树 */ function arrayToTree($sourceArr, $key, $parentKey, $childrenKey) { $tempSrcArr = array(); $allRoot = TRUE; foreach ($sourceArr as $v) { $isLeaf = TRUE; foreach ($sourceArr as $cv ) { if (($v[$key]) != $cv[$key]) { if ($v[$key] == $cv[$parentKey]) { $isLeaf = FALSE; } if ($v[$parentKey] == $cv[$key]) { $allRoot = FALSE; } } } if ($isLeaf) { $leafArr[$v[$key]] = $v; } $tempSrcArr[$v[$key]] = $v; } if ($allRoot) { return $tempSrcArr; } else { unset($v, $cv, $sourceArr, $isLeaf); foreach ($leafArr as $v) { if (isset($tempSrcArr[$v[$parentKey]])) { $tempSrcArr[$v[$parentKey]][$childrenKey] = (isset($tempSrcArr[$v[$parentKey]][$childrenKey]) && is_array($tempSrcArr[$v[$parentKey]][$childrenKey])) ? $tempSrcArr[$v[$parentKey]][$childrenKey] : array(); array_push ($tempSrcArr[$v[$parentKey]][$childrenKey], $v); unset($tempSrcArr[$v[$key]]); } } unset($v); return arrayToTree($tempSrcArr, $key, $parentKey, $childrenKey); } }
/**递归方法:**/ $rows = array( 0 => array('id' => 1, 'name' => '菜单1', 'parentId' => 0) , 1 => array('id' => 2, 'name' => '菜单2', 'parentId' => 0) , 2 => array('id' => 3, 'name' => '菜单3', 'parentId' => 0) , 3 => array('id' => 4, 'name' => '菜单1_1', 'parentId' => 1) , 4 => array('id' => 5, 'name' => '菜单1_2', 'parentId' => 1) , 5 => array('id' => 6, 'name' => '菜单2_1', 'parentId' => 2) ); print_r(getTree($rows, 0, 'id', 'parentId')); /** * 数组根据父id生成树 * @staticvar int $depth 递归深度 * @param array $data 数组数据 * @param integer $pid 父id的值 * @param string $key id在$data数组中的键值 * @param string $chrildKey 要生成的子的键值 * @param string $pKey 父id在$data数组中的键值 * @param int $maxDepth 最大递归深度,防止无限递归 * @return array 重组后的数组 */ function getTree($data, $pid = 0, $key = 'id', $pKey = 'parentId', $childKey = 'child', $maxDepth = 0){ static $depth = 0; $depth++; if (intval($maxDepth) <= 0) { $maxDepth = count($data) * count($data); } if ($depth > $maxDepth) { exit("error recursion:max recursion depth {$maxDepth}"); } $tree = array(); foreach ($data as $rk => $rv) { if ($rv[$pKey] == $pid) { $rv[$childKey] = getTree($data, $rv[$key], $key, $pKey, $childKey, $maxDepth); $tree[] = $rv; } } return $tree; }
update 2014-04-17
/** * 生成树列表 * */ static function createGroupSelect($haystack, $focusOpt = false){ $result = '<select name="pid">'; $groupTree = getTree($haystack, 0, 'ag_id', 'ag_pid'); echo $result.createOption($groupTree, $focusOpt).'</select>'; } /** * 根据树生成option * */ static function createOption($tree, $focusOpt = false, $deepth = 0){ foreach($tree as $gtv){ $nullStr = ($deepth == 0) ? '' : str_repeat(' ', $deepth * 4 - 1); $preStr = ($deepth == 0) ? '' : (isset($gtv['last']) ? '└' : '├'); if ($focusOpt && ($gtv['ag_id'] == $focusOpt)){ $result .= '<option value="'.$gtv['ag_id'].'" selected >'.$nullStr.$preStr.$gtv['ag_name'].'</option>'; }else{ $result .= '<option value="'.$gtv['ag_id'].'">'.$nullStr.$preStr.$gtv['ag_name'].'</option>'; } if(!empty($gtv['child'])){ $result .= createOption($gtv['child'], $focusOpt, $deepth+1); } } return $result; } /** * 数组根据父id生成树 * @staticvar int $depth 递归深度 * @param array $data 数组数据 * @param integer $pid 父id的值 * @param string $key id在$data数组中的键值 * @param string $chrildKey 要生成的子的键值 * @param string $pKey 父id在$data数组中的键值 * @param int $maxDepth 最大递归深度,防止无限递归 * @return array 重组后的数组 */ static function getTree($data, $pid = 0, $key = 'id', $pKey = 'parentId', $childKey = 'child', $maxDepth = 0){ static $depth = 0; $depth++; if (intval($maxDepth) <= 0) { $maxDepth = count($data) * count($data); } if ($depth > $maxDepth) { exit("error recursion:max recursion depth {$maxDepth}"); } $tree = array(); foreach ($data as $rk => $rv) { if ($rv[$pKey] == $pid) { $rv[$childKey] = getTree($data, $rv[$key], $key, $pKey, $childKey, $maxDepth); $tree[] = $rv; } } if(!empty($tree)){ $tree[count($tree) - 1]['last'] = 1; } return $tree; }
相关推荐
js数组转换成tree结构数组算法
php 数组转换成树形结构输出代码php 数组转换成树形结构输出代码php 数组转换成树形结构输出代码
题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的...将有序数组转换为二叉搜索树的结果肯定是不唯一的,因为存在多种建树方法。
English |智能阵列到树 快速将大量数据数组转换为嵌套数据结构! 使用阵列长度为46086的测试数据,只需花费约0.1秒。安装与使用在Node.js中: $ npm i smart-arraytotree --save var smartArrayToTree = require ( '...
将有序数组转换为二叉搜索树.md
ArrayTree 是一种将二维数组转换为树结构(也称为嵌套或分层)数组的工具。 下面是一个例子: $ dataList = array ( array ( 'id' => 1 , 'parent_id' => 0 , 'name' => 'games' ), array ( 'id' => 2 , ...
算法笔记,将有序数组转为二叉搜索树
关于二叉树的程序,分别对无序数组和有序数组建立二叉树,实现遍历和查找。
利用C语言实现树形数组到默认中缀式的转换,算法比较幼稚,各位大神轻喷啊
示例1给定有序数组: [-10,-3,0,5,9],一个可能的答案是:[0,-3,9,-10,null,5],它可以表示下面这个高度平衡二叉搜索树:代码* De
示例:给定有序数组: [-10,-3,0,5,9],一个可能的答案是:[0,-3,9,-10,null,5],它可以表示下面这个高度平衡二叉搜索树:解题思路将有
本文为用Vue实现方式,另有一篇为用knockout.js的实现方法。 html代码 <table v-for=item v-bind:list=item></table> 组件模板代码 <script type=text/x-template id=table-component-tem
主要介绍了JavaScript将数组转换为链表的方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
示例:给定有序数组: [-10,-3,0,5,9],一个可能的答案是:[0,-3,9,-10,null,5],它可以表示下面这个高度平衡二叉搜索树:1、思路由二
JS 将有父子关系的平行数组转换成树形数据 方法:双重遍历,一次遍历parentId,一次遍历id == parendId; 该方法应该能很容易被想到,实现起来也一步一步可以摸索出来; const oldData = [ {id:1,name:'boss',...
树结构使用实例-实现数组和树结构的转换
示例 1:解释:[0,-10,5,null,-3,null,9] 也将被视为正确答案:示例 2:输出:[3,1]解释:[1,3] 和 [3,1] 都是高度平衡二
图+查找+排序+循环链表+循环链表+数组+广义表+二叉树与树的转换+哈夫曼树
题目:将[0,1,2,3,4,5,6,7,8,9,10]存储到二叉树,原数组有序,转换为二叉排序树。 二叉排序树的特点:当前节点的左子树上的所有节点都小于该节点,右子树上的所有节点都小于该节点。 二叉排序也称为二叉查找树。 我...