You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.1 KiB

  1. <?php
  2. /*
  3. This file is part of ActiveLink PHP XML Package (www.active-link.com).
  4. Copyright (c) 2002-2004 by Zurab Davitiani
  5. You can contact the author of this software via E-mail at
  6. hattrick@mailcan.com
  7. ActiveLink PHP XML Package is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU Lesser General Public License as published by
  9. the Free Software Foundation; either version 2.1 of the License, or
  10. (at your option) any later version.
  11. ActiveLink PHP XML Package is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public License
  16. along with ActiveLink PHP XML Package; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * Tree class provides a base for Tree-Branch-Leaf trio
  21. * @class Tree
  22. * @package org.active-link.xml
  23. * @author Zurab Davitiani
  24. * @version 0.4.0
  25. * @see Branch, Leaf
  26. */
  27. class Tree {
  28. // protected variables
  29. var $nodes;
  30. var $id = 0;
  31. /**
  32. * Constructor for the object
  33. * @method Tree
  34. * @returns none
  35. */
  36. function Tree() {
  37. $this->nodes = array();
  38. }
  39. /**
  40. * Adds given node to the Tree
  41. * @method addNode
  42. * @param mixed id
  43. * @param mixed node
  44. * @returns true if successful, false otherwise
  45. */
  46. function addNode($id, $node) {
  47. $success = true;
  48. if($id == -1)
  49. $this->nodes[] = $node;
  50. else
  51. if(isset($this->nodes[$id]))
  52. $success = false;
  53. else
  54. $this->nodes[$id] = $node;
  55. return $success;
  56. }
  57. /**
  58. * Removes all nodes
  59. * @method removeAllNodes
  60. * @returns none
  61. */
  62. function removeAllNodes () {
  63. $this->nodes = array();
  64. }
  65. /**
  66. * Removes specified node from the Tree
  67. * @method removeNode
  68. * @param mixed id
  69. * @returns true if successful, false otherwise
  70. */
  71. function removeNode($id) {
  72. $success = false;
  73. if(isset($this->nodes[$id])) {
  74. unset($this->nodes[$id]);
  75. $success = true;
  76. }
  77. return $success;
  78. }
  79. }
  80. ?>