Skip to content

Commit ef6764c

Browse files
committed
feat: BinaryTreeToString.java Added with tests
1 parent c691b31 commit ef6764c

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
/**
4+
* Converts a Binary Tree to its string representation using preorder traversal.
5+
* Rules:
6+
* - Each node is wrapped in parentheses (val)
7+
* - Include "()" when a right child exists without a left child
8+
* - Skip empty parentheses otherwise
9+
*
10+
* Example:
11+
* Input: 1
12+
* / \
13+
* 2 3
14+
* \
15+
* 4
16+
* Output: "1(2()(4))(3)"
17+
*
18+
* Matches the logic from LeetCode 606, using a DFS traversal.
19+
*/
20+
public class BinaryTreeToString {
21+
22+
private StringBuilder sb;
23+
24+
public String tree2str(BinaryTree.Node root) {
25+
if (root == null) {
26+
return "";
27+
}
28+
29+
sb = new StringBuilder();
30+
dfs(root);
31+
// remove the first and last parentheses
32+
return sb.substring(1, sb.length() - 1);
33+
}
34+
35+
private void dfs(BinaryTree.Node node) {
36+
if (node == null) {
37+
return;
38+
}
39+
40+
sb.append("(").append(node.data);
41+
42+
if (node.left != null) {
43+
dfs(node.left);
44+
}
45+
46+
if (node.right != null && node.left == null) {
47+
sb.append("()");
48+
dfs(node.right);
49+
} else if (node.right != null) {
50+
dfs(node.right);
51+
}
52+
53+
sb.append(")");
54+
}
55+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
/**
7+
* Tests for the BinaryTreeToString class.
8+
*/
9+
public class BinaryTreeToStringTest {
10+
11+
@Test
12+
public void testTreeToStringBasic() {
13+
BinaryTree tree = new BinaryTree();
14+
tree.put(1);
15+
tree.put(2);
16+
tree.put(3);
17+
tree.put(4);
18+
19+
BinaryTreeToString converter = new BinaryTreeToString();
20+
String result = converter.tree2str(tree.getRoot());
21+
22+
// Output will depend on insertion logic of BinaryTree.put()
23+
// which is BST-style, so result = "1()(2()(3()(4)))"
24+
Assertions.assertEquals("1()(2()(3()(4)))", result);
25+
}
26+
27+
@Test
28+
public void testSingleNodeTree() {
29+
BinaryTree tree = new BinaryTree();
30+
tree.put(10);
31+
32+
BinaryTreeToString converter = new BinaryTreeToString();
33+
String result = converter.tree2str(tree.getRoot());
34+
35+
Assertions.assertEquals("10", result);
36+
}
37+
38+
@Test
39+
public void testComplexTreeStructure() {
40+
BinaryTree.Node root = new BinaryTree.Node(10);
41+
root.left = new BinaryTree.Node(5);
42+
root.right = new BinaryTree.Node(20);
43+
root.right.left = new BinaryTree.Node(15);
44+
root.right.right = new BinaryTree.Node(25);
45+
46+
BinaryTreeToString converter = new BinaryTreeToString();
47+
String result = converter.tree2str(root);
48+
49+
Assertions.assertEquals("10(5)(20(15)(25))", result);
50+
}
51+
52+
@Test
53+
public void testNullTree() {
54+
BinaryTreeToString converter = new BinaryTreeToString();
55+
Assertions.assertEquals("", converter.tree2str(null));
56+
}
57+
}

0 commit comments

Comments
 (0)