{"id":8398,"date":"2023-02-14T10:25:47","date_gmt":"2023-02-14T02:25:47","guid":{"rendered":"http:\/\/xinyiworld.top\/wordpress_it\/?p=8398"},"modified":"2023-02-14T10:51:23","modified_gmt":"2023-02-14T02:51:23","slug":"%e4%ba%8c%e5%8f%89%e6%a0%91%e6%90%9c%e7%b4%a2%e9%81%8d%e5%8e%86java%e7%89%88","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=8398","title":{"rendered":"\u4e8c\u53c9\u6811\u641c\u7d22\u904d\u5386(Java\u7248)"},"content":{"rendered":"<p>\u4e0b\u9762\u662f\u4e00\u5f20\u8868\u8fbe\u5f0f\u6811\u7684\u6811\u56fe<\/p>\n<p><img src=\"http:\/\/xinyiworld.top\/wordpress_it\/wp-content\/uploads\/2023\/02\/binary_tree_search.png\" alt=\"\" \/><\/p>\n<p>\u4e3a\u4e86\u7528java\u4ee3\u7801\u6784\u9020\u6811\u6a21\u578b\uff0c\u6211\u5bf9\u6811\u56fe\u7684\u8282\u70b9\u8fdb\u884c\u4e86\u7f16\u53f7\uff1a\u6839\u7ed3\u70b9R\uff0c\u5de6\u4fa7\u7ed3\u70b9\u4f9d\u6b21\u7528L\u52a0\u4e0a\u6570\u5b57\u6765\u8868<\/p>\n<p>\u793a\uff0c\u53f3\u4fa7\u7ed3\u70b9\u4f9d\u6b21\u7528R+\u6570\u5b57\u6765\u8868\u793a<\/p>\n<p><strong>\u7ed3\u70b9\u7c7b<\/strong>\u00a0<\/p>\n<pre><code class=\"language-java\">public class Node {\n  private Object data;\n    private Node leftNode;\n    private Node rightNode;\n\n    \/\/\u9002\u7528\u4e8e\u53ea\u6709\u6570\u636e\u6ca1\u6709\u5de6\u53f3\u5b50\u7ed3\u70b9\u7684\u7ed3\u70b9\n    public Node(Object data) {\n        this.data = data;\n        this.leftNode = null;\n        this.rightNode = null;\n    }\n\n    \/\/\u9002\u7528\u4e8e\u6709\u6216\u6ca1\u6709\u5de6\u53f3\u5b50\u7ed3\u70b9\u7684\u7ed3\u70b9\n    public Node(Object data, Node leftNode, Node rightNode) {\n        this.data = data;\n        this.leftNode = leftNode;\n        this.rightNode = rightNode;\n    }\n\n    ...  \/\/getter and setter method \n}<\/code><\/pre>\n<p><strong>\u4e3b\u7c7b<\/strong><\/p>\n<pre><code class=\"language-java\">public class BiaoDaShiTree {\n    public static Node init()\n    {\n          Node L5 = new Node(&quot;c&quot;);\n                    Node L4 = new Node(&quot;b&quot;);\n                    Node L3 = new Node(&quot;*&quot;,L4,L5);\n                    Node L2 = new Node(&quot;a&quot;);\n                    Node L1 = new Node(&quot;+&quot;,L2,L3);\n\n                    Node R7 = new Node(&quot;g&quot;);\n                    Node R6 = new Node(&quot;f&quot;);\n                    Node R5 = new Node(&quot;e&quot;);\n                    Node R4 = new Node(&quot;d&quot;);\n                    Node R3 = new Node(&quot;*&quot;,R4,R5);\n                    Node R2 = new Node(&quot;+&quot;,R3,R6);\n                    Node R1 = new Node(&quot;*&quot;,R2,R7);\n\n          Node root = new Node(&quot;+&quot;,L1,R1);\n          return root;\n    }\n\n    public static void main(String[] args)\n    {\n        Node root = init();\n        System.out.println(&quot;\u524d\u5e8f\u904d\u5386&quot;);\n        preorderTravesal(root);\n        System.out.println();\n        System.out.println(&quot;\u4e2d\u5e8f\u904d\u5386&quot;);\n        inorderTravesal(root);\n        System.out.println();\n        System.out.println(&quot;\u540e\u5e8f\u904d\u5386&quot;);\n        postorderTravesal(root);\n\n    }\n\n    \/\/\u524d\u5e8f\u904d\u5386\n    public static void preorderTravesal(Node root)\n    {\n        System.out.print(root.getData());\n        Node leftNode = root.getLeftNode();\n        Node rightNode = root.getRightNode();\n        if(leftNode !=null)\n        {\n            preorderTravesal(leftNode);\n        }\n\n        if(rightNode != null)\n        {\n            preorderTravesal(rightNode);\n        }\n    }\n\n    \/\/\u4e2d\u5e8f\u904d\u5386\n    public static void inorderTravesal(Node root)\n    {\n        Node leftNode = root.getLeftNode();\n        Node rightNode = root.getRightNode();\n        if(leftNode !=null)\n        {\n            inorderTravesal(leftNode);\n        }\n\n        System.out.print(root.getData());\n\n        if(rightNode != null)\n        {\n            inorderTravesal(rightNode);\n        }\n    }\n\n    \/\/\u540e\u5e8f\u904d\u5386\n    public static void postorderTravesal(Node root)\n    {\n        Node leftNode = root.getLeftNode();\n        Node rightNode = root.getRightNode();\n        if(leftNode !=null)\n        {\n            postorderTravesal(leftNode);\n        }\n\n        if(rightNode != null)\n        {\n            postorderTravesal(rightNode);\n        }\n\n        System.out.print(root.getData());\n    }\n}<\/code><\/pre>\n<p><strong>\u6253\u5370\u7ed3\u679c<\/strong>\uff1a<br \/>\n<img src=\"http:\/\/xinyiworld.top\/wordpress_it\/wp-content\/uploads\/2023\/02\/print_result.png\" alt=\"\" \/><\/p>\n<p><strong>\u89c4\u5f8b\u603b\u7ed3<\/strong>\uff1a<br \/>\n\u524d\u5e8f\u904d\u5386\uff1a\u5bf9\u7ed3\u70b9\u7684\u5904\u7406\u5de5\u4f5c\uff082\u4e2a\u4f8b\u5b50\u5c31\u662f\u6253\u5370\u7ed3\u70b9\u6570\u636e\uff09\u5728\u5bf9\u7ed3\u70b9\u8bf8\u513f\u5b50\u7ed3\u70b9\u8ba1\u7b97\u4e4b\u524d\u8fdb\u884c\u7684\u3002<\/p>\n<p>\u00a0\u540e\u5e8f\u904d\u5386\uff1a\u5bf9\u7ed3\u70b9\u7684\u5904\u7406\u5de5\u4f5c\uff082\u4e2a\u4f8b\u5b50\u5c31\u662f\u6253\u5370\u7ed3\u70b9\u6570\u636e\uff09\u5728\u5bf9\u7ed3\u70b9\u8bf8\u513f\u5b50\u7ed3\u70b9\u8ba1\u7b97\u4e4b\u540e\u8fdb\u884c\u7684\u3002<\/p>\n<p>\u00a0\u4e2d\u5e8f\u904d\u5386\uff1a\u5bf9\u7ed3\u70b9\u7684\u5904\u7406\u5de5\u4f5c\uff082\u4e2a\u4f8b\u5b50\u5c31\u662f\u6253\u5370\u7ed3\u70b9\u6570\u636e\uff09\u5728\u5bf9\u7ed3\u70b9\u8bf8\u513f\u5b50\u7ed3\u70b9\u8ba1\u7b97\u4e4b\u95f4\u8fdb\u884c\u7684\u3002<\/p>\n<button class=\"simplefavorite-button\" data-postid=\"8398\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u662f\u4e00\u5f20\u8868\u8fbe\u5f0f\u6811\u7684\u6811\u56fe \u4e3a\u4e86\u7528java\u4ee3\u7801\u6784\u9020\u6811\u6a21\u578b\uff0c\u6211\u5bf9\u6811\u56fe\u7684\u8282\u70b9\u8fdb\u884c\u4e86\u7f16\u53f7\uff1a\u6839\u7ed3\u70b9R\uff0c\u5de6\u4fa7\u7ed3\u70b9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[632],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/8398"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8398"}],"version-history":[{"count":4,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/8398\/revisions"}],"predecessor-version":[{"id":8404,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/8398\/revisions\/8404"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8398"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}