当前位置: 首页>>代码示例>>C++>>正文


C++ BSTree::countComparisonsToSearch方法代码示例

本文整理汇总了C++中BSTree::countComparisonsToSearch方法的典型用法代码示例。如果您正苦于以下问题:C++ BSTree::countComparisonsToSearch方法的具体用法?C++ BSTree::countComparisonsToSearch怎么用?C++ BSTree::countComparisonsToSearch使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BSTree的用法示例。


在下文中一共展示了BSTree::countComparisonsToSearch方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

// Analyze the ten thousand words
int main(int argc, char *argv[])
{
	if (argc != 2)
	{
		// Make sure the file to analyze exists
		cout << "Usage: " << argv[0] << " <filename>" << endl;
		return 0;
	}

	// Put all words into a BST and AVL
	string wordFiles[] = { "adjectives.txt", "nouns.txt", "verbs.txt", "adverbs.txt", "animals.txt" };
	string wordTypes[] = { "Adjective", "Noun", "Animal", "Adverb", "Verb" };
	int wordTypeSizes[] = { 0, 0, 0, 0, 0 };

	BSTree bst;
	AVLTree avl;

	for (int i = 0; i < 5; i++)
	{
		string wordFilePath = "./word_files/" + wordFiles[i];
		ifstream dictionaryFile(wordFilePath.c_str());

		if (!dictionaryFile.is_open())
		{
			cout << "Error: '" << wordFiles[i] << "' failed to open for reading." << endl;
			return 0;
		}

		string word;

		while (dictionaryFile >> word)
		{
			bst.insert(word);
			avl.insert(word);
			wordTypeSizes[i]++;
		}

		dictionaryFile.close();
	}

	// After putting everything to the BST and AVL, we load the crazy words and search them in the BST and AVL
	// counting the total comparisons made
	ifstream crazyWordsFile(argv[1]);

	if (!crazyWordsFile.is_open())
	{
		cout << "Error: '" << argv[1] << "' failed to open for reading." << endl;
		return 0;
	}

	int bstComparisons = 0;
	int avlComparisons = 0;
	string crazyWord;

	while (crazyWordsFile >> crazyWord)
	{
		bstComparisons += bst.countComparisonsToSearch(crazyWord);
		avlComparisons += avl.countComparisonsToSearch(crazyWord);
	}

	crazyWordsFile.close();

	// Print out the analysis to an output file
	ofstream analysisFile("analysis.out");

	analysisFile << "BST Comparisons = " << bstComparisons << endl;
	analysisFile << "AVL Comparisons = " << avlComparisons << endl;
	
	for (int i = 0; i < 5; i++)
		analysisFile << "Number of " << wordTypes[i] << " = " << wordTypeSizes[i] << endl;

	analysisFile.close();
}
开发者ID:kanibond,项目名称:3013-Algorithms-Oji,代码行数:74,代码来源:analyze_trees.cpp


注:本文中的BSTree::countComparisonsToSearch方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。