コード例 #1
0
 function test_current_badInnerIterator_throwsException()
 {
     $this->setExpectedException('\\Collections\\TypeException');
     $tree = new BinaryTree(0);
     $tree->setLeft(new BinaryTree(-1));
     $tree->setRight(new BinaryTree(1));
     $iterator = new SortedMapIterator(new InOrderIterator($tree, 0), 3);
     $iterator->current();
 }
コード例 #2
0
 function test_toArray()
 {
     $tree = new BinaryTree(0);
     $tree->setLeft(new BinaryTree(-4));
     $tree->left()->setLeft(new BinaryTree(1));
     $tree->left()->setRight(new BinaryTree(2));
     $iterator = $this->instance($tree, 4);
     $expect = [0, -4, 1, 2];
     $actual = $iterator->toArray();
     $this->assertEquals($expect, $actual);
 }
コード例 #3
0
 private function next_valueNotNull()
 {
     $right = $this->value->right();
     if ($right !== null) {
         $this->stack->push($right);
     }
     $this->next_push($this->value->left());
 }
コード例 #4
0
ファイル: Demo5.php プロジェクト: EdenChan/Instances
 /**
  * Main program.
  *
  * @param array $args Command-line arguments.
  * @return integer Zero on succes; non-zero on failure.
  */
 public static function main($args)
 {
     printf("Demonstration program number 5.\n");
     $status = 0;
     GeneralTree::main($args);
     BinaryTree::main($args);
     NaryTree::main($args);
     BinarySearchTree::main($args);
     AVLTree::main($args);
     MWayTree::main($args);
     BTree::main($args);
     return $status;
 }
コード例 #5
0
 public function test_removeRemovesElement()
 {
     $tree = new BinaryTree();
     $tree->insert(new types\NumericContainer(3));
     $tree->insert(new types\NumericContainer(2));
     $tree->insert(new types\NumericContainer(1));
     $tree->insert(new types\NumericContainer(5));
     $tree->insert(new types\NumericContainer(4));
     $tree->insert(new types\NumericContainer(7));
     $tree->insert(new types\NumericContainer(6));
     $tree->insert(new types\NumericContainer(8));
     $tree->remove(new types\NumericContainer(5));
     print_r($tree->toArray());
 }
コード例 #6
0
ファイル: AvlTreeTest.php プロジェクト: RadekDvorak/Ardent
 function testConstructor()
 {
     $avl = new AvlTree(function ($a, $b) {
         if ($a < $b) {
             return 1;
         } elseif ($b < $a) {
             return -1;
         } else {
             return 0;
         }
     });
     $avl->add(4);
     $avl->add(3);
     $avl->add(5);
     $root = new BinaryTree(4);
     $root->setLeft(new BinaryTree(5));
     $root->setRight(new BinaryTree(3));
     $this->reCalculateHeights($root);
     $this->assertEquals($root, $avl->toBinaryTree());
 }
コード例 #7
0
 public function testGetRightTree()
 {
     $obj_binaryTree = new BinaryTree('content', new BinaryTree('content L'), new BinaryTree('content R'));
     $this->assertEquals('content R', $obj_binaryTree->getRightTree()->getObject(), 'Right tree\'s content should equal content R.');
     $obj_binaryTreeEmpty = new BinaryTree();
     $this->assertNull($obj_binaryTreeEmpty->getRightTree(), 'Right tree should be null.');
 }
コード例 #8
0
 function ranges()
 {
     $groups = array();
     $Indexes = new BinaryTree();
     if ($this->range === null) {
         return $groups;
     }
     foreach (func_get_args() as $number_set) {
         if (is_int($number_set)) {
             $number_set = intval($number_set);
             if ($number_set < 0) {
                 $assigned_range = ceil(abs($number_set) / $this->range) * -1;
             } else {
                 $assigned_range = ceil(abs($number_set) / $this->range);
             }
             $Indexes->insert($assigned_range);
             if (!isset($groups[$assigned_range])) {
                 $groups[$assigned_range] = new BinaryTree();
             }
             $groups[$assigned_range]->insert($number_set);
         } else {
             throw new InvalidArgumentException('This method only accpets integers:' . $number_set);
         }
     }
     $array_indexes = $Indexes->traverse();
     $returned_array = array();
     print_r($array_indexes);
     print_r(array_keys($groups));
     foreach ($array_indexes as $index) {
         $returned_array[] = $groups[$index]->traverse();
     }
     return $returned_array;
 }
