是二叉查找樹入門
//============================================================================
// Name : hello.cpp// Author : key
// Version : 8
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
#define NUM_INF 0x7FFFFFFF
int sum;
struct Node{
char str[100];
int num;
Node* lNode;
Node* rNode;
Node(char* s)
{
lNode = rNode = NULL;
num = 1;
strcpy(str,s);
}
}*root;
Node* My_insert(Node* root_from,char* s)
{
if(NULL == root_from)
{
root_from = new Node(s);
return root_from;
}
int temp = strcmp(root_from->str,s);
if(temp==0)
{
(*root_from).num++;
return root_from;
}
if(temp > 0)
{
root_from->lNode = My_insert(root_from->lNode,s);
return root_from;
}
root_from->rNode = My_insert(root_from->rNode,s);
return root_from;
}
void dfs(Node* root_from)
{
if(NULL == root_from)
return;
dfs(root_from->lNode);
printf("%s %.4f\n", root_from->str , (*root_from).num*(100.0)/sum);
dfs(root_from->rNode);
}
int main()
{
char str_in[100];
sum = 0;
root = NULL;
while(gets(str_in))
{
root = My_insert(root,str_in);
sum++;
}
dfs(root);
return 0;
}