主要功能

  1. 基本表示
  2. 二叉树的创建(默认层次创建,不需要删除Null值)
    • 多种传参方式
    • 支持搜索树创建
  3. 打印树的结构
  4. 前中后序列的数组形式或打印

后续会根据功能代码详细划分

总体代码

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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import java.util.*;
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;

public TreeNode() {
}

public TreeNode(int val) {
this.val = val;
}

TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}

//创建一个二叉树 默认是层次创建
public static TreeNode creatTree(String s) {
return creatTree(s, "null");
}

public static TreeNode creatTree(String s, String NUll) {
String[] dataArray = s.split(",");
List<String> dataList = new LinkedList<>(Arrays.asList(dataArray));
int len = dataList.size() - 1;
dataList.set(0, dataList.get(0).replace("[", ""));
dataList.set(len, dataList.get(len).replace("]", ""));
return creatTree(dataList, NUll);
}

public static TreeNode creatTree(List<String> data, String NUll) {
TreeNode root = new TreeNode(Integer.parseInt(data.get(0)));
int i = 1, len = data.size();
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (i < len && !queue.isEmpty()) {
TreeNode node = queue.remove();
if (!data.get(i).equals(NUll)) {
TreeNode left = new TreeNode(Integer.parseInt(data.get(i)));
node.left = left;
queue.add(left);
}
i++;
if (i < len) {
if (!data.get(i).equals(NUll)) {
TreeNode right = new TreeNode(Integer.parseInt(data.get(i)));
node.right = right;
queue.add(right);
}
i++;
} else break;
}
return root;
}

public static TreeNode creatTree(int[] num, int NULL) {//NUll表示为空的值
TreeNode root = new TreeNode(num[0]);
int i = 1, len = num.length;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (i < len && !queue.isEmpty()) {
TreeNode node = queue.remove();
TreeNode left = new TreeNode(num[i]);
if (left.val != NULL) {
node.left = left;
queue.add(left);
}
i++;
if (i < len) {
TreeNode right = new TreeNode(num[i]);
if (right.val != NULL) {
node.right = right;
queue.add(right);
}
i++;
} else break;
}
return root;
}

//创建一个二叉搜索树
public static TreeNode creatSearchTree(String s) {
String s1 = s.replace("[", "").replace("]", "");
String[] data;
if (s1.contains(",")) data = s1.split(",");
else data = s1.split(" ");
int[] nums = new int[data.length];
for (int i = 0; i < data.length; i++)
nums[i] = Integer.parseInt(data[i]);
return creatSearch(nums);
}

public static TreeNode creatSearch(int[] nums) {
if (nums.length == 0) return null;
int len = nums.length;
TreeNode root = new TreeNode(nums[0]);
for (int i = 1; i < len; i++) addSearchNode(root, nums[i]);
return root;
}

public static boolean addSearchNode(TreeNode root, int num) {
if (root == null || root.val == num) return true;
if (root.val > num) {
if (root.left != null) return addSearchNode(root.left, num);
root.left = new TreeNode(num);
} else {
if (root.right != null) return addSearchNode(root.right, num);
root.right = new TreeNode(num);
}
return true;
}

//打印树结构 不能太复杂否则打印不出来
public static void show(TreeNode root) {
if (root == null) System.out.println("EMPTY!");
// 得到树的深度
int treeDepth = getTreeDepth(root);

// 最后一行的宽度为2的(n - 1)次方乘3,再加1
// 作为整个二维数组的宽度
int arrayHeight = treeDepth * 2 - 1;
int arrayWidth = (2 << (treeDepth - 2)) * 3 + 1;
// 用一个字符串数组来存储每个位置应显示的元素
String[][] res = new String[arrayHeight][arrayWidth];
// 对数组进行初始化,默认为一个空格
for (int i = 0; i < arrayHeight; i++) {
for (int j = 0; j < arrayWidth; j++) {
res[i][j] = " ";
}
}

// 从根节点开始,递归处理整个树
// res[0][(arrayWidth + 1)/ 2] = (char)(root.val + '0');
writeArray(root, 0, arrayWidth / 2, res, treeDepth);

// 此时,已经将所有需要显示的元素储存到了二维数组中,将其拼接并打印即可
for (String[] line : res) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < line.length; i++) {
sb.append(line[i]);
if (line[i].length() > 1 && i <= line.length - 1) {
i += line[i].length() > 4 ? 2 : line[i].length() - 1;
}
}
System.out.println(sb);
}
}
// 用于获得树的层数
private static int getTreeDepth(TreeNode root) {
return root == null ? 0 : (1 + Math.max(getTreeDepth(root.left), getTreeDepth(root.right)));
}

