php树型类实例
2015-01-24信息快讯网
这篇文章主要介绍了php树型类,涉及数据结构与算法中的树结构,实例相对简单易懂,对于学习数据结构有一定的参考借鉴价值,需要的朋友可以参考下
本文实例讲述了php树型类。分享给大家供大家参考。具体分析如下:
该实例原理简单,学过数据结构的一看就明白是什么道理了,不过今天在使用时数据中出现了子节点id(71)小于父节点id(104).导致部分子节点没被存储入数组,修改了一下,实例代码如下:
<?php
class tree
{
var $data = array();
var $child = array(-1=>array());
var $layer = array(-1=>-1);
var $parent = array();
var $num = array();
function setnode($id, $parent, $value,$num=0)
{
$parent = $parent ? $parent : 0;
$this->data[$id] = $value;
$this->num[$id] = $num;
if (!isset($this->child[$id])) $this->child[$id] = array();
$this->child[$parent][] = $id;
$this->parent[$id] = $parent;
if (!isset($this->layer[$parent]) && $parent == 0)
{
$this->layer[$id] = 0;
}
else
{
$this->layer[$id] = $this->layer[$parent] + 1;
}
}
function getlist(&$tree, $root= 0)
{
foreach ($this->child[$root] as $key=>$id)
{
$tree[] = $id;
if($this->child[$id]) $this->getlist($tree, $id);
}
}
function getvalue($id)
{
if($this->layer[$id]==0)
{
return $this->data[$id];
}
else
{
return $leftmar.$this->data[$id];
}
}
function getnum($id)
{
return $this->num[$id];
}
function getbitvalue($id)
{
return $this->data[$id];
}
function getlayer($id, $space = false)
{
return $space ? str_repeat($space, $this->layer[$id]) : $this->layer[$id];
}
function getparent($id)
{
return $this->parent[$id];
}
function getparents($id)
{
while ($this->parent[$id] != -1)
{
$id = $parent[$this->layer[$id]] = $this->parent[$id];
}
ksort($parent);
reset($parent);
return $parent;
}
function getchild($id)
{
return $this->child[$id];
}
function getchilds($id = 0)
{
$child = array($id);
$this->getlist($child, $id);
return $child;
}
function printdata()
{
return $this->layer;
}
}
?>希望本文所述对大家的PHP程序设计有所帮助。
php使用正则表达式获取图片url的方法
php使用CURL伪造IP和来源实例详解
php+mysql实现无限分类实例详解
php截取html字符串及自动补全html标签的方法
php在linux下检测mysql同步状态的方法
php+mysql查询优化简单实例
PHP针对JSON操作实例分析
php中的动态调用实例分析
彻底删除thinkphp3.1案例blog标签的方法
Yii框架form表单用法实例
Yii不依赖Model的表单生成器用法实例
Yii分页用法实例详解
Dwz与thinkphp整合下的数据导出到Excel实例
yii的CURD操作实例详解
php生成随机颜色方法汇总
php实现使用正则将文本中的网址转换成链接标签
PHP多线程类及用法实例
php提取字符串中网站url地址的方法