{"id":19153799,"url":"https://github.com/codermjlee/binarytrees","last_synced_at":"2025-04-09T16:21:08.842Z","repository":{"id":81330359,"uuid":"178333131","full_name":"CoderMJLee/BinaryTrees","owner":"CoderMJLee","description":"Some operations for binary tree","archived":false,"fork":false,"pushed_at":"2020-06-22T16:19:19.000Z","size":700,"stargazers_count":312,"open_issues_count":3,"forks_count":67,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-09T16:20:53.787Z","etag":null,"topics":["binary-search-tree","binary-tree"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CoderMJLee.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2019-03-29T04:37:32.000Z","updated_at":"2025-01-26T09:09:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"db8c77e5-b3d2-42c2-81cc-7947fbfcb1cc","html_url":"https://github.com/CoderMJLee/BinaryTrees","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CoderMJLee%2FBinaryTrees","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CoderMJLee%2FBinaryTrees/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CoderMJLee%2FBinaryTrees/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CoderMJLee%2FBinaryTrees/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CoderMJLee","download_url":"https://codeload.github.com/CoderMJLee/BinaryTrees/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248065287,"owners_count":21041872,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binary-search-tree","binary-tree"],"created_at":"2024-11-09T08:24:17.940Z","updated_at":"2025-04-09T16:21:08.823Z","avatar_url":"https://github.com/CoderMJLee.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BinaryTrees\n- 一些二叉树相关的操作(Some operations for binary tree)\n- [BinaryTreeGraph（JS版本）](#binarytreegraphjs版本)\n- [BinaryTreePrinter（Java版本）](#binarytreeprinterjava版本)\n  - [简介（Intro）](#简介intro)\n  - [核心API（Core API）](#核心apicore-api)\n  - [示例（Example）](#示例example)\n- [BinaryTreePrinterOC](#binarytreeprinteroc)\n\n## BinaryTreeGraph（JS版本）\n- 用于展示二叉树的图形化小工具（Graph for displaying a binary tree）\n  - - 在线演示：[BinaryTreeGraph](http://520it.com/binarytrees/)\n\n![](https://img2018.cnblogs.com/blog/497279/201904/497279-20190426144618950-1992989113.png)\n\n![](https://img2018.cnblogs.com/blog/497279/201904/497279-20190426144622883-1736652037.png)\n\n![](https://img2018.cnblogs.com/blog/497279/201904/497279-20190426144627187-1212361005.png)\n\n![](https://img2018.cnblogs.com/blog/497279/201904/497279-20190426144632113-1136919830.png)\n\n![](https://img2018.cnblogs.com/blog/497279/201904/497279-20190426144636366-250176607.png)\n\n## BinaryTreePrinter（Java版本）\n### 简介（Intro）\n- 树状打印一棵二叉树（Print a binary tree like a real tree）\n- 比如输入一棵二叉搜索树（For exampe, if you input a binary search tree）: \n  -  [381, 12, 410, 9, 40, 394, 540, 35, 190, 476, 760, 146, 445, 600, 800]\n- 就会输出（Output）:\n\n![](https://img2018.cnblogs.com/blog/497279/201904/497279-20190406094223007-512106824.png)\n\n- 或者输出（Or output）\n\n![](https://img2018.cnblogs.com/blog/497279/201904/497279-20190406094237106-573651641.png)\n\n### 核心API（Core API）\n```java\npublic final class BinaryTrees {\n\t// 打印一棵二叉树\n\tpublic static void print(BinaryTreeInfo tree);\n\tpublic static void print(BinaryTreeInfo tree, PrintStyle style);\n\n\t// 打印一棵二叉树（打印完自动换行）\n\tpublic static void println(BinaryTreeInfo tree);\n\tpublic static void println(BinaryTreeInfo tree, PrintStyle style);\n\n\t// 获得一棵二叉树的打印字符串\n\tpublic static String printString(BinaryTreeInfo tree);\n\tpublic static String printString(BinaryTreeInfo tree, PrintStyle style);\n\n\t// 可选的打印样式\n\tpublic enum PrintStyle {\n\t\tLEVEL_ORDER, \n\t\tINORDER\n\t}\n}\n```\n\n### 示例（Example）\n- 先实现**BinaryTreeInfo**的相关操作(Implements BinaryTreeInfo)\n  - 根节点是谁？（Who is the root node?）\n  - 如何查找左节点？（How to get the left child?）\n  - 如何查找右节点？（How to get the right child?）\n  - 如何打印单个节点？（How to print a node?）\n```java\n/**\n* BinarySearchTree是你自己编写的二叉树类\n* BinarySearchTree is a binary tree class that is created by yourself.\n*/\npublic class BinarySearchTree\u003cE\u003e implements BinaryTreeInfo {\n\t/**这里省略了大量代码，只贴出了脉络代码**/\n\t/** only show some main code **/\n\t\n\tprivate Node\u003cE\u003e root;\n\tprivate static class Node\u003cE\u003e {\n\t\tE element;\n\t\tNode\u003cE\u003e left;\n\t\tNode\u003cE\u003e right;\n\t}\n\t\n\t/********** BinaryTreeInfo **********/\n\t@Override\n\tpublic Object root() {\n\t\t// 根节点是谁？\n\t\t// who is the root node?\n\t\treturn root;\n\t}\n\n\t@Override\n\tpublic Object left(Object node) {\n\t\t// 如何查找左节点？\n\t\t// how to get the left child of the node?\n\t\treturn ((Node)node).left;\n\t}\n\n\t@Override\n\tpublic Object right(Object node) {\n\t\t// 如何查找右节点？\n\t\t// how to get the right child of the node?\n\t\treturn ((Node)node).right;\n\t}\n\n\t@Override\n\tpublic Object string(Object node) {\n\t\t// 如何打印单个节点？\n\t\t// how to print the node?\n\t\treturn ((Node)node).element;\n\t}\n\t/********** BinaryTreeInfo **********/\n}\n```\n\n- 打印(Print)\n```java\n// 随机生成的一棵二叉搜索树（random generation）\nBinarySearchTree\u003cInteger\u003e bst = ...;\n\n// PrintStyle.LEVEL_ORDER（层序打印）\nBinaryTrees.println(bst); \n\n// PrintStyle.INORDER（中序打印）\nBinaryTrees.println(bst, PrintStyle.INORDER);\n```\n\n![](https://img2018.cnblogs.com/blog/497279/201904/497279-20190406111607906-1148747309.png)\n\n![](https://img2018.cnblogs.com/blog/497279/201904/497279-20190406111614353-1717134516.png)\n\n- 也可以生成字符串写入文件(Write to file)\n```java\nFiles.writeToFile(\"F:/test/bst.txt\", BinaryTrees.printString(bst));\n```\n\n- 甚至你还都不用定义二叉树类(Even you don't need to create a binary tree class)\n```java\nBinaryTrees.println(new BinaryTreeInfo() {\n\t@Override\n\tpublic Object root() {\n\t\treturn 8;\n\t}\n\n\t@Override\n\tpublic Object left(Object node) {\n\t\tif (node.equals(8)) return 3;\n\t\tif (node.equals(3)) return 1;\n\t\tif (node.equals(6)) return 4;\n\t\tif (node.equals(14)) return 13;\n\t\treturn null;\n\t}\n\n\t@Override\n\tpublic Object right(Object node) {\n\t\tif (node.equals(8)) return 10;\n\t\tif (node.equals(10)) return 14;\n\t\tif (node.equals(3)) return 6;\n\t\tif (node.equals(6)) return 7;\n\t\treturn null;\n\t}\n\t\n\t@Override\n\tpublic Object string(Object node) {\n\t\treturn node;\n\t}\n});\n\nBinaryTrees.println(new BinaryTreeInfo() {\n\t@Override\n\tpublic Object root() {\n\t\treturn \"Life\";\n\t}\n\t\n\t@Override\n\tpublic Object left(Object node) {\n\t\tif (node.equals(\"Life\")) return \"Animal\";\n\t\tif (node.equals(\"Person\")) return \"Man\";\n\t\tif (node.equals(\"Animal\")) return \"Cat\";\n\t\tif (node.equals(\"Dog\")) return \"Teddy\";\n\t\treturn null;\n\t}\n\t\n\t@Override\n\tpublic Object right(Object node) {\n\t\tif (node.equals(\"Life\")) return \"Person\";\n\t\tif (node.equals(\"Person\")) return \"Woman\";\n\t\tif (node.equals(\"Animal\")) return \"Dog\";\n\t\tif (node.equals(\"Dog\")) return \"SingleDog\";\n\t\treturn null;\n\t}\n\t\n\t@Override\n\tpublic Object string(Object node) {\n\t\treturn node;\n\t}\n});\n```\n![](https://img2018.cnblogs.com/blog/497279/201904/497279-20190406100247015-1301544281.png)\n\n![](https://img2018.cnblogs.com/blog/497279/201904/497279-20190406100252563-950745142.png)\n\n- 二叉堆\n```java\npublic class BinaryHeap\u003cE\u003e implements BinaryTreeInfo {\n\tprivate int size;\n\tprivate E[] elements;\n\n\t@Override\n\tpublic Object root() {\n\t\treturn 0;\n\t}\n\n\t@Override\n\tpublic Object left(Object node) {\n\t\tint index = ((int)node \u003c\u003c 1) + 1;\n\t\treturn index \u003e= size ? null : index;\n\t}\n\n\t@Override\n\tpublic Object right(Object node) {\n\t\tint index = ((int)node \u003c\u003c 1) + 2;\n\t\treturn index \u003e= size ? null : index;\n\t}\n\n\t@Override\n\tpublic Object string(Object node) {\n\t\treturn elements[(int)node];\n\t}\n}\n\nBinaryHeap\u003cInteger\u003e heap = new BinaryHeap\u003c\u003e();\nfor (int i = 0; i \u003c 10; i++) {\n\theap.add((int)(Math.random() * 100));\n}\nBinaryTrees.println(heap);\n```\n![](https://img2018.cnblogs.com/blog/497279/201904/497279-20190426114408842-867838307.png)\n\n## BinaryTreePrinterOC\n- 实现`MJBinaryTreeInfo`协议\n```objective-c\n@interface MJBSTNode : NSObject {\n@public\n    id _element;\n    MJBSTNode *_left;\n    MJBSTNode *_right;\n}\n@end\n\n@interface MJBinarySearchTree : NSObject \u003cMJBinaryTreeInfo\u003e\n@end\n\n@interface MJBinarySearchTree() {\n    MJBSTNode *_root;\n}\n@end\n\n@implementation MJBinarySearchTree\n#pragma mark - MJBinaryTreeInfo\n- (id)left:(MJBSTNode *)node {\n    return node-\u003e_left;\n}\n\n- (id)right:(MJBSTNode *)node {\n    return node-\u003e_right;\n}\n\n- (id)string:(MJBSTNode *)node {\n    return node-\u003e_element;\n}\n\n- (id)root {\n    return _root;\n}\n@end\n```\n\n- 打印\n```objective-c\n[MJBinaryTrees println:bst];\n\n[MJBinaryTrees println:bst style:MJPrintStyleLevelOrder];\n\n[MJBinaryTrees println:bst style:MJPrintStyleInorder];\n\nNSString *str = [MJBinaryTrees printString:bst];\nNSString *file = @\"/Users/mj/Desktop/bst.txt\";\n[str writeToFile:file atomically:YES encoding:NSUTF8StringEncoding error:nil];\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodermjlee%2Fbinarytrees","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodermjlee%2Fbinarytrees","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodermjlee%2Fbinarytrees/lists"}