private static void writeArray(TreeNode currNode, int rowIndex, int columnIndex, String[][] res, int treeDepth) {
// 保证输入的树不为空
if (currNode == null) return;
// 先将当前节点保存到二维数组中
res[rowIndex][columnIndex] = String.valueOf(currNode.val);

// 计算当前位于树的第几层
int currLevel = ((rowIndex + 1) / 2);
// 若到了最后一层,则返回
if (currLevel == treeDepth) return;
// 计算当前行到下一行,每个元素之间的间隔(下一行的列索引与当前元素的列索引之间的间隔)
int gap = treeDepth - currLevel - 1;

// 对左儿子进行判断,若有左儿子,则记录相应的"/"与左儿子的值
if (currNode.left != null) {
res[rowIndex + 1][columnIndex - gap] = "/";
writeArray(currNode.left, rowIndex + 2, columnIndex - gap * 2, res, treeDepth);
}

// 对右儿子进行判断,若有右儿子,则记录相应的"\"与右儿子的值
if (currNode.right != null) {
res[rowIndex + 1][columnIndex + gap] = "\\";
writeArray(currNode.right, rowIndex + 2, columnIndex + gap * 2, res, treeDepth);
}
}


//得到前序的集合
public static ArrayList<Integer> getArray_Q(TreeNode root) {
ArrayList<Integer> ans = new ArrayList<>();
TreeNode pre; //当前节点的左节点的最右节点
while (root != null) {
if (root.left != null) {
pre = root.left;
while (pre.right != null && pre.right != root) {
pre = pre.right;
}
if (pre.right == null) {
ans.add(root.val);
pre.right = root;
root = root.left;
} else {
pre.right = null; //回复原状 说明来过了
root = root.right;
}
} else {
ans.add(root.val);
root = root.right;
}
}
return ans;
}

//得到前序的集合
public static ArrayList<Integer> getArray_Z(TreeNode root) {
TreeNode pre; //当前节点的左节点的最右节点
ArrayList<Integer> ans = new ArrayList<>();
while (root != null) {
if (root.left != null) {
pre = root.left;
while (pre.right != null && pre.right != root) {
pre = pre.right;
}
if (pre.right == null) {
pre.right = root;
root = root.left;
} else {
ans.add(root.val);
pre.right = null; //回复原状
root = root.right;
}
} else {
ans.add(root.val);
root = root.right;
}
}
return ans;
}

//得到后序的集合
public static ArrayList<Integer>getArray_H(TreeNode root) {
TreeNode pre; //当前节点的左节点的最右节点
ArrayList<Integer> ans = new ArrayList<>();
while (root != null) {
if (root.right != null) {
pre = root.right;
while (pre.left != null && pre.left != root) {
pre = pre.left;
}
if (pre.left == null) {
ans.add(0,root.val);
pre.left = root;
root = root.right;
} else {
pre.left = null; //回复原状 说明来过了
root = root.left;
}
} else {
ans.add(0,root.val);
root = root.left;
}
}
return ans;
}


//打印前中后序的结果
public static void print_Q(TreeNode root){
printArray(getArray_Q(root));
}
public static void print_Z(TreeNode root){
printArray(getArray_Z(root));
}
public static void print_H(TreeNode root){
printArray(getArray_H(root));
}
private static void printArray(ArrayList<Integer> list){
for (Integer num : list)
System.out.format("%d ",num);
System.out.println();
}

}