mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 17:58:41 +00:00
fix tests
This commit is contained in:
parent
d96a842e81
commit
999ec823b3
|
|
@ -192,6 +192,8 @@ class BinarySearchTree
|
|||
|
||||
$BST->root = $new;
|
||||
$current->left = $BST;
|
||||
|
||||
return;
|
||||
} else {
|
||||
$current = $current->left->root;
|
||||
}
|
||||
|
|
@ -204,14 +206,16 @@ class BinarySearchTree
|
|||
|
||||
$BST->root = $new;
|
||||
$current->right = $BST;
|
||||
|
||||
return;
|
||||
} else {
|
||||
$current = $current->right->root;
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a node
|
||||
|
|
@ -308,6 +312,6 @@ class BinarySearchTree
|
|||
*/
|
||||
public function toArray() : array
|
||||
{
|
||||
return $this->root?->toArray() ?? ['key' => null, 0 => null, 1 => null];
|
||||
return $this->root?->toArray() ?? null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace phpOMS\tests\Stdlib\Tree;
|
||||
|
||||
include_once __DIR__ . '/../../Autoloader.php';
|
||||
|
||||
use phpOMS\Stdlib\Tree\BinarySearchTree;
|
||||
use phpOMS\Stdlib\Tree\Node;
|
||||
|
||||
|
|
@ -24,7 +26,7 @@ use phpOMS\Stdlib\Tree\Node;
|
|||
*/
|
||||
final class BinarySearchTreeTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testBST() : void
|
||||
public function testInsert() : void
|
||||
{
|
||||
$bst = new BinarySearchTree();
|
||||
$bst->insert(new Node('D', 'D'));
|
||||
|
|
@ -72,19 +74,74 @@ final class BinarySearchTreeTest extends \PHPUnit\Framework\TestCase
|
|||
],
|
||||
$bst->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
public function testSearch() : void
|
||||
{
|
||||
$bst = new BinarySearchTree();
|
||||
$bst->insert(new Node('D', 'D'));
|
||||
$bst->insert(new Node('I', 'I'));
|
||||
$bst->insert(new Node('N', 'N'));
|
||||
$bst->insert(new Node('O', 'O'));
|
||||
$bst->insert(new Node('S', 'S'));
|
||||
$bst->insert(new Node('A', 'A'));
|
||||
$bst->insert(new Node('U', 'U'));
|
||||
$bst->insert(new Node('R', 'R'));
|
||||
|
||||
self::assertEquals('S', $bst->search('S')->key);
|
||||
self::assertEquals('U', $bst->search('U')->key);
|
||||
self::assertEquals('R', $bst->search('R')->key);
|
||||
}
|
||||
|
||||
public function testDelete() : void
|
||||
{
|
||||
$bst = new BinarySearchTree();
|
||||
$bst->insert(new Node('D', 'D'));
|
||||
$bst->insert(new Node('I', 'I'));
|
||||
$bst->insert(new Node('N', 'N'));
|
||||
$bst->insert(new Node('O', 'O'));
|
||||
$bst->insert(new Node('S', 'S'));
|
||||
$bst->insert(new Node('A', 'A'));
|
||||
$bst->insert(new Node('U', 'U'));
|
||||
$bst->insert(new Node('R', 'R'));
|
||||
$bst->delete($bst->search('I'));
|
||||
$bst->insert(new Node('Z', 'Z'));
|
||||
|
||||
// @todo: this breaks stuff, why?
|
||||
//$bst->delete($bst->search('S'));
|
||||
//$bst->insert(new Node('T', 'T'));
|
||||
$bst->delete($bst->search('S'));
|
||||
$bst->insert(new Node('T', 'T'));
|
||||
|
||||
self::assertEquals(
|
||||
[
|
||||
'key' => 'D',
|
||||
0 => ['key' => 'I'],
|
||||
1 => ['key' => 'I'],
|
||||
0 => [
|
||||
'key' => 'A',
|
||||
0 => null,
|
||||
1 => null
|
||||
],
|
||||
1 => [
|
||||
'key' => 'N',
|
||||
0 => null,
|
||||
1 => [
|
||||
'key' => 'O',
|
||||
0 => null,
|
||||
1 => [
|
||||
'key' => 'R',
|
||||
0 => null,
|
||||
1 => [
|
||||
'key' => 'U',
|
||||
0 => [
|
||||
'key' => 'T',
|
||||
0 => null,
|
||||
1 => null
|
||||
],
|
||||
1 => [
|
||||
'key' => 'Z',
|
||||
0 => null,
|
||||
1 => null
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
$bst->toArray()
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user