如何直觀形象地樹狀打印一棵二叉樹?


網上絕大部分的二叉樹打印效果都十分潦草,也不夠直觀形象,最近自己用JS寫了個圖形化小工具BinaryTreeGraph,也用Java寫了個打印器BinaryTreePrinter,還有個Objective-C版本BinaryTreePrinterOC
具體代碼實現請看github

BinaryTreeGraph(JS版)

二叉樹

二叉搜索樹

AVL樹

紅黑樹

二叉堆

BinaryTreePrinter(Java版)

簡介

  • 樹狀打印一棵二叉樹
  • 比如輸入一棵二叉搜索樹
    • [381, 12, 410, 9, 40, 394, 540, 35, 190, 476, 760, 146, 445, 600, 800]
  • 就會輸出

  • 或者輸出

核心API

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
	}
}

示例

實現BinaryTreeInfo

  • 根節點是誰?
  • 如何查找左節點?
  • 如何查找右節點?
  • 如何打印單個節點?
/**
* BinarySearchTree是你自己編寫的二叉樹類
*/
public class BinarySearchTree<E> implements BinaryTreeInfo {
	/**這里省略了大量代碼,只貼出了脈絡代碼**/
	
	private Node<E> root;
	private static class Node<E> {
		E element;
		Node<E> left;
		Node<E> right;
	}
	
	/********** BinaryTreeInfo **********/
	@Override
	public Object root() {
		// 根節點是誰?
		return root;
	}

	@Override
	public Object left(Object node) {
		// 如何查找左節點?
		return ((Node<E>) node).left;
	}

	@Override
	public Object right(Object node) {
		// 如何查找右節點?
		return ((Node<E>) node).right;
	}

	@Override
	public Object string(Object node) {
		// 如何打印單個節點?
		return ((Node<E>) node).element;
	}
	/********** BinaryTreeInfo **********/
}

打印

// 隨機生成的一棵二叉搜索樹(random generation)
BinarySearchTree<Integer> bst = ...;

// PrintStyle.LEVEL_ORDER(層序打印)
BinaryTrees.println(bst); 

// PrintStyle.INORDER(中序打印)
BinaryTrees.println(bst, PrintStyle.INORDER);

PrintStyle.LEVEL_ORDER

PrintStyle.INORDER

生成字符串寫入文件

Files.writeToFile("F:/test/bst.txt", BinaryTrees.printString(bst));

不需要定義二叉樹類

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;
	}
});

二叉堆

public class BinaryHeap<E> 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<Integer> heap = new BinaryHeap<>();
for (int i = 0; i < 10; i++) {
	heap.add((int) (Math.random() * 100));
}
BinaryTrees.println(heap);

BinaryTreePrinterOC

  • 實現MJBinaryTreeInfo協議
@interface MJBSTNode : NSObject {
@public
    id _element;
    MJBSTNode *_left;
    MJBSTNode *_right;
}
@end

@interface MJBinarySearchTree : NSObject <MJBinaryTreeInfo>
@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
  • 打印
[MJBinaryTrees println:bst];

[MJBinaryTrees println:bst style:MJPrintStyleLevelOrder];

[MJBinaryTrees println:bst style:MJPrintStyleInorder];

NSString *str = [MJBinaryTrees printString:bst];
NSString *file = @"/Users/mj/Desktop/1.txt";
[str writeToFile:file atomically:YES encoding:NSUTF8StringEncoding error:nil];


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM