Given a string str containing only lowercase characters. The problem is to print the characters along with their frequency in the order of their occurrence using Binary Tree
Examples:
Input: str = “aaaabbnnccccz”
Output: “a4b2n2c4z”
Explanation:
Input: str = “geeksforgeeks”
Output: g2e4k2s2for
Approach:
- Start with the first character in the string.
- Perform a level order insertion of the character in the Binary Tree
- Pick the next character:
- If the character has been seen and we encounter it during level order insertion increase the count of the node.
- If the character has not been seen so far, go to step number 2.
- Repeat the process for all the characters of the string.
- Print the level order traversal of the tree which should output the desired output.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct node {
char data;
int cnt;
node *left, *right;
};
node* add( char data)
{
node* newnode = new node;
newnode->data = data;
newnode->cnt = 1;
newnode->left = newnode->right = NULL;
return newnode;
}
node* addinlvlorder(node* root, char data)
{
if (root == NULL) {
return add(data);
}
queue<node*> Q;
Q.push(root);
while (!Q.empty()) {
node* temp = Q.front();
Q.pop();
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.push(temp->left);
}
if (temp->right == NULL) {
temp->right = add(data);
break ;
}
else {
if (temp->right->data == data) {
temp->right->cnt++;
break ;
}
Q.push(temp->right);
}
}
return root;
}
void printlvlorder(node* root)
{
queue<node*> Q;
Q.push(root);
while (!Q.empty()) {
node* temp = Q.front();
if (temp->cnt > 1) {
cout << temp->data << temp->cnt;
}
else {
cout << temp->data;
}
Q.pop();
if (temp->left != NULL) {
Q.push(temp->left);
}
if (temp->right != NULL) {
Q.push(temp->right);
}
}
}
int main()
{
string s = "geeksforgeeks" ;
node* root = NULL;
for ( int i = 0; i < s.size(); i++) {
root = addinlvlorder(root, s[i]);
}
printlvlorder(root);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class node
{
char data;
int cnt;
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);
}
Queue<node> Q = new LinkedList<node>();
Q.add(root);
while (!Q.isEmpty())
{
node temp = Q.peek();
Q.remove();
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)
{
Queue<node> Q = new LinkedList<node>();
Q.add(root);
while (!Q.isEmpty())
{
node temp = Q.peek();
if (temp.cnt > 1 )
{
System.out.print((temp.data + "" + temp.cnt));
}
else
{
System.out.print(( char )temp.data);
}
Q.remove();
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.charAt(i));
}
printlvlorder(root);
}
}
|
Python3
class node:
def __init__( self ):
self .data = ''
self .cnt = 0
self .left = None
self .right = None
def add(data):
newnode = node()
newnode.data = data
newnode.cnt = 1
newnode.left = newnode.right = None
return newnode
def addinlvlorder(root, data):
if (root = = None ):
return add(data)
Q = []
Q.append(root)
while ( len (Q) ! = 0 ):
temp = Q[ 0 ]
Q = Q[ 1 :]
if (temp.data = = data):
temp.cnt + = 1
break
if (temp.left = = None ):
temp.left = add(data)
break
else :
if (temp.left.data = = data):
temp.left.cnt + = 1
break
Q.append(temp.left)
if (temp.right = = None ):
temp.right = add(data)
break
else :
if (temp.right.data = = data):
temp.right.cnt + = 1
break
Q.append(temp.right)
return root
def printlvlorder(root):
Q = []
Q.append(root)
while ( len (Q) ! = 0 ):
temp = Q[ 0 ]
if (temp.cnt > 1 ):
print (f "{temp.data}{temp.cnt}" ,end = "")
else :
print (temp.data,end = "")
Q = Q[ 1 :]
if (temp.left ! = None ):
Q.append(temp.left)
if (temp.right ! = None ):
Q.append(temp.right)
s = "geeksforgeeks"
root = None
for i in range ( len (s)):
root = addinlvlorder(root, s[i])
printlvlorder(root)
|
C#
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);
}
}
|
Javascript
<script>
class node
{
constructor()
{
this .data = '' ;
this .cnt = 0;
this .left = null ;
this .right = null ;
}
};
function add(data)
{
var newnode = new node();
newnode.data = data;
newnode.cnt = 1;
newnode.left = newnode.right = null ;
return newnode;
}
function addinlvlorder(root, data)
{
if (root == null )
{
return add(data);
}
var Q = [];
Q.push(root);
while (Q.length != 0)
{
var temp = Q[0];
Q.shift();
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.push(temp.left);
}
if (temp.right == null )
{
temp.right = add(data);
break ;
}
else
{
if (temp.right.data == data)
{
temp.right.cnt++;
break ;
}
Q.push(temp.right);
}
}
return root;
}
function printlvlorder(root)
{
var Q = [];
Q.push(root);
while (Q.length != 0)
{
var temp = Q[0];
if (temp.cnt > 1)
{
document.write((temp.data + "" + temp.cnt));
}
else
{
document.write(temp.data);
}
Q.shift();
if (temp.left != null )
{
Q.push(temp.left);
}
if (temp.right != null )
{
Q.push(temp.right);
}
}
}
var s = "geeksforgeeks" ;
var root = null ;
for ( var i = 0; i < s.length; i++)
{
root = addinlvlorder(root, s[i]);
}
printlvlorder(root);
</script>
|
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
15 Dec, 2022
Like Article
Save Article