VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • php树型类实例

这篇文章主要介绍了php树型类,涉及数据结构与算法中的树结构,实例相对简单易懂,对于学习数据结构有一定的参考借鉴价值,需要的朋友可以参考下

本文实例讲述了php树型类。分享给大家供大家参考。具体分析如下:

该实例原理简单,学过数据结构的一看就明白是什么道理了,不过今天在使用时数据中出现了子节点id(71)小于父节点id(104).导致部分子节点没被存储入数组,修改了一下,实例代码如下:

  1. <?php 
  2. class tree 
  3. { 
  4.     var $data = array(); 
  5.     var $child = array(-1=>array()); 
  6.     var $layer = array(-1=>-1); 
  7.     var $parent = array(); 
  8.     var $num = array(); 
  9.  
  10.     function setnode($id, $parent, $value,$num=0) 
  11.     { 
  12.         $parent = $parent ? $parent : 0; 
  13.  
  14.         $this->data[$id]  = $value; 
  15.         $this->num[$id]      = $num; 
  16.         if (!isset($this->child[$id])) $this->child[$id] = array(); 
  17.         $this->child[$parent][] = $id; 
  18.         $this->parent[$id]  = $parent; 
  19.  
  20.         if (!isset($this->layer[$parent]) && $parent == 0) 
  21.         { 
  22.            $this->layer[$id] = 0; 
  23.         } 
  24.         else 
  25.         { 
  26.             $this->layer[$id] = $this->layer[$parent] + 1; 
  27.         } 
  28.     } 
  29.  
  30.     function getlist(&$tree, $root= 0) 
  31.     { 
  32.         foreach ($this->child[$root] as $key=>$id) 
  33.         { 
  34.             $tree[] = $id; 
  35.             if($this->child[$id]) $this->getlist($tree, $id); 
  36.         } 
  37.     } 
  38.  
  39.     function getvalue($id) 
  40.     { 
  41.    if($this->layer[$id]==0) 
  42.    { 
  43.     return $this->data[$id]; 
  44.    } 
  45.    else 
  46.    { 
  47.     return $leftmar.$this->data[$id]; 
  48.    } 
  49.     } 
  50.  
  51.     function getnum($id) 
  52.     { 
  53.    return $this->num[$id]; 
  54.     } 
  55.  
  56.     function getbitvalue($id) 
  57.     { 
  58.    return $this->data[$id]; 
  59.     } 
  60.  
  61.     function getlayer($id, $space = false) 
  62.     { 
  63.         return $space ? str_repeat($space, $this->layer[$id]) : $this->layer[$id]; 
  64.     } 
  65.  
  66.     function getparent($id) 
  67.     { 
  68.         return $this->parent[$id]; 
  69.     } 
  70.  
  71.     function getparents($id) 
  72.     { 
  73.         while ($this->parent[$id] != -1) 
  74.         { 
  75.             $id = $parent[$this->layer[$id]] = $this->parent[$id]; 
  76.         } 
  77.  
  78.         ksort($parent); 
  79.         reset($parent); 
  80.  
  81.         return $parent; 
  82.     } 
  83.  
  84.     function getchild($id) 
  85.     { 
  86.         return $this->child[$id]; 
  87.     } 
  88.  
  89.     function getchilds($id = 0) 
  90.     { 
  91.         $child = array($id); 
  92.         $this->getlist($child, $id); 
  93.  
  94.         return $child; 
  95.     } //www.phpfensi.com 
  96.  
  97.     function printdata() 
  98.     { 
  99.         return $this->layer; 
  100.     } 
  101. } 
  102. ?> 

希望本文所述对大家的PHP程序设计有所帮助。

 



出处:http://www.phpfensi.com/php/20210502/14783.html


相关教程