PHP code example of locr-company / splay-tree

1. Go to this page and download the library: Download locr-company/splay-tree library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

locr-company / splay-tree example snippets




use Locr\Lib\SplayTree\SplayTree;

$tree = new SplayTree();



use Locr\Lib\SplayTree\SplayTree;

$t = new SplayTree();
$t->insert(5);
$t->insert(-10);
$t->insert(0);
$t->insert(33);
$t->insert(2);

print_r($t->keys());
/**
 * Array(
 *  [0] => -10  
 *  [1] => 0  
 *  [2] => 2  
 *  [3] => 5  
 *  [4] => 33
 * )
 */
print $t->size;  // 5
print $t->min(); // -10
print $t->max(); // 33

$t->remove(0);
print $t->size;   // 4



use Locr\Lib\SplayTree\SplayTree;

$t = new SplayTree(function ($a, $b) {
    return $b - $a;
});
$t->insert(5);
$t->insert(-10);
$t->insert(0);
$t->insert(33);
$t->insert(2);

print_r($t->keys());
/**
 * Array
 *(
 *  [0] => 33
 *  [1] => 5
 *  [2] => 2
 *  [3] => 0
 *  [4] => -10
 *)
 */



use Locr\Lib\SplayTree\SplayTree;

$t = new SplayTree();
$t->load([3, 2, -10, 20], ['C', 'B', 'A', 'D']);
print_r($t->keys());
/**
 * Array
 *(
 *  [0] => 3
 *  [1] => 2
 *  [2] => -10
 *  [3] => 20
 *)
 */
print_r($t->values());
/**
 * Array
 *(
 *  [0] => C
 *  [1] => B
 *  [2] => A
 *  [3] => D
 *)
 */