1. 先序、后序,序列化与反序列化
采用递归方式进行序列化和反序列化
/**
* 递归序列化
*
* @param node 头节点
* @param mode 模式 0 先序, 2 后序
* @return 序列化结果
*/
public Queue<String> serializable(BiNode node, int mode) {
Queue<String> queue = new LinkedList<>();
processSerialize(node, mode, queue);
return queue;
}
private void processSerialize(BiNode node, int mode, Queue<String> queue) {
if (node == null) {
queue.offer(null);
return;
}
if (mode == 0) {
queue.offer(node.value.toString());
}
processSerialize(node.left, mode, queue);
processSerialize(node.right, mode, queue);
if (mode == 2) {
queue.offer(node.value.toString());
}
}
// 中序不能反序列化
public BiNode unserializable(Queue<String> queue, int mode) {
return processUnSerialize(mode, (LinkedList<String>) queue);
}
private BiNode processUnSerialize(int mode, LinkedList<String> queue) {
if (queue.isEmpty()) {
return null;
}
String v;
if (mode == 0) {
v = queue.poll();
} else {
// 后序遍历,逆序取出
v = queue.removeLast();
}
if (v == null) {
return null;
}
BiNode node = new BiNode(Integer.parseInt(v));
BiNode node1 = processUnSerialize(mode, queue);
BiNode node2 = processUnSerialize(mode, queue);
if (mode == 0) {
node.left = node1;
node.right = node2;
}
if (mode == 2) {
node.right = node1;
node.left = node2;
}
return node;
}
@Test
public void test1() {
for (int i = 0; i < 10000; i++) {
BiNode node = Reduce.binaryTree(3, 100);
BiNode node1 = Reduce.copy(node);
Queue<String> s1 = serializable(node1, 2);
List<String> s2 = new ArrayList<>(s1);
BiNode node2 = unserializable(s1, 2);
if (!CompareUtil.compare(node1, node2)) {
System.out.println(Printer.print(node1));
System.out.println("----------------------");
System.out.println(s2);
System.out.println("----------------------");
System.out.println(Printer.print(node2));
return;
}
}
}
2. 按层序列化和反序列化
public Queue<String> serializableLevel(BiNode node) {
Queue<String> res = new LinkedList<>();
Queue<BiNode> queue = new LinkedList<>();
if (node == null) {
return res;
}
queue.add(node);
res.add(node.value.toString());
while (!queue.isEmpty()) {
BiNode c = queue.poll();
if (c.left != null) {
res.add(c.left.value.toString());
queue.add(c.left);
} else {
res.add(null);
}
if (c.right != null) {
res.add(c.right.value.toString());
queue.add(c.right);
} else {
res.add(null);
}
}
return res;
}
public BiNode unserializableLevel(Queue<String> queue) {
if (queue == null || queue.isEmpty()) {
return null;
}
BiNode node = new BiNode(Integer.parseInt(queue.poll()));
BiNode curr = node;
Queue<BiNode> levelQueue = new LinkedList<>();
levelQueue.offer(curr);
while (!queue.isEmpty()) {
curr = levelQueue.poll();
curr.left = newNode(queue.poll());
curr.right = newNode(queue.poll());
if (curr.left != null) {
levelQueue.add(curr.left);
}
if (curr.right != null) {
levelQueue.add(curr.right);
}
}
return node;
}
@Test
public void test2() {
for (int i = 0; i < 10000; i++) {
BiNode node = Reduce.binaryTree(5, 100);
BiNode node1 = Reduce.copy(node);
Queue<String> s1 = serializableLevel(node1);
List<String> s2 = new ArrayList<>(s1);
BiNode node2 = unserializableLevel(s1);
if (!CompareUtil.compare(node1, node2)) {
System.out.println(Printer.print(node1));
System.out.println("----------------------");
System.out.println(s2);
System.out.println("----------------------");
System.out.println(Printer.print(node2));
return;
}
}
}