feat: Add PHP Codes for utils

This commit is contained in:
DoWake 2024-05-19 22:37:21 +08:00
parent 21be3fdaf8
commit e1e0668e0f
4 changed files with 245 additions and 0 deletions

View File

@ -0,0 +1,33 @@
<?php
/**
* File: ListNode.php
* Created Time: 2024-05-19
* Author: DoWake (admin@mengxing.cc)
*/
/* 链表节点 */
class ListNode
{
public $val; // 节点值
public $next; // 指向下一个节点的引用
/* 构造方法 */
public function __construct($val = 0, $next = null)
{
$this->val = $val;
$this->next = $next;
}
/* 将列表反序列化为链表 */
public static function arrToLinkedList($arr)
{
$dum = new ListNode(0);
$head = $dum;
foreach ($arr as $val) {
$head->next = new ListNode($val);
$head = $head->next;
}
return $dum->next;
}
}

View File

@ -0,0 +1,111 @@
<?php
/**
* File: PrintUtil.php
* Created Time: 2024-05-19
* Author: DoWake (admin@mengxing.cc)
*/
require_once 'TreeNode.php';
class Trunk
{
public $prev;
public $str;
public function __construct($prev, $str)
{
$this->prev = $prev;
$this->str = $str;
}
}
class PrintUtil
{
/* 打印矩阵Array */
public static function printMatrix($matrix)
{
echo '[' . PHP_EOL;
foreach ($matrix as $row) {
echo ' [' . implode(', ', $row) . '],' . PHP_EOL;
}
echo ']' . PHP_EOL;
}
/* 打印链表 */
public static function printLinkedList($head)
{
$list = [];
while ($head !== null) {
$list[] = $head->val;
$head = $head->next;
}
echo join(' -> ', $list) . PHP_EOL;
}
/**
* 打印二叉树
* This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
*/
public static function printTree($root, $prev = null, $isRight = false)
{
if ($root === null) {
return;
}
$prev_str = ' ';
$trunk = new Trunk($prev, $prev_str);
self::printTree($root->right, $trunk, true);
if ($prev === null) {
$trunk->str = '———';
} else if ($isRight) {
$trunk->str = '/———';
$prev_str = ' |';
} else {
$trunk->str = '\\———';
$prev->str = $prev_str;
}
self::showTrunks($trunk);
echo ' ' . $root->val . PHP_EOL;
if ($prev !== null) {
$prev->str = $prev_str;
}
$trunk->str = ' |';
self::printTree($root->left, $trunk, false);
}
public static function showTrunks($p)
{
if ($p === null) {
return;
}
self::showTrunks($p->prev);
echo $p->str;
}
/* 打印哈希表 */
public static function printHashMap($map)
{
foreach ($map as $key => $value) {
echo $key . ' -> ' . $value . PHP_EOL;
}
}
/* 打印堆(优先队列) */
public static function printHeap($queue)
{
echo '堆的数组表示:';
echo '[' . join(', ', $queue) . ']' . PHP_EOL;
echo '堆的树状表示:' . PHP_EOL;
$root = TreeNode::listToTree($queue);
self::printTree($root);
}
}

View File

@ -0,0 +1,62 @@
<?php
/**
* File: TreeNode.php
* Created Time: 2024-05-19
* Author: DoWake (admin@mengxing.cc)
*/
/* 二叉树节点类 */
class TreeNode
{
public $val; // 节点值
public $height; // 节点高度
public $left; // 左子节点引用
public $right; // 右子节点引用
/* 构造方法 */
public function __construct($val = 0, $left = null, $right = null, $height = 0)
{
$this->val = $val;
$this->left = $left;
$this->right = $right;
$this->height = $height;
}
/* 将列表反序列化为二叉树:递归 */
private static function listToTreeDFS($arr, $i = 0)
{
if ($i < 0 || $i >= count($arr) || $arr[$i] === null) {
return null;
}
$root = new TreeNode($arr[$i]);
$root->left = self::listToTreeDFS($arr, 2 * $i + 1);
$root->right = self::listToTreeDFS($arr, 2 * $i + 2);
return $root;
}
/* 将列表反序列化为二叉树 */
public static function listToTree($arr)
{
return self::listToTreeDFS($arr, 0);
}
/* 将二叉树序列化为列表:递归 */
private static function treeToListDFS($root, $i, &$res)
{
if ($root === null) {
return;
}
$res[$i] = $root->val;
self::treeToListDFS($root->left, 2 * $i + 1, $res);
self::treeToListDFS($root->right, 2 * $i + 2, $res);
}
/* 将二叉树序列化为列表 */
public static function treeToList($root)
{
$res = [];
self::treeToListDFS($root, 0, $res);
return $res;
}
}

View File

@ -0,0 +1,39 @@
<?php
/**
* File: Vertex.php
* Created Time: 2024-05-19
* Author: DoWake (admin@mengxing.cc)
*/
/* 顶点类 */
class Vertex
{
public $val;
/* 构造方法 */
public function __construct($val)
{
$this->val = $val;
}
/* 输入值列表 vals ,返回顶点列表 vets */
public static function valsToVets($vals)
{
$vets = [];
foreach ($vals as $val) {
$vets[] = new Vertex($val);
}
return $vets;
}
/* 输入顶点列表 vets ,返回值列表 vals */
public static function vetsToVals($vets)
{
$vals = [];
foreach ($vets as $vet) {
$vals[] = $vet->val;
}
return $vals;
}
}