コード例 #9
0
 /**
  * @link http://php.net/manual/en/iterator.current.php
  * @return mixed
  */
 function current()
 {
     return $this->value->value();
 }
コード例 #10
0
ファイル: BinaryTree.php プロジェクト: EdenChan/Instances
 /**
  * Main program.
  *
  * @param array $args Command-line arguments.
  * @return integer Zero on success; non-zero on failure.
  */
 public static function main($args)
 {
     printf("BinaryTree main program.\n");
     $status = 0;
     $bt = new BinaryTree(box(4));
     $bt->attachLeft(new BinaryTree(box(2)));
     $bt->attachRight(new BinaryTree(box(6)));
     AbstractTree::test($bt);
     return $status;
 }
コード例 #11
0
function viewBinaryNetwork()
{
    global $wpdb;
    $obj = new BinaryTree();
    global $current_user;
    get_currentuserinfo();
    $username1 = $current_user->user_login;
    $var = $obj->network();
    global $wpdb;
    $res = mysql_fetch_array(mysql_query("SELECT user_key FROM {$wpdb->prefix}mlm_users WHERE username = '******'"));
    ?>
    <style type="text/css">
        span.owner
        {
            color:#339966; 
            font-style:italic;
        }
        span.paid
        {
            color: #669966!important; 
            /*background-color:#770000; */
            font-style:normal;
        }
        span.leg
        {
            color:red; 
            font-style:italic;
        }
    </style>
    <script type='text/javascript' src='https://www.google.com/jsapi'></script>
    <script type='text/javascript'>
        google.load('visualization', '1', {packages: ['orgchart']});
        google.setOnLoadCallback(drawChart);
        function drawChart() {
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Name');
            data.addColumn('string', 'Manager');
            data.addColumn('string', 'ToolTip');
            data.addRows([<?php 
    for ($i = 0; $i < count($var); $i++) {
        for ($j = 0; $j < count($var[$i]); $j++) {
            _e($var[$i][$j]);
        }
    }
    ?>
 ['', null, '']]);
            var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));
            chart.draw(data, {allowHtml: true});
        }
    </script>
    <script type="text/javascript" language="javascript">
        function searchUser()
        {
            var user = document.getElementById("username").value;
            if (user == "")
            {
                alert("Please enter username then searched.");
                document.getElementById("username").focus();
                return false;
            }
        }
    </script>
    <table border="0" cellspacing="0" cellpadding="0" >
        <tr>
            <td align="center">	
                <form action="<?php 
    bloginfo('url', 'binary-mlm-pro');
    ?>
/?page_id=<?php 
    echo $obj->view_page_id;
    ?>
&k=<?php 
    _e($res['user_key']);
    ?>
" method="post">
                    <input type="submit" value="<?php 
    echo __('YOU', 'binary-mlm-pro');
    ?>
">
                </form>
            </td>
            <td align="center">
                <form name="usersearch" id="usersearch" action="" method="post" onSubmit="return searchUser();">
                    <input type="text" name="username" id="username"> <input type="submit" name="search" value="<?php 
    echo __('Search', 'binary-mlm-pro');
    ?>
">
                </form>
            </td>
        </tr>               
    </table>
    <div style="margin:0 auto;padding:0px;clear:both; width:100%!important;" align="center">
        <div id='chart_div'></div>
    </div>
    <?php 
}
コード例 #12
0
ファイル: SplayTree.php プロジェクト: RadekDvorak/Ardent
 private function copyNode(SplayNode $n = null)
 {
     if ($n === null) {
         return null;
     }
     $new = new BinaryTree($n->value);
     $new->setLeft($this->copyNode($n->left));
     $new->setRight($this->copyNode($n->right));
     return $new;
 }
