PHPackages                             rstoetter/cbalancedbinarytree-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. rstoetter/cbalancedbinarytree-php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rstoetter/cbalancedbinarytree-php
=================================

The repository rstoetter\\cbalancedbinarytree-php implements the class cBalancedBinaryTree in PHP which is a balanced binary tree.

v1.0.2(8y ago)0361MITPHPPHP &gt;=7.0

Since Mar 25Pushed 8y agoCompare

[ Source](https://github.com/rstoetter/cbalancedbinarytree-php)[ Packagist](https://packagist.org/packages/rstoetter/cbalancedbinarytree-php)[ RSS](/packages/rstoetter-cbalancedbinarytree-php/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (3)DependenciesVersions (4)Used By (1)

The repository rstoetter\\cbalancedbinarytree-php implements the class cBalancedBinaryTree in PHP which is a balanced binary tree.

You will need PHP 7 or later to use this repository

The class cBalancedBinaryTree consists of nodes of the type cBalancedBinaryTreeNode. You provide a class which holds the data part, a class subclassing cBalancedTreeNode, which manages the class with the data and a class subclassing cBalancedBinaryTree, which handles the class, which was subclassed from cBalancedBinaryTreeNode.

You need to know how to sort the Tree as you have to provide a unique key for each node, too. You could implement a method GetKey( ) in the class providing the data part to calculate such an unique key on the data it is holding.

An Example would be:

```
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
}

```

Installation
------------

[](#installation)

This project assumes you have composer installed. Simply add:

"require" : {

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

```

}

to your composer.json, and then you can simply install with:

composer install

More information
----------------

[](#more-information)

See the [project wiki](https://github.com/rstoetter/cbalancedbinarytree-php/wiki) for more information.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

2971d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f638bd6cc066cad10d9187777df8782f4b4ccea4eb2b29ffac141d50744a557c?d=identicon)[rstoetter](/maintainers/rstoetter)

---

Top Contributors

[![rstoetter](https://avatars.githubusercontent.com/u/24063757?v=4)](https://github.com/rstoetter "rstoetter (12 commits)")

---

Tags

phpclasstreebinarybalanced

### Embed Badge

![Health badge](/badges/rstoetter-cbalancedbinarytree-php/health.svg)

```
[![Health](https://phpackages.com/badges/rstoetter-cbalancedbinarytree-php/health.svg)](https://phpackages.com/packages/rstoetter-cbalancedbinarytree-php)
```

###  Alternatives

[loophp/phptree

An implementation of tree data structure

981.8M2](/packages/loophp-phptree)[yzen.dev/plain-to-class

Class-transformer to transform your dataset into a structured object

16293.9k6](/packages/yzendev-plain-to-class)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
