import java.util.ArrayList;
class Rope {
Rope left;
Rope right;
Rope parent;
ArrayList<Character> str;
int lCount;
Rope()
{
this.left = null;
this.right = null;
this.parent = null;
this.str = new ArrayList<Character>();
this.lCount = 0;
}
}
class Main {
static final int LEAF_LEN = 2;
static Rope createRopeStructure(Rope node, Rope par,
String a, int l, int r)
{
Rope tmp = new Rope();
tmp.left = tmp.right = null;
tmp.parent = par;
if ((r - l) > LEAF_LEN) {
tmp.str = null;
tmp.lCount = (int)Math.floor((r - l) / 2);
node = tmp;
int m = (int)Math.floor((l + r) / 2);
node.left = createRopeStructure(node.left, node,
a, l, m);
node.right = createRopeStructure(
node.right, node, a, m + 1, r);
}
else {
node = tmp;
tmp.lCount = (r - l);
int j = 0;
for (int i = l; i <= r; i++) {
tmp.str.add(a.charAt(i));
}
}
return node;
}
static void printstring(Rope r)
{
if (r == null) {
return;
}
if (r.left == null && r.right == null) {
for (char c : r.str) {
System.out.print(c);
}
}
printstring(r.left);
printstring(r.right);
}
static Rope concatenate(Rope root3, Rope root1,
Rope root2, int n1)
{
Rope tmp = new Rope();
tmp.left = root1;
tmp.right = root2;
root1.parent = tmp;
root2.parent = tmp;
tmp.lCount = n1;
tmp.str = null;
root3 = tmp;
return root3;
}
public static void main(String[] args)
{
Rope root1 = null;
String a = "Hi This is geeksforgeeks. ";
int n1 = a.length();
root1 = createRopeStructure(root1, null, a, 0,
n1 - 1);
Rope root2 = null;
String b = "You are welcome here.";
int n2 = b.length();
root2 = createRopeStructure(root2, null, b, 0,
n2 - 1);
Rope root3 = null;
root3 = concatenate(root3, root1, root2, n1);
printstring(root3);
}
}