-
Notifications
You must be signed in to change notification settings - Fork 688
/
SerializeBinaryTree.java
149 lines (113 loc) · 4.01 KB
/
SerializeBinaryTree.java
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
package trees;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Stack;
public class SerializeBinaryTree {
/*
* Serialize binary tree to a file and then deserialize back to tree
* such that original and deserialized trees are identical
*
* Runtime Complexity:
* Linear, O(n)
*
* Memory Complexity:
* Logarithmic, O(logn)
*
* One approach is to perform a depth-first traversal and serialize individual nodes to the stream.
* Use pre-order traversal here.
*
*
* */
private static class Node {
private int data;
private Node left, right;
Node(int item) {
data = item;
left = right = null;
}
public int getData() {
return data;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
}
private Node root1;
private static final int MARKER = Integer.MIN_VALUE;
protected void serialize(Node node, ObjectOutputStream stream)
throws java.io.IOException {
if (node == null) {
stream.writeInt(MARKER);
return;
}
stream.writeInt(node.data);
serialize(node.left, stream);
serialize(node.right, stream);
}
protected Node deserialize(ObjectInputStream stream) throws java.io.IOException {
int val = stream.readInt();
if (val == MARKER) {
return null;
}
Node node = new Node(val);
node.left = deserialize(stream);
node.right = deserialize(stream);
return node;
}
protected void inOrderDisplay(Node root) {
if (root == null)
return;
Stack<Node> stk = new Stack<>();
while (!stk.empty() || root != null) {
if (root != null) {
stk.push(root);
root = root.left;
continue;
}
System.out.print(stk.peek().data + " ");
root = stk.pop().right;
}
}
protected boolean isIdentical(Node root1,
Node root2) {
if (root1 == null && root2 == null) {
return true;
}
if (root1 != null && root2 != null) {
return ((root1.data == root2.data) &&
isIdentical(root1.left, root2.left) &&
isIdentical(root1.right, root2.right));
}
return false;
}
public static void main(String[] args) {
try{
SerializeBinaryTree serializeBinaryTree = new SerializeBinaryTree();
serializeBinaryTree.root1 = new Node(1);
serializeBinaryTree.root1.left = new Node(2);
serializeBinaryTree.root1.right = new Node(3);
serializeBinaryTree.root1.left.left = new Node(4);
serializeBinaryTree.root1.left.right = new Node(5);
serializeBinaryTree.root1.right.left = new Node(6);
serializeBinaryTree.root1.right.right = new Node(7);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream stream = new ObjectOutputStream(byteArrayOutputStream);
serializeBinaryTree.serialize(serializeBinaryTree.root1, stream);
stream.close();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
Node deserialize = serializeBinaryTree.deserialize(objectInputStream);
System.out.println("\nResult");
serializeBinaryTree.inOrderDisplay(deserialize);
System.out.println(serializeBinaryTree.isIdentical(serializeBinaryTree.root1, deserialize));
}
catch(Exception ex){
ex.printStackTrace();
}
}
}