poj2418 Hardwood Species 排序二叉树

简介:

很基本的排序二叉树

 

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
int all;
struct node
{
    bool used;
    char name[35];
    int num,l,r;
};
node tree[10005];
char s[35];
int cnt;
void increase(int now)
{
    node &nn=tree[now];
    if(nn.used==0)
    {
        strcpy(nn.name,s);
        nn.used=1;
        nn.num++;
        return;
    }
    int c=strcmp(s,nn.name);
    if(c<0)
    {
        if(nn.l==0)nn.l=++cnt;
        increase(nn.l);
    }
    else if(c>0)
    {
        if(nn.r==0)nn.r=++cnt;
        increase(nn.r);
    }
    else nn.num++;
}
void output(int now)
{
    node &nn=tree[now];
    if(nn.used==0)return;
    output(nn.l);
    printf("%s %.4f\n",nn.name,(double)nn.num/all*100.0);
    output(nn.r);
    return;
}
int main()
{
    memset(tree,0,sizeof(tree));
    all=0;cnt=1;
    while(gets(s))
    {
        increase(1);
        all++;
    }
    output(1);
}


 

目录
相关文章
【剑指offer】-平衡二叉树-37/67
【剑指offer】-平衡二叉树-37/67
剑指offer 60. 平衡二叉树
剑指offer 60. 平衡二叉树
45 0
leetcode 之浅谈 N 叉树
之前去看 vue-router 源码的时候发现了 N 叉树的经典遍历框架,在源码中找到数据结构之类的算法,其实不算是很简单(对我来说)因为源码本身是嵌套嵌套再嵌套,无限套娃,有很多技术细节容
|
算法 前端开发 程序员
「LeetCode」94-二叉树的中序遍历⚡️
「LeetCode」94-二叉树的中序遍历⚡️
76 0
「LeetCode」94-二叉树的中序遍历⚡️
|
算法 前端开发 程序员
「LeetCode」剑指Offer-33二叉搜索树的后序遍历序列 ⚡️
「LeetCode」剑指Offer-33二叉搜索树的后序遍历序列 ⚡️
107 0
「LeetCode」剑指Offer-33二叉搜索树的后序遍历序列 ⚡️
|
算法 前端开发 程序员
「LeetCode」145-二叉树的后序遍历⚡️
「LeetCode」145-二叉树的后序遍历⚡️
101 0
「LeetCode」145-二叉树的后序遍历⚡️
|
机器学习/深度学习 算法 前端开发
「LeetCode」104-二叉树的最大深度⚡️
「LeetCode」104-二叉树的最大深度⚡️
262 0
「LeetCode」104-二叉树的最大深度⚡️
|
人机交互
POJ-2524,Ubiquitous Religions(并查集模板题)
POJ-2524,Ubiquitous Religions(并查集模板题)
[剑指offer] 平衡二叉树
本文首发于我的个人博客:尾尾部落 题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树。 解题思路 定义:平衡二叉查找树,简称平衡二叉树。 可以是空树。
923 0