https://github.com/codermjlee/binarytrees
Some operations for binary tree
https://github.com/codermjlee/binarytrees
binary-search-tree binary-tree
Last synced: about 1 month ago
JSON representation
Some operations for binary tree
- Host: GitHub
- URL: https://github.com/codermjlee/binarytrees
- Owner: CoderMJLee
- License: mit
- Created: 2019-03-29T04:37:32.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-22T16:19:19.000Z (almost 5 years ago)
- Last Synced: 2025-04-09T16:20:53.787Z (about 1 month ago)
- Topics: binary-search-tree, binary-tree
- Language: JavaScript
- Size: 684 KB
- Stars: 312
- Watchers: 10
- Forks: 67
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# BinaryTrees
- 一些二叉树相关的操作(Some operations for binary tree)
- [BinaryTreeGraph(JS版本)](#binarytreegraphjs版本)
- [BinaryTreePrinter(Java版本)](#binarytreeprinterjava版本)
- [简介(Intro)](#简介intro)
- [核心API(Core API)](#核心apicore-api)
- [示例(Example)](#示例example)
- [BinaryTreePrinterOC](#binarytreeprinteroc)## BinaryTreeGraph(JS版本)
- 用于展示二叉树的图形化小工具(Graph for displaying a binary tree)
- - 在线演示:[BinaryTreeGraph](http://520it.com/binarytrees/)




## BinaryTreePrinter(Java版本)
### 简介(Intro)
- 树状打印一棵二叉树(Print a binary tree like a real tree)
- 比如输入一棵二叉搜索树(For exampe, if you input a binary search tree):
- [381, 12, 410, 9, 40, 394, 540, 35, 190, 476, 760, 146, 445, 600, 800]
- 就会输出(Output):
- 或者输出(Or output)

### 核心API(Core API)
```java
public final class BinaryTrees {
// 打印一棵二叉树
public static void print(BinaryTreeInfo tree);
public static void print(BinaryTreeInfo tree, PrintStyle style);// 打印一棵二叉树(打印完自动换行)
public static void println(BinaryTreeInfo tree);
public static void println(BinaryTreeInfo tree, PrintStyle style);// 获得一棵二叉树的打印字符串
public static String printString(BinaryTreeInfo tree);
public static String printString(BinaryTreeInfo tree, PrintStyle style);// 可选的打印样式
public enum PrintStyle {
LEVEL_ORDER,
INORDER
}
}
```### 示例(Example)
- 先实现**BinaryTreeInfo**的相关操作(Implements BinaryTreeInfo)
- 根节点是谁?(Who is the root node?)
- 如何查找左节点?(How to get the left child?)
- 如何查找右节点?(How to get the right child?)
- 如何打印单个节点?(How to print a node?)
```java
/**
* BinarySearchTree是你自己编写的二叉树类
* BinarySearchTree is a binary tree class that is created by yourself.
*/
public class BinarySearchTree implements BinaryTreeInfo {
/**这里省略了大量代码,只贴出了脉络代码**/
/** only show some main code **/
private Node root;
private static class Node {
E element;
Node left;
Node right;
}
/********** BinaryTreeInfo **********/
@Override
public Object root() {
// 根节点是谁?
// who is the root node?
return root;
}@Override
public Object left(Object node) {
// 如何查找左节点?
// how to get the left child of the node?
return ((Node)node).left;
}@Override
public Object right(Object node) {
// 如何查找右节点?
// how to get the right child of the node?
return ((Node)node).right;
}@Override
public Object string(Object node) {
// 如何打印单个节点?
// how to print the node?
return ((Node)node).element;
}
/********** BinaryTreeInfo **********/
}
```- 打印(Print)
```java
// 随机生成的一棵二叉搜索树(random generation)
BinarySearchTree bst = ...;// PrintStyle.LEVEL_ORDER(层序打印)
BinaryTrees.println(bst);// PrintStyle.INORDER(中序打印)
BinaryTrees.println(bst, PrintStyle.INORDER);
```

- 也可以生成字符串写入文件(Write to file)
```java
Files.writeToFile("F:/test/bst.txt", BinaryTrees.printString(bst));
```- 甚至你还都不用定义二叉树类(Even you don't need to create a binary tree class)
```java
BinaryTrees.println(new BinaryTreeInfo() {
@Override
public Object root() {
return 8;
}@Override
public Object left(Object node) {
if (node.equals(8)) return 3;
if (node.equals(3)) return 1;
if (node.equals(6)) return 4;
if (node.equals(14)) return 13;
return null;
}@Override
public Object right(Object node) {
if (node.equals(8)) return 10;
if (node.equals(10)) return 14;
if (node.equals(3)) return 6;
if (node.equals(6)) return 7;
return null;
}
@Override
public Object string(Object node) {
return node;
}
});BinaryTrees.println(new BinaryTreeInfo() {
@Override
public Object root() {
return "Life";
}
@Override
public Object left(Object node) {
if (node.equals("Life")) return "Animal";
if (node.equals("Person")) return "Man";
if (node.equals("Animal")) return "Cat";
if (node.equals("Dog")) return "Teddy";
return null;
}
@Override
public Object right(Object node) {
if (node.equals("Life")) return "Person";
if (node.equals("Person")) return "Woman";
if (node.equals("Animal")) return "Dog";
if (node.equals("Dog")) return "SingleDog";
return null;
}
@Override
public Object string(Object node) {
return node;
}
});
```

- 二叉堆
```java
public class BinaryHeap implements BinaryTreeInfo {
private int size;
private E[] elements;@Override
public Object root() {
return 0;
}@Override
public Object left(Object node) {
int index = ((int)node << 1) + 1;
return index >= size ? null : index;
}@Override
public Object right(Object node) {
int index = ((int)node << 1) + 2;
return index >= size ? null : index;
}@Override
public Object string(Object node) {
return elements[(int)node];
}
}BinaryHeap heap = new BinaryHeap<>();
for (int i = 0; i < 10; i++) {
heap.add((int)(Math.random() * 100));
}
BinaryTrees.println(heap);
```
## BinaryTreePrinterOC
- 实现`MJBinaryTreeInfo`协议
```objective-c
@interface MJBSTNode : NSObject {
@public
id _element;
MJBSTNode *_left;
MJBSTNode *_right;
}
@end@interface MJBinarySearchTree : NSObject
@end@interface MJBinarySearchTree() {
MJBSTNode *_root;
}
@end@implementation MJBinarySearchTree
#pragma mark - MJBinaryTreeInfo
- (id)left:(MJBSTNode *)node {
return node->_left;
}- (id)right:(MJBSTNode *)node {
return node->_right;
}- (id)string:(MJBSTNode *)node {
return node->_element;
}- (id)root {
return _root;
}
@end
```- 打印
```objective-c
[MJBinaryTrees println:bst];[MJBinaryTrees println:bst style:MJPrintStyleLevelOrder];
[MJBinaryTrees println:bst style:MJPrintStyleInorder];
NSString *str = [MJBinaryTrees printString:bst];
NSString *file = @"/Users/mj/Desktop/bst.txt";
[str writeToFile:file atomically:YES encoding:NSUTF8StringEncoding error:nil];
```