コード例 #13
0
ファイル: bt_test.php プロジェクト: brumar/DIANE_syntax
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Interface de Test</title>
<link rel="stylesheet" type="text/css" href="view.css" media="all">
<script type="text/javascript" src="view.js"></script>
<script type="text/javascript" src="userscript.js"></script>

</head>
<body id="main_body" >

<?php 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(-1);
require_once 'class.binarytree.php';
require_once 'class.binarynode.php';
echo "Hell yeah!<br />";
$testtree = new BinaryTree();
$testtree->root = new BinaryNode('+');
$testtree->root->left = new BinaryNode("coucou");
$testtree->root->right = new BinaryNode(3);
$testtree->traverse();
?>


</body>
コード例 #14
0
ファイル: SplayTreeTest.php プロジェクト: RadekDvorak/Ardent
 function testContains()
 {
     $object = new SplayTree();
     $this->assertFalse($object->contains(1));
     $object->add(0);
     $this->assertFalse($object->contains(1));
     $object->add(2);
     $this->assertFalse($object->contains(1));
     $object->add(1);
     $this->assertTrue($object->contains(1));
     $expected = new BinaryTree(1);
     $expected->setLeft(new BinaryTree(0));
     $expected->setRight(new BinaryTree(2));
     $actual = $object->toBinaryTree();
     $this->assertEquals($expected, $actual);
 }
コード例 #15
0
ファイル: LeftistHeap.php プロジェクト: EdenChan/Instances
 /**
  * Destructor.
  */
 public function __destruct()
 {
     parent::__destruct();
 }
コード例 #16
0
ファイル: BinaryTree.php プロジェクト: pramoddas/Ardent
 /**
  * @return int
  */
 function rightHeight()
 {
     return $this->right === null ? 0 : $this->right->height();
 }
