From e1e0668e0ffd32c4b442664a0d8160873f6e6aaa Mon Sep 17 00:00:00 2001 From: DoWake <298666015@qq.com> Date: Sun, 19 May 2024 22:37:21 +0800 Subject: [PATCH] feat: Add PHP Codes for utils --- codes/php/utils/ListNode.php | 33 ++++++++++ codes/php/utils/PrintUtil.php | 111 ++++++++++++++++++++++++++++++++++ codes/php/utils/TreeNode.php | 62 +++++++++++++++++++ codes/php/utils/Vertex.php | 39 ++++++++++++ 4 files changed, 245 insertions(+) create mode 100644 codes/php/utils/ListNode.php create mode 100644 codes/php/utils/PrintUtil.php create mode 100644 codes/php/utils/TreeNode.php create mode 100644 codes/php/utils/Vertex.php diff --git a/codes/php/utils/ListNode.php b/codes/php/utils/ListNode.php new file mode 100644 index 000000000..6f82e3c10 --- /dev/null +++ b/codes/php/utils/ListNode.php @@ -0,0 +1,33 @@ +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; + } +} diff --git a/codes/php/utils/PrintUtil.php b/codes/php/utils/PrintUtil.php new file mode 100644 index 000000000..b2ef22db7 --- /dev/null +++ b/codes/php/utils/PrintUtil.php @@ -0,0 +1,111 @@ +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); + } +} diff --git a/codes/php/utils/TreeNode.php b/codes/php/utils/TreeNode.php new file mode 100644 index 000000000..5b6efb1b2 --- /dev/null +++ b/codes/php/utils/TreeNode.php @@ -0,0 +1,62 @@ +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; + } +} diff --git a/codes/php/utils/Vertex.php b/codes/php/utils/Vertex.php new file mode 100644 index 000000000..26d062a13 --- /dev/null +++ b/codes/php/utils/Vertex.php @@ -0,0 +1,39 @@ +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; + } +}