Skip to content

Commit 5cb53d0

Browse files
committed
Serialize and Deserialize the Binary Tree added
1 parent 95dda15 commit 5cb53d0

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
// Define the TreeNode class to represent nodes in the binary tree.
55
class TreeNode {
6-
int val; // Value stored in the node
7-
TreeNode left; // Pointer to the left child
8-
TreeNode right; // Pointer to the right child
6+
int val;// Value stored in the node
7+
TreeNode left;// Pointer to the left child
8+
TreeNode right;// Pointer to the right child
99

1010
// Constructor to create a new node with a given value
1111
TreeNode(int val) {
1212
this.val = val;
13-
this.left = null; // Initially, the left child is null
13+
this.left = null; // Initially, the left child is null
1414
this.right = null; // Initially, the right child is null
1515
}
1616
}
@@ -115,11 +115,11 @@ public static void main(String[] args) {
115115
// / \
116116
// 4 5
117117

118-
TreeNode root = new TreeNode(1); // Create the root node with value 1
119-
root.left = new TreeNode(2); // Create the left child (value 2)
120-
root.right = new TreeNode(3); // Create the right child (value 3)
121-
root.right.left = new TreeNode(4); // Create the left child of node 3 (value 4)
122-
root.right.right = new TreeNode(5); // Create the right child of node 3 (value 5)
118+
TreeNode root = new TreeNode(1); // Create the root node with value 1
119+
root.left = new TreeNode(2); // Create the left child (value 2)
120+
root.right = new TreeNode(3); // Create the right child (value 3)
121+
root.right.left = new TreeNode(4); // Create the left child of node 3 (value 4)
122+
root.right.right = new TreeNode(5); // Create the right child of node 3 (value 5)
123123

124124
// Instantiate the SerializeaBinaryTree class
125125
SerializeaBinaryTree serializer = new SerializeaBinaryTree();
@@ -132,7 +132,6 @@ public static void main(String[] args) {
132132
// Deserialize the string back to a binary tree
133133
TreeNode deserializedRoot = serializer.deserialize(serializedTree);
134134
System.out.println("Tree deserialized successfully. Root value: " + deserializedRoot.val);
135-
// Expected output: Root value should be 1
136135
}
137136
}
138137

0 commit comments

Comments
 (0)