rstoetter/cbalancedbinarytree-php

rstoetter\cbalancedbinarytree-php 仓库实现了 PHP 中的 cBalancedBinaryTree 类,这是一个平衡二叉树。

v1.0.2 2018-03-26 12:05 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:55:13 UTC


README

rstoetter\cbalancedbinarytree-php 仓库实现了 PHP 中的 cBalancedBinaryTree 类,这是一个平衡二叉树。

使用此仓库需要 PHP 7 或更高版本

cBalancedBinaryTree 类由 cBalancedBinaryTreeNode 类型的节点组成。您提供包含数据部分的类,一个继承自 cBalancedTreeNode 的类,用于管理数据和继承自 cBalancedBinaryTree 的类,用于处理从 cBalancedBinaryTreeNode 继承的类。

您需要了解如何排序树,因为您还需要为每个节点提供一个唯一的键。您可以在提供数据部分的类中实现 GetKey( ) 方法,用于计算所持数据的唯一键。

一个例子是

class cMyTreeData {

    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 for 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( )
    
    
    
	

}   // class cMyTree

$my_tree = new cMyTree( );
$my_tree->MyInsert( 'data 1', 1 );
$my_tree->MyInsert( 'data 2', 2 );
$my_tree->MyInsert( 'data 3', 3 );

$obj = $my_tree->MySearch( 'data 1' );
if ( $obj !== false ) {
    // do something withe $obj
}

安装

该项目假设您已安装 composer。只需将以下内容添加到您的 composer.json 中,然后您可以使用以下命令安装:

"require" : {

"rstoetter/cbalancedbinarytree-php" : ">=1.0.0"

}

,然后您可以使用以下命令安装:

composer install

更多信息

有关更多信息,请参阅项目维基