Source for file TreeRoot.class.php
Documentation is available at TreeRoot.class.php
* For including this file you have to define the constant "CLASSPATH".
* Because every include in the framework depends on the CLASSPATH definition.
* The CLASSPATH means the relative path to the folder that contains the
* @package data_structures
echo "<h3>You have to define the constant CLASSPATH!</h3>\r\n";
echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\r\n";
require_once( CLASSPATH. "data_structures/Vector.class.php" );
require_once( CLASSPATH. "data_structures/TreeNode.class.php" );
require_once( CLASSPATH. "core/Arrays.inc.php" );
* @package data_structures
* @author Daniel Plücken <daniel@debakel.net>
* @license http://www.gnu.org/copyleft/lesser.html
* GNU Lesser General Public License
* @copyright Copyright (C) 2005 Daniel Plücken <daniel@debakel.net>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
* Builds a tree from a given two dimensional array.
* @author Daniel Plücken <daniel@debakel.net>
* @param array &$array A two dimensional array.
* @param string $primary_key The keyname whose value represents an id.
* @param string $recursive_key The keyname whose value refers to the value
function buildTree( &$array, $primary_key, $recursive_key )
if( preg_match( "!^\d+$!", $array[0][$primary_key] ) )
for( $i = 0; $i < count( $array ); $i++ )
if( !empty( $array[$i][$recursive_key] ) )
foreach( $array[$i] as $key => $value )
$tmp_arr[count($tmp_arr)- 1]->$key = $value;
$last_key = count( $tmp_arr ) - 1;
$this->children[] = & $tmp_arr[$last_key];
foreach( $array[$i] as $key => $value )
$this->children[$last_key]->$key = $value;
$this->children[$last_key]->parent = & $this;
for( $i = 0; $i < count( $tmp_arr ); $i++ )
for( $k = 0; $k < count( $tmp_arr ); $k++ )
if( $tmp_arr[$i]->$primary_key == $tmp_arr[$k]->$recursive_key )
$tmp_arr[$i]->children[] = & $tmp_arr[$k];
$tmp_arr[$k]->parent = & $tmp_arr[$i];
* Adds a childnode of type TreeNode to this object.
* @author Daniel Plücken <daniel@debakel.net>
* @param TreeNode $obj_ref
function addChild( &$obj_ref, $pos = "" )
$this->parent = & $obj_ref;
is_a( $obj_ref, "TreeNode" )
$this->child->add( $obj_ref, $pos );
"<h3>The given parameter is not a reference "
. "of an object of a treenode!</h3>"
} // END of class TreeRoot
|