suggestTree C++版本及测试

来源:互联网 发布:mac装win7好不好 编辑:程序博客网 时间:2024/06/10 17:42
// suggestTree.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "sugggestTree.h"#include <iostream>#include <string>#include <vector>using namespace std;int main(){SuggestTree lST(10);//input datamap<string, int> lmInput;lmInput.insert(make_pair("a", 1));lmInput.insert(make_pair("ab", 2));lmInput.insert(make_pair("ad", 3));lmInput.insert(make_pair("b", 1));lmInput.insert(make_pair("bb", 2));lmInput.insert(make_pair("ba", 2));lmInput.insert(make_pair("c", 1));lmInput.insert(make_pair("cb", 2));lmInput.insert(make_pair("ca", 2));lST.Build(lmInput);string input;vector<string> output;while(cin >> input){lST.getBestSuggesttions(input, output);for(int i = 0; i< output.size(); i++)cout << output[i] << endl;output.clear();}return 0;}


 

#ifndef SUGGESTTREE#define SUGGESTTREE#include <string>#include <vector>#include <map>#include <algorithm>using namespace std;class SuggestTree{private:typedef vector<pair<string, int> > vectorPairType;class Node{public:vector<string> list;unsigned int count;unsigned int end;Node* left, *mid, *right;Node(string& suggestion){list.resize(1);list[0] = suggestion;count  = 1;end = suggestion.length();left = mid = right = NULL;}Node(string& suggestion, vector<string>& a){list.clear();list.insert(list.begin(), a.begin(), a.end());list[0] = suggestion;count = 1;end = suggestion.length();left = mid = right = NULL;}Node(SuggestTree::Node& n){list.clear();list.insert(list.begin(), n.list.begin(), n.list.end());count = n.count;end = n.end;mid = n.mid;left = right = NULL;}string get(int index){return list[index];}int length(){return list.size();}};unsigned int k;Node* root;class ComparePairKey{public:bool operator()(const pair<string, int>& a, const pair<string, int>& b ){if( strcmp(a.first.c_str(), b.first.c_str()) >= 0)return true;elsereturn false;}};class ComparePairValue{public:bool operator()(const pair<string, int>& a, const pair<string, int>& b){return a.second < b.second;}};void hBuildTST(vectorPairType& irVP, int min, int max){if(min <= max){int mid = (min + max) / 2;insert(irVP[mid].first);hBuildTST(irVP, min, mid -1);hBuildTST(irVP, mid + 1, max);}}void insert(string& suggestion){if(root == NULL){root = new Node(suggestion);return;}Node* lpn = root;int i = 0;while(true){string s = lpn->list[0];if(s.at(i) > suggestion.at(i)){if(lpn->left == NULL){lpn->left = new Node(suggestion);return;}lpn = lpn->left;}else if(s.at(i) < suggestion.at(i)){if(lpn->right == NULL){lpn->right = new Node(suggestion);return;}lpn = lpn->right;}else{while( ++i < lpn->end){if(i == suggestion.length() || s.at(i) != suggestion.at(i)){lpn->mid = new Node(*lpn);lpn->end = i;break;}}lpn->count ++;if(i == suggestion.length())return;if(lpn->mid == NULL){lpn->mid = new Node(suggestion, lpn->list);return;}lpn = lpn->mid;}}}void addToList(string& suggestion){Node* lpn = root;int i = 0;while(true){string s = lpn->list[0];if(s.at(i) > suggestion.at(i))lpn = lpn->left;else if(s.at(i) < suggestion.at(i))lpn = lpn->right;else{if(lpn->count > lpn->list.size()){lpn->list.resize(min(lpn->count, k));lpn->list[0] = suggestion;lpn->count = 1;}else if(lpn-> count < lpn->list.size()){lpn->list[lpn->count++] = suggestion;}i = lpn->end;if(i == suggestion.length())return;lpn = lpn->mid;}}}Node* hgetBestSuggesttions(string& prefix){if(prefix.length() == 0)            return NULL;        Node* lpn = root;        int i = 0;        while(lpn != NULL) {            string s = lpn->list[0];            if(s.at(i) > prefix.at(i))                lpn = lpn->left;            else if(s.at(i) < prefix.at(i))                lpn = lpn->right;            else{                while(++i < lpn->end)                    if(i == prefix.length())                        return lpn;                    else if(s.at(i) != prefix.at(i))                        return NULL;                if(i == prefix.length())                    return lpn;                lpn = lpn->mid;            }        }}public:SuggestTree(int ik){k = ik;}void Build(map<string, int>& iMap){root = NULL;vectorPairType lMapVector;lMapVector.insert(lMapVector.begin(), iMap.begin(), iMap.end());//sort lMapVector by pair->key//for the balance of TST treesort(lMapVector.begin(), lMapVector.end(), ComparePairKey());hBuildTST(lMapVector, 0, lMapVector.size() - 1);//sort lMapVector by pair->valuesort(lMapVector.begin(), lMapVector.end(), ComparePairValue());vectorPairType::iterator ivter = lMapVector.begin();for(; ivter != lMapVector.end(); ivter ++){addToList(ivter->first);}}void getBestSuggesttions(string& prefix, vector<string>& output){Node* lpNode = hgetBestSuggesttions(prefix);if(lpNode && lpNode->list.size() != 0)output.insert(output.begin(), lpNode->list.begin(), lpNode->list.end());}};#endif


 

原创粉丝点击