コード例 #17
0
    for ($i = 0; $i <= count($arr1); $i++) {
        for ($j = 0; $j <= count($arr2); $j++) {
            if ($arr1[$i] == $arr2[$j]) {
                return $arr1[$i];
            }
        }
    }
}
$a = new Node(30);
$b = new Node(52);
$c = new Node(8);
$d = new Node(3);
$e = new Node(20);
$f = new Node(10);
$g = new Node(29);
$t = new BinaryTree();
$t->insert($a);
$t->insert($b);
$t->insert($c);
$t->insert($d);
$t->insert($e);
$t->insert($f);
$t->insert($g);
$fh = fopen($argv[1], "r");
while (!feof($fh)) {
    $test = trim(fgets($fh));
    $node_values = explode(" ", $test);
    if ($node_values[0] != "") {
        $parents1 = $t->search($node_values[0]);
        $parents2 = $t->search($node_values[1]);
        echo lewest_common($parents1, $parents2) . "\n";
コード例 #18
0
ファイル: binary_tree.php プロジェクト: pfuhrmann/phpalgos
     * Insert new node into tree
     *
     * @param TreeNode $node
     * @return $this
     */
    public function insert(TreeNode $node)
    {
        $this->_insert($node, $this->root);
        return $this;
    }
    /**
     * Check if tree is balanced
     *
     * @return bool True if tree is balanced
     */
    public function isBalanced()
    {
        return $this->_isBalanced($this->root);
    }
}
// Test cases
// Unbalanced Tree
$tree1 = new BinaryTree();
$tree1->insert(new TreeNode(3))->insert(new TreeNode(2))->insert(new TreeNode(4))->insert(new TreeNode(7))->insert(new TreeNode(6));
var_dump($tree1->isBalanced());
// bool -> false
// Balanced Tree
$tree2 = new BinaryTree();
$tree2->insert(new TreeNode(15))->insert(new TreeNode(10))->insert(new TreeNode(20))->insert(new TreeNode(8))->insert(new TreeNode(12))->insert(new TreeNode(16))->insert(new TreeNode(25));
var_dump($tree2->isBalanced());
// bool -> true
コード例 #19
0
ファイル: AvlTree.php プロジェクト: pramoddas/Ardent
 /**
  * @param BinaryTree $node
  * @return BinaryTree
  */
 private function deleteNeitherChildIsNull(BinaryTree $node)
 {
     $value = $node->inOrderPredecessor()->value();
     $node->setLeft($this->removeRecursive($value, $node->left()));
     $node->setValue($value);
     return $node;
 }
コード例 #20
0
        /*********************************************** First level ******************************************/
        $level = $this->buildLevelByLevelNetwork($key, $this->add_page_id, $this->view_page_id, 1, $level);
        /*********************************************** 2 and more level's ******************************************/
        if ($this->counter >= 2) {
            $j = 1;
            for ($i = 2; $i <= $this->counter; $i++) {
                $j = $j * 2;
                $level = $this->buildLevelByLevelNetwork($key, $this->add_page_id, $this->view_page_id, $j, $level);
            }
        }
        return $this->clients;
    }
}
// end class
error_reporting(0);
$obj = new BinaryTree();
$var = $obj->network();
global $current_user;
get_currentuserinfo();
$username1 = $current_user->user_login;
$res = mysql_fetch_array(mysql_query("SELECT user_key FROM " . WPMLM_TABLE_USER . " WHERE username = '******'"));
?>
<style type="text/css">
	span.owner
	{
		color:#339966; 
		font-style:italic;
	}
	span.paid
	{
		color:#FFFFFF!important; 
コード例 #21
0
var_dump(BinaryTree::find(300, $tree));
var_dump(BinaryTree::find(4000, $tree));
var_dump(BinaryTree::find(5000, $tree));
var_dump(BinaryTree::find(100, $tree));
var_dump(BinaryTree::find(50, $tree));
var_dump(BinaryTree::find(10, $tree));
var_dump(BinaryTree::find(200, $tree));
var_dump(BinaryTree::find(400, $tree));
var_dump(BinaryTree::find(1, $tree));
echo '<br>';
echo 'ここから消去テスト';
var_dump(BinaryTree::delete(1, $tree));
echo '<br>';
//リーフの削除テスト
//var_dump(BinaryTree::delete(400, $tree));
//右のノードしかない時の削除テスト
//var_dump(BinaryTree::delete(2000, $tree));
//左のノードしかない時の削除テスト
//var_dump(BinaryTree::delete(50, $tree));
//右と左のノードがある時のテスト
//var_dump(BinaryTree::delete(300, $tree));
//右と左のノードがある時のテスト
////var_dump(BinaryTree::delete(100, $tree));
//ルートの削除テスト
//var_dump(BinaryTree::delete(1000, $tree));
//木を左のノードしかないようにしてルートを削除
//木を右のノードにしてルートを削除
var_dump(BinaryTree::delete(1000, $tree));
echo '<pre>';
var_dump($tree);
echo '</pre>';
コード例 #22
0
<?php

ini_set("memory_limit", "-1");
set_time_limit(0);
require_once "src/BinaryTree.class.php";
$tree = new BinaryTree();
$test_values = array();
$limit = 1000000;
for ($i = 0; $i < $limit; $i++) {
    $test_values[] = mt_rand(1, 1000000);
}
$test_values = array_values(array_unique($test_values));
foreach ($test_values as $value) {
    $node = new BTNode($value);
    $tree->add($node);
}
$needle = $test_values[mt_rand(0, count($test_values) - 1)];
$time1 = -microtime(true);
array_search($needle, $test_values);
$time1 += microtime(true);
echo $time1 * 1000;
echo "</br>------------------------</br>";
$time2 = -microtime(true);
$tree->search($needle);
$time2 += microtime(true);
echo $time2 * 1000;
echo "</br>------------------------</br>";
echo $time1 * 100 / $time2 . "%";