{"id":323,"date":"2021-01-04T11:30:30","date_gmt":"2021-01-04T11:30:30","guid":{"rendered":"https:\/\/blog.gwlin.com\/?p=323"},"modified":"2023-03-30T18:00:57","modified_gmt":"2023-03-30T18:00:57","slug":"PHP \u4e8c\u53c9\u6811\u7684\u6784\u5efa\u548c\u904d\u5386","status":"publish","type":"post","link":"https:\/\/www.gwlin.com\/blog\/posts\/323","title":{"rendered":"PHP \u4e8c\u53c9\u6811\u7684\u6784\u5efa\u548c\u904d\u5386"},"content":{"rendered":"\n<p>\u7f51\u4e0aPHP\u5173\u4e8e\u4e8c\u53c9\u6811\u7684\u5185\u5bb9\u6bd4\u8f83\u5c11\uff0c\u6bd5\u7adf\u5b9e\u9645\u5f00\u53d1\u4e2d\u7528\u5f97\u4e0d\u591a\u3002\u8fd9\u91cc\u628a\u81ea\u5df1\u5b66\u4e60\u7684\u7ed3\u679c\u5206\u4eab\u51fa\u6765\uff0c\u5171\u52c9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\"  lang=\"php\" class=\"language-php\">&lt;?php\n\n\/**\n * \u8282\u70b9\u7c7b\n *\/\nclass TreeNode\n{\n\n    private ?self $left = null;\n\n    private ?self $right = null;\n\n    private int $value;\n\n    public function __construct(int $value)\n    {\n        $this-&gt;value = $value;\n    }\n\n    \/**\n     * @return int\n     *\/\n    public function getValue(): int\n    {\n        return $this-&gt;value;\n    }\n\n    \/**\n     * @return self|null\n     *\/\n    public function getLeft(): ?self\n    {\n        return $this-&gt;left;\n    }\n\n    \/**\n     * @return self|null\n     *\/\n    public function getRight(): ?self\n    {\n        return $this-&gt;right;\n    }\n\n    \/**\n     * @param self $left\n     *\/\n    public function setLeft(self $left): void\n    {\n        $this-&gt;left = $left;\n    }\n\n    \/**\n     * @param self $right\n     *\/\n    public function setRight(self $right): void\n    {\n        $this-&gt;right = $right;\n    }\n}\n\n\/**\n * \u6811\n *\/\nclass Tree\n{\n\n    private TreeNode $root;\n\n    public function __construct(array $dataSet1, array $dataSet2, string $method)\n    {\n        switch ($method) {\n            case &#039;pre&#039;: \/\/ \u6839\u636e\u524d\u5e8f\u548c\u4e2d\u5e8f\u904d\u5386\u7684\u7ed3\u679c\u6784\u5efa\n                $this-&gt;root = $this-&gt;makeFromPreOrderAndInOrder($dataSet1, $dataSet2);\n                break;\n            case &#039;post&#039;:\/\/ \u6839\u636e\u540e\u5e8f\u548c\u4e2d\u5e8f\u904d\u5386\u7684\u7ed3\u679c\u6784\u5efa\n                $this-&gt;root = $this-&gt;makeFromPostOrderAndInOrder($dataSet1, $dataSet2);\n                break;\n            default:\n                throw new InvalidArgumentException;\n        }\n    }\n\n    \/**\n     * \u4ece\u524d\u5e8f\u904d\u5386\u548c\u4e2d\u5e8f\u904d\u5386\u7684\u7ed3\u679c\u6784\u5efa\u539f\u6765\u7684\u6811\n     * \u89e3\u6cd5\uff1a\u5148\u5e8f\u4e2d\u7684\u9996\u5143\u7d20a \u5fc5\u4e3a\u8be5\u4e8c\u53c9\u6811\u7684\u6839\u7ed3\u70b9\uff0c\u5728\u4e2d\u5e8f\u5e8f\u5217\u91cca\u4e4b\u524d\u7684\u5143\u7d20\u4e00\u5b9a\u662fa\u7684\u5de6\u5b50\u6811\u90e8\u5206\uff0ca\u4e4b\u540e\u7684\u5143\u7d20\u4e00\u5b9a\u4e3aa\u7684\u53f3\u5b50\u6811\u90e8\u5206\u3002\n     *\n     * @param int[] $pre\n     * @param int[] $in\n     * @return TreeNode\n     *\/\n    public function makeFromPreOrderAndInOrder(array $pre, array $in): TreeNode\n    {\n        $currentRoot = $pre[0]; \/\/ \u524d\u5e8f\u904d\u5386\u7684\u9996\u5143\u7d20\u80af\u5b9a\u662f\u6839\u8282\u70b9\n        $currentRootPos = array_search($currentRoot, $in, true);\n        $left = array_slice($in, 0, $currentRootPos);\n        $right = array_slice($in, $currentRootPos + 1);\n        $preLeft = array_slice($pre, 1, $currentRootPos);\n        $preRight = array_slice($pre, $currentRootPos + 1);\n        $currentRoot = new TreeNode($currentRoot);\n\n        if ($left) {\n            $currentRoot-&gt;setLeft($this-&gt;makeFromPreOrderAndInOrder($preLeft, $left));\n        }\n\n        if ($right) {\n            $currentRoot-&gt;setRight($this-&gt;makeFromPreOrderAndInOrder($preRight, $right));\n        }\n\n        return $currentRoot;\n    }\n\n    \/**\n     * \u4ece\u540e\u5e8f\u904d\u5386\u548c\u4e2d\u5e8f\u904d\u5386\u7684\u7ed3\u679c\u91cd\u65b0\u6784\u5efa\u6811\n     * \u601d\u8def\u540c\u4e0a\uff0c\u53ea\u662f\u53cd\u4e86\u8fc7\u6765\n     *\n     * @param array $post\n     * @param array $in\n     * @return TreeNode\n     *\/\n    public function makeFromPostOrderAndInOrder(array $post, array $in): TreeNode\n    {\n        $currentRoot = $post[count($post) - 1]; \/\/ \u540e\u5e8f\u904d\u5386\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u662f\u6839\u8282\u70b9\n        $currentRootPos = array_search($currentRoot, $in, true);\n        $left = array_slice($in, 0, $currentRootPos);\n        $right = array_slice($in, $currentRootPos + 1);\n        $postLeft = array_slice($post, 0, $currentRootPos);\n        $postRight = array_slice($post, $currentRootPos, -1);\n\n        $currentRoot = new TreeNode($currentRoot);\n\n        if ($left) {\n            $currentRoot-&gt;setLeft($this-&gt;makeFromPostOrderAndInOrder($postLeft, $left));\n        }\n\n        if ($right) {\n            $currentRoot-&gt;setRight($this-&gt;makeFromPostOrderAndInOrder($postRight, $right));\n        }\n\n        return $currentRoot;\n    }\n\n    \/**\n     * \u524d\u5e8f\u904d\u5386\n     *\n     * @param TreeNode|null $treeNode\n     * @return array\n     *\/\n    public function preOrderTraversal(TreeNode $treeNode = null): array\n    {\n        if (is_null($treeNode)) {\n            $treeNode = $this-&gt;root;\n        }\n\n        $ret = [];\n\n        $ret[] = $treeNode-&gt;getValue(); \/\/ \u8282\u70b9\u9a6c\u4e0a\u8f93\u51fa\n\n        if ($treeNode-&gt;getLeft()) {\n            $ret = array_merge($ret, $this-&gt;preOrderTraversal($treeNode-&gt;getLeft()));\n        }\n        if ($treeNode-&gt;getRight()) {\n            $ret = array_merge($ret, $this-&gt;preOrderTraversal($treeNode-&gt;getRight()));\n        }\n\n        return $ret;\n    }\n\n    \/**\n     * \u4e2d\u5e8f\u904d\u5386\n     *\n     * @param TreeNode $treeNode\n     * @return array\n     *\/\n    public function inOrderTraversal(TreeNode $treeNode = null): array\n    {\n        if (is_null($treeNode)) {\n            $treeNode = $this-&gt;root;\n        }\n\n        $ret = [];\n\n        if ($treeNode-&gt;getLeft()) {\n            $ret = array_merge($ret, $this-&gt;inOrderTraversal($treeNode-&gt;getLeft()));\n        }\n\n        $ret[] = $treeNode-&gt;getValue(); \/\/\u53ea\u6709\u5f53\u904d\u5386\u5b8c\u5de6\u8282\u70b9\u540e\u624d\u8f93\u51fa\n\n        if ($treeNode-&gt;getRight()) {\n            $ret = array_merge($ret, $this-&gt;inOrderTraversal($treeNode-&gt;getRight()));\n        }\n\n        return $ret;\n    }\n\n    \/**\n     * \u540e\u5e8f\u904d\u5386\n     *\n     * @param TreeNode|null $treeNode\n     * @return array\n     *\/\n    public function postOrderTraversal(TreeNode $treeNode = null): array\n    {\n        if (is_null($treeNode)) {\n            $treeNode = $this-&gt;root;\n        }\n\n        $ret = [];\n\n        if ($treeNode-&gt;getLeft()) {\n            $ret = array_merge($ret, $this-&gt;postOrderTraversal($treeNode-&gt;getLeft()));\n        }\n\n        if ($treeNode-&gt;getRight()) {\n            $ret = array_merge($ret, $this-&gt;postOrderTraversal($treeNode-&gt;getRight()));\n        }\n\n        $ret[] = $treeNode-&gt;getValue(); \/\/ \u904d\u5386\u5b8c\u5de6\u53f3\u8282\u70b9\u540e\u624d\u8f93\u51fa\n\n        return $ret;\n    }\n}\n\nfunction exampleTree()\n{\n    $n6 = new TreeNode(6);\n    $n6-&gt;setLeft(new TreeNode(7));\n    $n6-&gt;setRight(new TreeNode(8));\n    $n4 = new TreeNode(4);\n    $n4-&gt;setRight($n6);\n    $n2 = new TreeNode(2);\n    $n2-&gt;setLeft($n4);\n    $n1 = new TreeNode(1);\n    $n1-&gt;setLeft($n2);\n    $n3 = new TreeNode(3);\n    $n3-&gt;setRight(new TreeNode(5));\n    $n1-&gt;setRight($n3);\n    return $n1;\n}\n\n\n$pre = [1, 2, 4, 6, 7, 8, 3, 5];\n$in = [4, 7, 6, 8, 2, 1, 3, 5];\n$post = [7, 8, 6, 4, 2, 5, 3, 1];\n\n$tree = new Tree($post, $in, &#039;post&#039;);\n\nvar_export($tree-&gt;preOrderTraversal());\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7f51\u4e0aPHP\u5173\u4e8e\u4e8c\u53c9\u6811\u7684\u5185\u5bb9\u6bd4\u8f83\u5c11\uff0c\u6bd5\u7adf\u5b9e\u9645\u5f00\u53d1\u4e2d\u7528\u5f97\u4e0d\u591a\u3002\u8fd9\u91cc\u628a\u81ea\u5df1\u5b66\u4e60\u7684\u7ed3\u679c\u5206\u4eab\u51fa\u6765\uff0c\u5171\u52c9\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[179],"tags":[186,196],"class_list":["post-323","post","type-post","status-publish","format-standard","hentry","category-notes","tag-php","tag-196"],"_links":{"self":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/posts\/323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/comments?post=323"}],"version-history":[{"count":0,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/posts\/323\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/media?parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/categories?post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/tags?post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}