pub rstoetter cbalancedbinarytree cBalancedBinaryTree - rstoetter/cbalancedbinarytree-php GitHub Wiki
rstoetter\cbalancedbinarytree\cBalancedBinaryTree - of API Reference pub
- Class name: cBalancedBinaryTree
- Namespace: rstoetter\cbalancedbinarytree
description
The class cBalancedBinaryTree implements a balanced binary tree, which manages nodes of the type cBalancedBinaryTreeNode Normally you subclass your own class from cBalancedBinaryTree, which will handle the nodes in a more useful way you need
This version of the tree is not able to delete nodes
Usage:
class cMyTreeData {
// this class holds the data
public $m_data_1 = '';
public $m_data_2 = -1;
function __construct( string $data_1, int $data_2 ){
$this->m_data_1 = $data_1;
$this->m_data_1 = $data_2;
} // function __construct( )
public function GetKey( ) : string {
// calculate the unique sort key of the Tree
return "{$this->m_data_1}-{$this->m_data_2}";
} // function GetKey( )
// other methods here
} // class cMyTreeData
class cMyTreeNode extends \\rstoetter\\cbalancedbinarytree\\cBalancedBinaryTreeNode {
function __construct( string $data_1, int $data_2 ){
$obj_data = new cMyTreeData( $data_1, $data_2 );
parent::__construct( $obj_data->GetKey( ), $obj_data );
} // function __construct( )
public function __toString( ) : string {
$ret = $this->_data->GetKey();
return $ret;
} // function __toString( )
} // class cMyTreeNode
class cMyTree extends \rstoetter\cbalancedbinarytree\cBalancedBinaryTree {
function __construct( ){
// do something here
// $this->RebalanceTree( );
} // function __construct( )
public function MyInsert( string $data_1, int $data_2 ) {
// inserts a new node in the tree
$obj_new = new cMyTreeNode( $data_1, $data_2 );
$this->InsertNode( $obj_new );
// maybe you have to rebalance the tree now
// NOTE: if you are filling the tree with a bunch of data, then you can rebalance the tree after reading all objects, too
$this->RebalanceTree( );
} // function MyInsert( )
public function MySearch( string $key ) {
// returns an object of type cMyTreeData or false
$obj_found = $this->SearchByKey( $key );
// $obj_found is of type cBalancedBinaryTreeNode
if ( $obj_found !== false ) {
return $obj_found->GetData( );
}
return false;
} // function MySearch( )
public function PrintTree( ) : string {
// return the ordered keys of the Tree as a string
parent::_PrintTree( $this->m_root );
} // function PrintTree( )
protected function _TraverseInOrder( $tree ) {
// internal method to recurse the tree in order beginning with $tree and do something with each node
if ( is_null( $tree ) ) { return ; }
//
$this->_TraverseInOrder( $tree->GetLeft( ) );
//
$obj = $tree->GetData( );
//
// do something with $obj, which is of the type cMyTreeData
//
//
$this->_TraverseInOrder( $tree->GetRight( ) );
} // function _TraverseInOrder( )
public function TraverseInOrder( ) {
// traverse the whole tree in ordered order
// recurse the tree beginning with the root pointer
$this->_TraverseInOrder( $this->m_root );
} // function TraverseInOrder( )
properties of class cBalancedBinaryTree
- static public cBalancedBinaryTree::$m_version
methods of class cBalancedBinaryTree
public mixed rstoetter\cbalancedbinarytree\cBalancedBinaryTree::InsertNode(\rstoetter\cbalancedbinarytree\cBalancedBinaryTree $new_node)
public mixed rstoetter\cbalancedbinarytree\cBalancedBinaryTree::RebalanceTree()
public \rstoetter\cbalancedbinarytree\cBalancedBinaryTreeNode|boolean rstoetter\cbalancedbinarytree\cBalancedBinaryTree::SearchByKey(\rstoetter\cbalancedbinarytree\typo $key)
public string rstoetter\cbalancedbinarytree\cBalancedBinaryTree::__toString()