c++搜索树,利用多叉树的特性进行快速搜索和快速插入词条。
类似搜索引擎的词条搜索功能。
#include<iostream>
#include<windows.h>
#include<vector>
#include<string>
#include<cctype>
#include<conio.h>
using namespace std;
//搜索树节点
typedef struct SearchNode{
bool isWord;
string wordData;
vector<SearchNode*> LPChild;
SearchNode() : isWord(false) {}
SearchNode(string _wordData) : isWord(true),wordData(_wordData) {}
}STREE,*LPSTREE;
//搜索返回的结果
typedef struct SearchResult{
LPSTREE lpStree;
bool isFind;
int number;
SearchResult() : isFind(false),lpStree(NULL),number(0) {}
SearchResult(LPSTREE _lp,bool _f) : isFind(_f),lpStree(_lp),number(0) {}
}SRESULT;
//搜索树类
class SearchTree{
public:
STREE searchHead;
int maxWordLength;
SearchTree(){
maxWordLength=0;
}
void initSearchTree(){
for(int i=0;i<26;i++){
string ws;
ws.push_back('a');
ws[0]+=i;
addWordToSearchTree(ws,&searchHead);
}
}
//各单词以逗号隔开
void readDataFromFile(const char filePath[]){
FILE* fp;
if((fp=fopen(filePath,"r"))==NULL){
cout<<"文件打开出错"<<endl;
system("pause");
exit(0);
}
char ch=fgetc(fp);
string newWord;
while(ch!=EOF){
newWord.clear();
while(isalpha(ch)&&ch!=EOF){
newWord.push_back(ch);
ch=fgetc(fp);
}
if(maxWordLength<newWord.size()){
maxWordLength=newWord.size();
}
addWordToSearchTree(newWord,&this->searchHead);
if(ch!=EOF&&!isalpha(ch)) ch=fgetc(fp);
}
}
//对关键词查找并返回节点
SRESULT searchWordByKey(string word,LPSTREE lpSearchTree){
for(LPSTREE it : lpSearchTree->LPChild){
if(it->wordData==word){
return SRESULT(it,true);
}
if(it->wordData.size()<word.size()){
if(it->wordData==getStringInRange(word,0,it->wordData.size())){
return searchWordByKey(word,it);
}
}
}
return SRESULT(lpSearchTree,false);
}
//以起始位置和长度截取string中的字符
string getStringInRange(string word,int start,int length){
string newString;
for(int i=start;i<word.size()&&i<length+start;i++){
newString.push_back(word[i]);
}
return newString;
}
//向搜索树中添加关键词
void addWordToSearchTree(string word,LPSTREE lpSearchTree){
LPSTREE lpNode=searchWordByKey(word,lpSearchTree).lpStree;
lpNode->LPChild.push_back(new SearchNode(word));
}
//通过节点输出所有子数据
void showDataByNode(LPSTREE lpNode,SRESULT& res){
for(LPSTREE it : lpNode->LPChild){
cout<<it->wordData<<endl;
res.number++;
showDataByNode(it,res);
}
}
};
int main(){
SearchTree searchTree;
//初始化树的首字母:a,b,c,...,z
searchTree.initSearchTree();
//从文本文件中的单词作为词库添加进搜索树中
searchTree.readDataFromFile("E:\\C_WorkSpace\\Debug\\WORDS.txt");
//用户操作
string userInput;
SRESULT result,tempResult;
cout<<"搜索框:[ "<<userInput<<" ]"<<endl;
cout<<"请输入关键词进行搜索"<<endl;
while(true){
if(_kbhit()){
system("cls");
char key=getch();
if(key=='\b'&&userInput.size()>0) userInput.pop_back();
else if(isalpha(key))
userInput.push_back(key);
result=searchTree.searchWordByKey(userInput,&searchTree.searchHead);
if(result.isFind){
cout<<"搜索框:[ "<<userInput<<" ]"<<endl;
if(result.lpStree->wordData.size()>1){
cout<<"搜索结果:"<<endl<<result.lpStree->wordData<<endl;
result.number++;
}
else cout<<"搜索结果:"<<endl;
searchTree.showDataByNode(result.lpStree,result);
cout<<"词库中共有"<<result.number<<"条数据"<<endl;
tempResult=result;
}
else if(userInput.size()>0&&userInput.size()<=searchTree.maxWordLength){
cout<<"搜索框:[ "<<userInput<<" ]"<<endl;
if(tempResult.lpStree->wordData.size()>1){
cout<<"搜索结果:"<<endl<<tempResult.lpStree->wordData<<endl;
}
else cout<<"搜索结果:"<<endl;
searchTree.showDataByNode(tempResult.lpStree,result);
cout<<"词库中共有"<<tempResult.number<<"条数据"<<endl;
}
else{
cout<<"搜索框:[ "<<userInput<<" ]"<<endl;
cout<<"词库中暂未检索到您想要的结果"<<endl;
}
}
}
system("pause");
return 0;
}