-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsimple_test.php
More file actions
100 lines (83 loc) · 2.77 KB
/
Copy pathsimple_test.php
File metadata and controls
100 lines (83 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<link rel="stylesheet" href="style.css" type="text/css">
<?php
/**
* PHP AVL binary tree
*
* A simple test script for Rbppavl package.
*
* PHP version 5
*
* @category Structures
* @package Rbppavl
* @author mondrake <mondrake@mondrake.org>
* @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
* @link http://github.com/mondrake/Rbppavl
*/
require_once "Rbppavl/Rbppavl.php";
// change value below to increase/decrease the number of nodes to be generated
$howManyNodes = 10;
// ------------------------------------------------------------------
// Test classes
// ------------------------------------------------------------------
class TestClass {
public $q;
}
class TestCallback implements RbppavlCbInterface {
public function compare($a, $b) {
return ($a->q == $b->q) ? 0 : (($a->q < $b->q) ? -1 : 1);
}
public function dump($a) {
return $a->q;
}
public function diagnosticMessage($severity, $id, $text, $params, $qText, $className = null) {
print($qText . '<br/>');
}
public function errorHandler($id, $text, $params, $qText, $className = null) {
throw new exception($qText, $id);
}
}
// ------------------------------------------------------------------
// Main routine
// ------------------------------------------------------------------
print ('<h2>Simple Rbppavl tree test: a standard AVL tree</h2>');
print ("This test generates $howManyNodes random integers, inserts them in a standard AVL tree, validates the tree,<br/>");
print ('performs an inorder traversal, then deletes the tree and its content.<br/>');
print ('For more complex scenarios, see full test <a href="full_test.php">here</a>.<br/>');
print ('Check Rbppavl documentation <a href="Rbppavl_doc">here</a>.<br/><br/>');
// generate some random data values
$qres = array();
for ($i = 0; $i < $howManyNodes; $i++) {
$qres[$i] = rand(0, $howManyNodes * 5);
}
// create a new tree
$tree = new RbppavlTree("TestCallback", $balanceFactor = 1, $verbose = true);
// loop to insert the data
print('<b>Node inserts</b><br/>');
$ctr=0;
foreach ($qres as $r) {
$data = new TestClass;
$data->q = $r;
$t = $tree->insert($data);
if ($tree->getStatusLevel() <= RBPPAVL_WARNING) {
break;
}
$ctr++;
}
// check tree
print('<br/><b>Validation and statistics</b><br/>');
$failingNode = $tree->debugValidate($setStatusOnSuccess = true);
$stats = $tree->getStatistics($stat = null, $setStatus = true);
// loop to print the inorder traversal
print('<br/><b>Inorder traversal</b><br/>');
$trav = new RbppavlTraverser($tree);
$r = $trav->first();
$ctr=1;
while ($r != NULL) {
print("$ctr: $r->q<br/>");
$r = $trav->next();
$ctr++;
}
// cleanup
print('<br/><b>Cleanup</b><br/>');
$trav = null;
$tree = null;