-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryTreeSerializer.java
More file actions
59 lines (49 loc) · 1.51 KB
/
BinaryTreeSerializer.java
File metadata and controls
59 lines (49 loc) · 1.51 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
public class BinaryTreeSerializer {
class Node {
Integer data;
Node left;
Node right;
}
private Node tree;
public BinaryTreeSerializer(Integer rootData) {
tree = new Node();
tree.data = rootData;
}
public Node getRoot() {
return tree;
}
public Node addLeft(Node root, Integer data) {
Node left = constructNode(data);
root.left = left;
return left;
}
public Node addRight(Node root, Integer data) {
Node right = constructNode(data);
root.right = right;
return right;
}
Node constructNode(Integer data) {
Node node = new Node();
node.data = data;
return node;
}
public String serialize(Node root) {
if (root == null)
return "#";
return "" + root.data + "," + serialize(root.left) + "," + serialize(root.right);
}
public static void main(String[] args) {
System.out.println("Alhamdulillah");
BinaryTreeSerializer bts = new BinaryTreeSerializer(7);
Node root = bts.getRoot();
Node left = bts.addLeft(root, 5);
Node right = bts.addRight(root, 9);
Node left1 = bts.addLeft(left, 4);
Node right1 = bts.addRight(left, 6);
Node left11 = bts.addLeft(left1, 1);
Node right11 = bts.addRight(left1, 3);
Node left21 = bts.addLeft(right, 8);
Node right21 = bts.addRight(right, 10);
System.out.println(bts.serialize(root));
}
}