二叉排序树统计字符串中出现的字符及其次数

二叉排序树统计字符串

结点的类型:

              typedef struct tnode
                   {
               char ch;      //字符
               int count;    //出现次数
               struct tnode *lchild,*rchild;
                }  tnode,*BTree;

完整代码

//文件名:exp9-5.cpp
#include<iostream>
using namespace std;
#define MAXWORD 100
typedef struct tnode
// typedef tnode *BTree
{
    char ch;      //字符
    int count;    //出现次数
    struct tnode *lchild,*rchild;
}  tnode ,*BTree;
void CreaTree(BTree &p,char c) //采用递归方式构造一棵二叉排序树
{
    if (p==NULL)                //p为NULL,则建立一个新结点
    {
        p=new tnode;
        p->ch=c;
        p->count=1;
        p->lchild=p->rchild=NULL;
    }
    else if (c==p->ch){
        p->count++;
    }
    else if (c<p->ch) {
        CreaTree(p->lchild,c);
    }
    else {
        CreaTree(p->rchild,c);
    }
}
void InOrder(BTree p)   //中序遍历BST
{
    if (p!=NULL)
    {
        InOrder(p->lchild);                 //中序遍历左子树
        cout<<p->ch<<":"<<p->count<<endl;//访问根结点
        InOrder(p->rchild);                 //中序遍历右子树
    }
}
int main()
{
    BTree root=NULL;
    int i=0;
    char str[MAXWORD];
    cout<<("输入字符串:");
    gets(str);
    while (str[i]!='\0')
    {
        CreaTree(root,str[i]);
        i++;
    }
    cout<<"字符及出现次数:\n";
    InOrder(root);
    cout<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/ygjzs/p/12063134.html

时间: 2024-08-30 01:37:00

二叉排序树统计字符串中出现的字符及其次数的相关文章

统计字符串中,各个字符的个数(回炉练习)

__author__ = 'ZHHT' #!/usr/bin/env python # -*- coding:utf-8 -*- #统计字符串中,各个字符的个数 #比如:"hello world" 字符串统计的结果为: h:1 e:1 l:3 o:2 d:1 r:1 w:1 a = "hello world" b = set(a) for i in b: if i == ' ': c = a.count(i) i = '空格' print("%s出现%d次

Python统计字符串中的中英文字符、数字空格,特殊字符

# -*- coding:utf8 -*- import string from collections import namedtuple def str_count(s): '''找出字符串中的中英文.空格.数字.标点符号个数''' count_en = count_dg = count_sp = count_zh = count_pu = 0 s_len = len(s) for c in s: # 英文 if c in string.ascii_letters: count_en +=

【面试题总结】1、统计字符串中某个单词出现的次数

[解决方法一]C++ map解决 一.map中的find函数: 用于查找map中是否包含某个关键字条目,传入的参数是要查找的key,最后返回一个迭代器,如果没有找到,则返回的迭代器等于end()返回的迭代器.示例代码: 1 #include<iostream> 2 #include<string> 3 #include<map> 4 using namespace std; 5 6 int main() { 7 map<int, string> mapStu

【SQL Server 学习系列】-- 获取字符串中出现某字符的次数及字符某次出现的下标

DECLARE @Str NVARCHAR(500) = '1_BB_CC_DD_AA_EE_YY_WW_HH_GG' --// 1. 获取下划线在字符串中出现的次数 SELECT LEN(@Str) - LEN(REPLACE(@Str, '_', '')) --// 2. 获取下划线某次出现的位置下标 IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[IndexNumOfStr]') AND t

统计字符串中重复的字符个数及字符

做华为的试题,发现有很多需要字符串重复相关知识的.现在补上: #include <stdio.h> #include <stdlib.h> #include <string> #include <string.h> #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(int argc, char**

python之统计字符串中字母出现次数

dic=dict() d={} s=set() s='helloworld' (1)d=dict() for x in s: if x not in d.keys(): d[x]=1 else: d[x]=d[x]+1 print(d) (2)d2=dict() for x in s: d2[x]=d2.get(x,0)+1 print(d2) (3)d3=dict() for x in s: d3[x]=s.count(x) print(d3) 上面一共给出了三种方法,均是以字典的形式输出,但

技巧之C#统计字符串中字符出现的次数(转)

方法1.自定义类 class CharNum { private char c; private int num; public char C { get { return c; } } public int Num { get { return num; } set { num = value; } } public CharNum(char ch) { this.c = ch; this.num = 1; } } static void Main(string[] args) { /* */

Java基础知识强化之集合框架笔记61:Map集合之统计字符串中每个字符出现的次数的案例

1. 首先我们看看统计字符串中每个字符出现的次数的案例图解:

java统计字符串中字符及子字符串个数

import java.util.Scanner;public class Counter { static Scanner scanner = new Scanner(System.in); public static void count(String s) { int low, upper, num, others; low = upper = num = others = 0; for (int i = 0; i < s.length(); i++) { if (Character.is