From a76f68e294b3f1e642dbf2320b05e65768198352 Mon Sep 17 00:00:00 2001 From: Aksshay88 Date: Thu, 10 Oct 2024 09:17:34 +0530 Subject: [PATCH 1/7] Serialize and Deserialize the Binary Tree added --- .../trees/SerializeaBinaryTree.java | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java diff --git a/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java b/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java new file mode 100644 index 000000000000..b8470d5f0ad3 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java @@ -0,0 +1,133 @@ +import java.util.*; + +// Define the TreeNode class to represent nodes in the binary tree. +class TreeNode { + int val; // Value stored in the node + TreeNode left; // Pointer to the left child + TreeNode right; // Pointer to the right child + + // Constructor to create a new node with a given value + TreeNode(int val) { + this.val = val; + this.left = null; // Initially, the left child is null + this.right = null; // Initially, the right child is null + } +} + +public class BinaryTreeSerializer { + + // The 'serialize' method converts a binary tree into a string. + public String serialize(TreeNode root) { + // If the tree is empty (root is null), return an empty string. + if (root == null) return ""; + + // StringBuilder is used to build the final serialized string efficiently. + StringBuilder sb = new StringBuilder(); + + // We will use a queue to perform level-order traversal (breadth-first search). + Queue queue = new LinkedList<>(); + + // Start by adding the root node to the queue. + queue.add(root); + + // Process each node in the queue, and append its value (or "null" for empty nodes) to the result. + while (!queue.isEmpty()) { + // Remove the front node from the queue. + TreeNode node = queue.poll(); + + // If the node is not null, process its value and add its children to the queue. + if (node != null) { + // Append the node's value to the string, followed by a comma to separate values. + sb.append(node.val).append(","); + + // Add the left and right children to the queue (even if they are null). + queue.add(node.left); + queue.add(node.right); + } else { + // If the node is null, append "null" to the string to represent the absence of a node. + sb.append("null,"); + } + } + + // Remove the last comma from the end of the string to make it clean. + sb.setLength(sb.length() - 1); + + // Return the final serialized string. + return sb.toString(); + } + + // The 'deserialize' method converts a string back into a binary tree. + public TreeNode deserialize(String data) { + // If the input string is empty, return null (i.e., the tree is empty). + if (data == null || data.isEmpty()) return null; + + // Split the string by commas to get the serialized node values. + String[] nodes = data.split(","); + + // The first value in the string is the root node. + TreeNode root = new TreeNode(Integer.parseInt(nodes[0])); + + // Use a queue to manage the nodes during reconstruction of the tree. + Queue queue = new LinkedList<>(); + queue.add(root); + + // Use an index to track the current position in the node array (starting from 1, since 0 is root). + int index = 1; + + // Process each node in the queue to reconstruct the tree. + while (!queue.isEmpty()) { + // Get the current node from the queue. + TreeNode node = queue.poll(); + + // Rebuild the left child. + if (!nodes[index].equals("null")) { + // If the value is not "null", create a new node and add it to the left. + node.left = new TreeNode(Integer.parseInt(nodes[index])); + // Add the left child to the queue for further processing. + queue.add(node.left); + } + index++; // Move to the next value in the array. + + // Rebuild the right child. + if (!nodes[index].equals("null")) { + // If the value is not "null", create a new node and add it to the right. + node.right = new TreeNode(Integer.parseInt(nodes[index])); + // Add the right child to the queue for further processing. + queue.add(node.right); + } + index++; // Move to the next value in the array. + } + + // Return the root of the reconstructed tree. + return root; + } + + public static void main(String[] args) { + // Example of creating a binary tree manually. + // 1 + // / \ + // 2 3 + // / \ + // 4 5 + + TreeNode root = new TreeNode(1); // Create the root node with value 1 + root.left = new TreeNode(2); // Create the left child (value 2) + root.right = new TreeNode(3); // Create the right child (value 3) + root.right.left = new TreeNode(4); // Create the left child of node 3 (value 4) + root.right.right = new TreeNode(5); // Create the right child of node 3 (value 5) + + // Instantiate the BinaryTreeSerializer class + BinaryTreeSerializer serializer = new BinaryTreeSerializer(); + + // Serialize the tree to a string format + String serializedTree = serializer.serialize(root); + System.out.println("Serialized Tree: " + serializedTree); + // Expected output: "1,2,3,null,null,4,5,null,null,null,null" + + // Deserialize the string back to a binary tree + TreeNode deserializedRoot = serializer.deserialize(serializedTree); + System.out.println("Tree deserialized successfully. Root value: " + deserializedRoot.val); + // Expected output: Root value should be 1 + } +} + From 48fa2cd02e76006f84f8cfd67a5764dbbe0b5ae6 Mon Sep 17 00:00:00 2001 From: Aksshay88 Date: Thu, 10 Oct 2024 12:41:31 +0530 Subject: [PATCH 2/7] Serialize and Deserialize the Binary Tree added --- .../datastructures/trees/SerializeaBinaryTree.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java b/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java index b8470d5f0ad3..aca4bc315844 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java @@ -14,7 +14,7 @@ class TreeNode { } } -public class BinaryTreeSerializer { +public class SerializeaBinaryTree { // The 'serialize' method converts a binary tree into a string. public String serialize(TreeNode root) { @@ -116,8 +116,8 @@ public static void main(String[] args) { root.right.left = new TreeNode(4); // Create the left child of node 3 (value 4) root.right.right = new TreeNode(5); // Create the right child of node 3 (value 5) - // Instantiate the BinaryTreeSerializer class - BinaryTreeSerializer serializer = new BinaryTreeSerializer(); + // Instantiate the SerializeaBinaryTree class + SerializeaBinaryTree serializer = new SerializeaBinaryTree(); // Serialize the tree to a string format String serializedTree = serializer.serialize(root); From 95dda15cda8ee99fb935153d33e61b1bc809b974 Mon Sep 17 00:00:00 2001 From: Aksshay88 Date: Thu, 10 Oct 2024 12:54:38 +0530 Subject: [PATCH 3/7] Serialize and Deserialize the Binary Tree added --- .../datastructures/trees/SerializeaBinaryTree.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java b/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java index aca4bc315844..7f20872b4792 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java @@ -1,4 +1,5 @@ -import java.util.*; +import java.util.LinkedList; // Replace with specific imports +import java.util.Queue; // Define the TreeNode class to represent nodes in the binary tree. class TreeNode { @@ -19,7 +20,9 @@ public class SerializeaBinaryTree { // The 'serialize' method converts a binary tree into a string. public String serialize(TreeNode root) { // If the tree is empty (root is null), return an empty string. - if (root == null) return ""; + if (root == null) { + return ""; + } // StringBuilder is used to build the final serialized string efficiently. StringBuilder sb = new StringBuilder(); @@ -59,7 +62,9 @@ public String serialize(TreeNode root) { // The 'deserialize' method converts a string back into a binary tree. public TreeNode deserialize(String data) { // If the input string is empty, return null (i.e., the tree is empty). - if (data == null || data.isEmpty()) return null; + if (data == null || data.isEmpty()) { + return null; + } // Split the string by commas to get the serialized node values. String[] nodes = data.split(","); From 5cb53d0b46f85a16d021f031f1027a11e0bd9cef Mon Sep 17 00:00:00 2001 From: Aksshay88 Date: Thu, 10 Oct 2024 13:04:18 +0530 Subject: [PATCH 4/7] Serialize and Deserialize the Binary Tree added --- .../trees/SerializeaBinaryTree.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java b/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java index 7f20872b4792..a0916397d113 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java @@ -3,14 +3,14 @@ // Define the TreeNode class to represent nodes in the binary tree. class TreeNode { - int val; // Value stored in the node - TreeNode left; // Pointer to the left child - TreeNode right; // Pointer to the right child + int val;// Value stored in the node + TreeNode left;// Pointer to the left child + TreeNode right;// Pointer to the right child // Constructor to create a new node with a given value TreeNode(int val) { this.val = val; - this.left = null; // Initially, the left child is null + this.left = null; // Initially, the left child is null this.right = null; // Initially, the right child is null } } @@ -115,11 +115,11 @@ public static void main(String[] args) { // / \ // 4 5 - TreeNode root = new TreeNode(1); // Create the root node with value 1 - root.left = new TreeNode(2); // Create the left child (value 2) - root.right = new TreeNode(3); // Create the right child (value 3) - root.right.left = new TreeNode(4); // Create the left child of node 3 (value 4) - root.right.right = new TreeNode(5); // Create the right child of node 3 (value 5) + TreeNode root = new TreeNode(1); // Create the root node with value 1 + root.left = new TreeNode(2); // Create the left child (value 2) + root.right = new TreeNode(3); // Create the right child (value 3) + root.right.left = new TreeNode(4); // Create the left child of node 3 (value 4) + root.right.right = new TreeNode(5); // Create the right child of node 3 (value 5) // Instantiate the SerializeaBinaryTree class SerializeaBinaryTree serializer = new SerializeaBinaryTree(); @@ -132,7 +132,6 @@ public static void main(String[] args) { // Deserialize the string back to a binary tree TreeNode deserializedRoot = serializer.deserialize(serializedTree); System.out.println("Tree deserialized successfully. Root value: " + deserializedRoot.val); - // Expected output: Root value should be 1 } } From 14615d5715760804b3d0ac6ac8ad598a78d26b18 Mon Sep 17 00:00:00 2001 From: Aksshay88 <119944779+Aksshay88@users.noreply.github.com> Date: Thu, 10 Oct 2024 13:11:17 +0530 Subject: [PATCH 5/7] Final Update SerializeaBinaryTree.java --- .../datastructures/trees/SerializeaBinaryTree.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java b/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java index a0916397d113..0b1e97cc7780 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java @@ -3,9 +3,9 @@ // Define the TreeNode class to represent nodes in the binary tree. class TreeNode { - int val;// Value stored in the node - TreeNode left;// Pointer to the left child - TreeNode right;// Pointer to the right child + int val; // Value stored in the node + TreeNode left; // Pointer to the left child + TreeNode right; // Pointer to the right child // Constructor to create a new node with a given value TreeNode(int val) { @@ -91,7 +91,7 @@ public TreeNode deserialize(String data) { // Add the left child to the queue for further processing. queue.add(node.left); } - index++; // Move to the next value in the array. + index++; // Move to the next value in the array. // Rebuild the right child. if (!nodes[index].equals("null")) { @@ -100,7 +100,7 @@ public TreeNode deserialize(String data) { // Add the right child to the queue for further processing. queue.add(node.right); } - index++; // Move to the next value in the array. + index++; // Move to the next value in the array. } // Return the root of the reconstructed tree. From c94b20f0c4698a54f39366c532ae8d0d1e4467a0 Mon Sep 17 00:00:00 2001 From: Aksshay88 <119944779+Aksshay88@users.noreply.github.com> Date: Thu, 10 Oct 2024 13:15:02 +0530 Subject: [PATCH 6/7] Update SerializeaBinaryTree.java --- .../thealgorithms/datastructures/trees/SerializeaBinaryTree.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java b/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java index 0b1e97cc7780..8315ee4507f4 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java @@ -132,6 +132,7 @@ public static void main(String[] args) { // Deserialize the string back to a binary tree TreeNode deserializedRoot = serializer.deserialize(serializedTree); System.out.println("Tree deserialized successfully. Root value: " + deserializedRoot.val); + // Expected output: Root value should be 1 } } From 8fffd59563856d64c8870966bc7aa7c0db6d3f12 Mon Sep 17 00:00:00 2001 From: Aksshay88 <119944779+Aksshay88@users.noreply.github.com> Date: Thu, 10 Oct 2024 13:17:34 +0530 Subject: [PATCH 7/7] Removed Empty space SerializeaBinaryTree.java --- .../thealgorithms/datastructures/trees/SerializeaBinaryTree.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java b/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java index 8315ee4507f4..2a0923695f38 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java @@ -135,4 +135,3 @@ public static void main(String[] args) { // Expected output: Root value should be 1 } } -