forked from nibnait/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSysOut.java
More file actions
162 lines (138 loc) · 4.66 KB
/
SysOut.java
File metadata and controls
162 lines (138 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package common.util;
import common.datastruct.BinaryTreeNode;
import common.datastruct.ListNode;
import java.util.Stack;
/**
* 标准输出
*/
public final class SysOut {
public static void print(String format, Object... args) {
System.out.print(String.format(format, args));
}
public static void println(String format, Object... args) {
System.out.println(String.format(format, args));
}
public static void printSeparator() {
println("===========================================");
}
public static void printSeparator(String title) {
println("===========================================\n%s:\n", title);
}
//*********** 打印数组 ************************************/
public static void printArray(int[][] a) {
String digitFormat = "%3d";
String digitFormat_Comma = "%3d, ";
int n = a.length;
int m = a[0].length;
System.out.println("{");
for (int i = 0; i < n; i++) {
System.out.print("\t{");
for (int j = 0; j < m; j++) {
if (j == m - 1) {
print(digitFormat, a[i][j]);
continue;
}
print(digitFormat_Comma, a[i][j]);
}
System.out.println("},");
}
System.out.println("}");
}
public static void printArray(int[] a) {
for (int i = 0; i < a.length; i++) {
if (i == a.length - 1) {
System.out.print(a[i] + "\n");
} else {
System.out.print(a[i] + ", ");
}
}
}
//*********** 打印链表 ************************************/
public static void printList(ListNode head) {
while (head != null) {
String arrow = head != null ? " -> " : "";
System.out.print(head.val + arrow);
head = head.next;
}
System.out.println("null");
}
public static void printDoubleLinkedList(BinaryTreeNode head) {
System.out.println("Double Linked List: ");
BinaryTreeNode end = null;
while (head != null) {
System.out.print(head.value + " ");
end = head;
head = head.right;
}
System.out.print("| ");
while (end != null) {
System.out.print(end.value + " ");
end = end.left;
}
System.out.println();
}
//*********** 二叉树的前中后需遍历 ************************************/
public static void preOrderPrint(BinaryTreeNode head) {
if (head == null) {
return;
}
System.out.print(head.value + " ");
preOrderPrint(head.left);
preOrderPrint(head.right);
}
public static void inOrderPrint(BinaryTreeNode head) {
if (head == null) {
return;
}
inOrderPrint(head.left);
System.out.print(head.value + " ");
inOrderPrint(head.right);
}
public static void posOrderPrint(BinaryTreeNode head) {
if (head == null) {
return;
}
posOrderPrint(head.left);
posOrderPrint(head.right);
System.out.print(head.value + " ");
}
//*********** 打印二叉树 ************************************/
private static final int NODE_LENGTH = 17; //二叉树中每个结点的长度
public static void printBinaryTree(BinaryTreeNode head) {
System.out.println("Binary Tree:");
printInOrder(head, 0, "*");
System.out.println();
}
private static void printInOrder(BinaryTreeNode head, int height, String to) {
if (head == null) {
return;
}
printInOrder(head.left, height + 1, "~");
String val = to + head.value + to;
int lenM = val.length();
int lenL = (NODE_LENGTH - lenM) / 2;
int lenR = NODE_LENGTH - lenL - lenM;
val = getSpace(height * NODE_LENGTH + lenL) + val + getSpace(lenR);
System.out.println(val);
printInOrder(head.right, height + 1, "_");
}
private static String getSpace(int n) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < n; i++) {
sb.append(" ");
}
return sb.toString();
}
//*********** 打印一个栈 ************************************/
public static void printStack(Stack<Integer> stack) {
Stack<Integer> originStack = new Stack<>();
while (!stack.isEmpty()) {
Integer pop = stack.pop();
originStack.push(pop);
System.out.println(pop);
}
while (!originStack.isEmpty()) {
stack.push(originStack.pop());
}
}
}