原文:

给定一个只包含小写字符的字符串字符串。问题是使用二叉树 打印字符及其出现频率。示例:

输入:str = " aaaaabannccccz " 输出:【a4b2n2 c4z】 解释:

输入:输出: g2e4k2s2for

进场:

  1. 从字符串中的第一个字符开始。
  2. 在二叉树中执行字符的级别顺序插入
  3. 选择下一个字符:
    • 如果已经看到该字符,并且我们在级别顺序插入期间遇到它,则增加节点的计数。
    • 如果到目前为止还没有看到该角色,请转到第 2 步。
  4. 对字符串的所有字符重复该过程。
  5. 打印应该输出所需输出的树的层次顺序遍历。

以下是上述方法的实现:

c

// c   implementation of
// the above approach
#include 
using namespace std;
// node in the tree where
// data holds the character
// of the string and cnt
// holds the frequency
struct node {
    char data;
    int cnt;
    node *left, *right;
};
// function to add a new
// node to the binary tree
node* add(char data)
{
    // create a new node and
    // populate its data part,
    // set cnt as 1 and left
    // and right children as null
    node* newnode = new node;
    newnode->data = data;
    newnode->cnt = 1;
    newnode->left = newnode->right = null;
    return newnode;
}
// function to add a node
// to the binary tree in
// level order
node* addinlvlorder(node* root, char data)
{
    if (root == null) {
        return add(data);
    }
    // use the queue data structure
    // for level order insertion
    // and push the root of tree to queue
    queue q;
    q.push(root);
    while (!q.empty()) {
        node* temp = q.front();
        q.pop();
        // if the character to be
        // inserted is present,
        // update the cnt
        if (temp->data == data) {
            temp->cnt  ;
            break;
        }
        // if the left child is
        // empty add a new node
        // as the left child
        if (temp->left == null) {
            temp->left = add(data);
            break;
        }
        else {
            // if the character is present
            // as a left child, update the
            // cnt and exit the loop
            if (temp->left->data == data) {
                temp->left->cnt  ;
                break;
            }
            // add the left child to
            // the queue for further
            // processing
            q.push(temp->left);
        }
        // if the right child is empty,
        // add a new node to the right
        if (temp->right == null) {
            temp->right = add(data);
            break;
        }
        else {
            // if the character is present
            // as a right child, update the
            // cnt and exit the loop
            if (temp->right->data == data) {
                temp->right->cnt  ;
                break;
            }
            // add the right child to
            // the queue for further
            // processing
            q.push(temp->right);
        }
    }
    return root;
}
// function to print the
// level order traversal of
// the binary tree
void printlvlorder(node* root)
{
    // add the root to the queue
    queue q;
    q.push(root);
    while (!q.empty()) {
        node* temp = q.front();
        // if the cnt of the character
        // is more then one, display cnt
        if (temp->cnt > 1) {
            cout << temp->data << temp->cnt;
        }
        // if the cnt of character
        // is one, display character only
        else {
            cout << temp->data;
        }
        q.pop();
        // add the left child to
        // the queue for further
        // processing
        if (temp->left != null) {
            q.push(temp->left);
        }
        // add the right child to
        // the queue for further
        // processing
        if (temp->right != null) {
            q.push(temp->right);
        }
    }
}
// driver code
int main()
{
    string s = "geeksforgeeks";
    node* root = null;
    // add individual characters
    // to the string one by one
    // in level order
    for (int i = 0; i < s.size(); i  ) {
        root = addinlvlorder(root, s[i]);
    }
    // print the level order
    // of the constructed
    // binary tree
    printlvlorder(root);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java implementation of
// the above approach
import java.util.*;
class gfg
{
// node in the tree where
// data holds the character
// of the string and cnt
// holds the frequency
static class node
{
    char data;
    int cnt;
    node left, right;
};
// function to add a new
// node to the binary tree
static node add(char data)
{
    // create a new node and
    // populate its data part,
    // set cnt as 1 and left
    // and right children as null
    node newnode = new node();
    newnode.data = data;
    newnode.cnt = 1;
    newnode.left = newnode.right = null;
    return newnode;
}
// function to add a node
// to the binary tree in
// level order
static node addinlvlorder(node root, char data)
{
    if (root == null)
    {
        return add(data);
    }
    // use the queue data structure
    // for level order insertion
    // and push the root of tree to queue
    queue q = new linkedlist();
    q.add(root);
    while (!q.isempty())
    {
        node temp = q.peek();
        q.remove();
        // if the character to be
        // inserted is present,
        // update the cnt
        if (temp.data == data)
        {
            temp.cnt  ;
            break;
        }
        // if the left child is
        // empty add a new node
        // as the left child
        if (temp.left == null)
        {
            temp.left = add(data);
            break;
        }
        else
        {
            // if the character is present
            // as a left child, update the
            // cnt and exit the loop
            if (temp.left.data == data)
            {
                temp.left.cnt  ;
                break;
            }
            // add the left child to
            // the queue for further
            // processing
            q.add(temp.left);
        }
        // if the right child is empty,
        // add a new node to the right
        if (temp.right == null)
        {
            temp.right = add(data);
            break;
        }
        else
        {
            // if the character is present
            // as a right child, update the
            // cnt and exit the loop
            if (temp.right.data == data)
            {
                temp.right.cnt  ;
                break;
            }
            // add the right child to
            // the queue for further
            // processing
            q.add(temp.right);
        }
    }
    return root;
}
// function to print the
// level order traversal of
// the binary tree
static void printlvlorder(node root)
{
    // add the root to the queue
    queue q = new linkedlist();
    q.add(root);
    while (!q.isempty())
    {
        node temp = q.peek();
        // if the cnt of the character
        // is more then one, display cnt
        if (temp.cnt > 1)
        {
            system.out.print((temp.data  ""  temp.cnt));
        }
        // if the cnt of character
        // is one, display character only
        else
        {
            system.out.print((char)temp.data);
        }
        q.remove();
        // add the left child to
        // the queue for further
        // processing
        if (temp.left != null)
        {
            q.add(temp.left);
        }
        // add the right child to
        // the queue for further
        // processing
        if (temp.right != null)
        {
            q.add(temp.right);
        }
    }
}
// driver code
public static void main(string[] args)
{
    string s = "geeksforgeeks";
    node root = null;
    // add individual characters
    // to the string one by one
    // in level order
    for (int i = 0; i < s.length(); i  )
    {
        root = addinlvlorder(root, s.charat(i));
    }
    // print the level order
    // of the constructed
    // binary tree
    printlvlorder(root);
}
}
// this code is contributed by rajput-ji

c

// c# implementation of
// the above approach
using system;
using system.collections.generic;
class gfg
{
// node in the tree where
// data holds the character
// of the string and cnt
// holds the frequency
public class node
{
    public char data;
    public int cnt;
    public node left, right;
};
// function to add a new
// node to the binary tree
static node add(char data)
{
    // create a new node and
    // populate its data part,
    // set cnt as 1 and left
    // and right children as null
    node newnode = new node();
    newnode.data = data;
    newnode.cnt = 1;
    newnode.left = newnode.right = null;
    return newnode;
}
// function to add a node
// to the binary tree in
// level order
static node addinlvlorder(node root, char data)
{
    if (root == null)
    {
        return add(data);
    }
    // use the queue data structure
    // for level order insertion
    // and push the root of tree to queue
    list q = new list();
    q.add(root);
    while (q.count != 0)
    {
        node temp = q[0];
        q.removeat(0);
        // if the character to be
        // inserted is present,
        // update the cnt
        if (temp.data == data)
        {
            temp.cnt  ;
            break;
        }
        // if the left child is
        // empty add a new node
        // as the left child
        if (temp.left == null)
        {
            temp.left = add(data);
            break;
        }
        else
        {
            // if the character is present
            // as a left child, update the
            // cnt and exit the loop
            if (temp.left.data == data)
            {
                temp.left.cnt  ;
                break;
            }
            // add the left child to
            // the queue for further
            // processing
            q.add(temp.left);
        }
        // if the right child is empty,
        // add a new node to the right
        if (temp.right == null)
        {
            temp.right = add(data);
            break;
        }
        else
        {
            // if the character is present
            // as a right child, update the
            // cnt and exit the loop
            if (temp.right.data == data)
            {
                temp.right.cnt  ;
                break;
            }
            // add the right child to
            // the queue for further
            // processing
            q.add(temp.right);
        }
    }
    return root;
}
// function to print the
// level order traversal of
// the binary tree
static void printlvlorder(node root)
{
    // add the root to the queue
    list q = new list();
    q.add(root);
    while (q.count != 0)
    {
        node temp = q[0];
        // if the cnt of the character
        // is more then one, display cnt
        if (temp.cnt > 1)
        {
            console.write((temp.data  ""  temp.cnt));
        }
        // if the cnt of character
        // is one, display character only
        else
        {
            console.write((char)temp.data);
        }
        q.removeat(0);
        // add the left child to
        // the queue for further
        // processing
        if (temp.left != null)
        {
            q.add(temp.left);
        }
        // add the right child to
        // the queue for further
        // processing
        if (temp.right != null)
        {
            q.add(temp.right);
        }
    }
}
// driver code
public static void main(string[] args)
{
    string s = "geeksforgeeks";
    node root = null;
    // add individual characters
    // to the string one by one
    // in level order
    for (int i = 0; i < s.length; i  )
    {
        root = addinlvlorder(root, s[i]);
    }
    // print the level order
    // of the constructed
    // binary tree
    printlvlorder(root);
}
}
// this code is contributed by rajput-ji

java 描述语言


output: 

g2e4k2s2for