using
System;
using
System.Collections.Generic;
class
GFG
{
public
class
node
{
public
char
data;
public
int
cnt;
public
node left, right;
};
static
node add(
char
data)
{
node newnode =
new
node();
newnode.data = data;
newnode.cnt = 1;
newnode.left = newnode.right =
null
;
return
newnode;
}
static
node addinlvlorder(node root,
char
data)
{
if
(root ==
null
)
{
return
add(data);
}
List<node> Q =
new
List<node>();
Q.Add(root);
while
(Q.Count != 0)
{
node temp = Q[0];
Q.RemoveAt(0);
if
(temp.data == data)
{
temp.cnt++;
break
;
}
if
(temp.left ==
null
)
{
temp.left = add(data);
break
;
}
else
{
if
(temp.left.data == data)
{
temp.left.cnt++;
break
;
}
Q.Add(temp.left);
}
if
(temp.right ==
null
)
{
temp.right = add(data);
break
;
}
else
{
if
(temp.right.data == data)
{
temp.right.cnt++;
break
;
}
Q.Add(temp.right);
}
}
return
root;
}
static
void
printlvlorder(node root)
{
List<node> Q =
new
List<node>();
Q.Add(root);
while
(Q.Count != 0)
{
node temp = Q[0];
if
(temp.cnt > 1)
{
Console.Write((temp.data +
""
+ temp.cnt));
}
else
{
Console.Write((
char
)temp.data);
}
Q.RemoveAt(0);
if
(temp.left !=
null
)
{
Q.Add(temp.left);
}
if
(temp.right !=
null
)
{
Q.Add(temp.right);
}
}
}
public
static
void
Main(String[] args)
{
String s =
"geeksforgeeks"
;
node root =
null
;
for
(
int
i = 0; i < s.Length; i++)
{
root = addinlvlorder(root, s[i]);
}
printlvlorder(root);
}
}