用有序二叉树来统计单词

今天上午看到一个题

本程序从正文文件text.in读入一篇英文短文,统计该短文中不同单词和它的出现次数,并按词典编辑顺序将单词及它的出现次数输出到正文文件word.out中.程序用一棵有序二叉树存储这些单词及其出现的次数,一边读入一边建立.然后中序遍历该二叉树,将遍历经过的二叉树上的节点的内容输出.

搞了一会,代码如下

#include <iostream>
#include <fstream>
#include <string>
#include <memory>

using namespace std;

class Tree
{
public:
	char word[100];
	int count;
	Tree *left;
	Tree *right;

	Tree()
	{
		memset(word, 0, sizeof(word));
		count = 0;
		left = NULL;
		right = NULL;
	}

	void in_order_visit(Tree *t, ofstream &word_out)
	{
		if (t)
		{
			t->in_order_visit(t->left, word_out);
			word_out<<t->word<<endl<<t->count<<endl;
			t->in_order_visit(t->right, word_out);
		}
	}

	void insert_word(char *word)
	{
		if (strlen(this->word) == 0)
		{
			strcpy(this->word, word);
			this->count = 1;
		}
		else
		{
			if (strcmp(word, this->word) < 0)
			{
				if ( ! this->left)
				{
					this->left = new Tree();
					this->left->insert_word(word);
				}
				else
				{
					if (strcmp(word, this->left->word) <= 0)
					{
						this->left->insert_word(word);
					}
					else
					{
						Tree *tmp = new Tree();
						strcpy(tmp->word, word);
						tmp->count = 1;
						tmp->left = this->left;
						this->left = tmp;
					}
				}
			}
			else if (strcmp(word, this->word) == 0)
			{
				this->count++;
			}
			else
			{
				if ( ! this->right)
				{
					this->right = new Tree();
					this->right->insert_word(word);
				}
				else
				{
					if (strcmp(word, this->right->word) >= 0)
					{
						this->right->insert_word(word);
					}
					else
					{
						Tree *tmp = new Tree();
						strcpy(tmp->word, word);
						tmp->count = 1;
						tmp->right = this->right;
						this->right = tmp;
					}
				}
			}
		}
	}
};

int get_text(const char *filename)
{
	FILE *fp = fopen(filename, "r");
	if ( ! fp)
	{
		return 1;
	}
	int in_word = 0;
	char tmp, word[100]={0};
	int word_append_count = 0;
	Tree *t = new Tree();
	while (fscanf(fp, "%c", &tmp) != EOF)
	{
		if ((tmp>='a' && tmp<='z') || (tmp>='A' && tmp<='Z') || tmp==''')
		{
			if (in_word)
			{
				word[word_append_count++] = tmp;
			}
			else
			{
				memset(word, 0, sizeof(word));
				word_append_count = 0;
				word[word_append_count++] = tmp;
				in_word = 1;
			}
		}
		else
		{
			if (strlen(word) > 0)
			{
				t->insert_word(word);
				memset(word, 0, sizeof(word));
				in_word = 0;
			}
		}
	}
	fclose(fp);

	ofstream word_out;
	word_out.open("word.out", ios::app);
	t->in_order_visit(t, word_out);
	word_out.close();
}

int main()
{
	get_text("text.in");
	return 0;
}

没有考虑内存释放,罪过罪过

输入的文本是从 google news 上面随便摘的一段

Chicago teachers take to the picket lines for the first time in 25 years in dispute over Mayor Rahm Emanuel's longer school day, job security and class size. WSJ's Caroline Porter and Douglas Belkin report. Photo: AP.
CHICAGO—Teachers in the third-largest U.S. school district went on strike Monday, halting classes for about 350,000 students and casting an election-season spotlight on a growing national debate over how best to evaluate teachers, set their pay and fire them.

Months of acrimonious contract negotiations between the Chicago Teachers Union and Mayor Rahm Emanuel stalled this weekend, apparently over Mr. Emanuel's push to make teacher evaluations more reliant on student performance, and union efforts to preserve job security for teachers who are laid off, two issues driving public school reform battles.

输出

AP
1
Belkin
1
CHICAGO
1
Caroline
1
Chicago
2
Douglas
1
Emanuel
1
Emanuel's
2
Mayor
2
Monday
1
Months
1
Mr
1
Photo
1
Porter
1
Rahm
2
S
1
Teachers
2
U
1
Union
1
WSJ's
1
a
1
about
1
acrimonious
1
an
1
and
6
apparently
1
are
1
battles
1
best
1
between
1
casting
1
class
1
classes
1
contract
1
day
1
debate
1
dispute
1
district
1
driving
1
efforts
1
election
1
evaluate
1
evaluations
1
fire
1
first
1
for
3
growing
1
halting
1
how
1
in
3
issues
1
job
2
laid
1
largest
1
lines
1
longer
1
make
1
more
1
national
1
negotiations
1
of
1
off
1
on
3
over
3
pay
1
performance
1
picket
1
preserve
1
public
1
push
1
reform
1
reliant
1
report
1
school
3
season
1
security
2
set
1
size
1
spotlight
1
stalled
1
strike
1
student
1
students
1
take
1
teacher
1
teachers
3
the
4
their
1
them
1
third
1
this
1
time
1
to
4
two
1
union
1
weekend
1
went
1
who
1
years
1

Leave a Reply

Your email address will not be published. Required fields are marked *