lib_tree.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: lib_tree.php 23684 2011-08-04 02:24:59Z cnteacher $
  7. */
  8. if (!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class lib_tree {
  12. public $data = array();
  13. public $child = array(-1 => array());
  14. public $layer = array(-1 => -1);
  15. public $parent = array();
  16. public $countid = 0;
  17. public function __construct() {
  18. }
  19. public function setNode($id, $parent, $value) {
  20. $parent = $parent ? $parent : 0;
  21. $this->data[$id] = $value;
  22. $this->child[$parent][] = $id;
  23. $this->parent[$id] = $parent;
  24. if (!isset($this->layer[$parent])) {
  25. $this->layer[$id] = 0;
  26. } else {
  27. $this->layer[$id] = $this->layer[$parent] + 1;
  28. }
  29. }
  30. public function getList(&$tree, $root= 0) {
  31. foreach ($this->child[$root] as $key => $id) {
  32. $tree[] = $id;
  33. if ($this->child[$id])
  34. $this->getList($tree, $id);
  35. }
  36. }
  37. public function getValue($id) {
  38. return $this->data[$id];
  39. }
  40. public function reSetLayer($id) {
  41. if ($this->parent[$id]) {
  42. $this->layer[$this->countid] = $this->layer[$this->countid] + 1;
  43. $this->reSetLayer($this->parent[$id]);
  44. }
  45. }
  46. public function getLayer($id, $space = false) {
  47. $this->layer[$id] = 0;
  48. $this->countid = $id;
  49. $this->reSetLayer($id);
  50. return $space ? str_repeat($space, $this->layer[$id]) : $this->layer[$id];
  51. }
  52. public function getParent($id) {
  53. return $this->parent[$id];
  54. }
  55. public function getParents($id) {
  56. while ($this->parent[$id] != -1) {
  57. $id = $parent[$this->layer[$id]] = $this->parent[$id];
  58. }
  59. ksort($parent);
  60. reset($parent);
  61. return $parent;
  62. }
  63. public function getChild($id) {
  64. return $this->child[$id];
  65. }
  66. public function getChilds($id = 0) {
  67. $child = array();
  68. $this->getList($child, $id);
  69. return $child;
  70. }
  71. }
  72. ?>