-
Notifications
You must be signed in to change notification settings - Fork 2
/
suffix.h
105 lines (105 loc) · 2.6 KB
/
suffix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef __SUFFIX_H_
#define __SUFFIX_H_
#include<iostream>
#include<map>
#include<vector>
using namespace std;
class SuffixTree
{
int Tk;
vector<int> concept;
map<int,int>Candlist;
map<int,SuffixTree*> child;
public:
SuffixTree(){Tk=10;}
SuffixTree(vector<int> v)
{
Tk=10;
concept=v;
}
void insert(map<vector<int>,int >v);
SuffixTree *findnode(vector<int> v,SuffixTree *t);
map<int,int> suggestions(vector<int> v);
void printTree();
};
void SuffixTree::insert(map<vector<int>,int > v)
{
int i,size,min,count;
map<vector<int>,int>::iterator iter;
map<int,int>::iterator it,t;
vector<int> temp,temp1;
SuffixTree *cn;
for(iter=v.begin();iter!=v.end();iter++)
{
temp=(*iter).first;
temp1=temp;
temp1.pop_back();
cn=findnode(temp1,this);
count=0;
min=99999999;
for(it=cn->Candlist.begin();it!=cn->Candlist.end();it++)
{
if((*it).second<min)
{
min=(*it).second;
t=it;
}
count++;
}
size=temp.size();
if((*iter).second>min || count<Tk)
cn->Candlist[temp[size-1]]=(*iter).second;
if(count>=Tk)
cn->Candlist.erase(t);
}
}
SuffixTree *SuffixTree::findnode(vector<int> v,SuffixTree *t)
{
if(!v.size())
return t;
vector<int> temp=v;
temp.erase(temp.begin());
SuffixTree *tree,*cn=NULL;
tree=findnode(temp,t);
if(tree!=NULL and tree->child.find(v[0])!=tree->child.end())
cn=tree->child[v[0]];
if(cn==NULL)
{
cn=new SuffixTree(v);
tree->child[v[0]]=cn;
}
return cn;
}
map<int,int> SuffixTree::suggestions(vector<int> v)
{
SuffixTree *temp;
temp=this;
vector<int>::iterator it;
for(it=v.end()-1;it>=v.begin();it--)
{
if(temp->child.find(*it)==temp->child.end())
break;
temp=temp->child[*it];
}
return temp->Candlist;
}
void SuffixTree::printTree()
{
if(this==NULL)
return;
vector<int>::iterator it;
map<int,int>::iterator iter;
map<int,SuffixTree*>::iterator it1;
cout<<"Concepts"<<endl;
cout<<"===================="<<endl;
for(it=this->concept.begin();it!=this->concept.end();it++)
cout<<(*it)<<" ";
cout<<endl;
cout<<"Candidates Concepts"<<endl;
cout<<"-------------------"<<endl;
for(iter=this->Candlist.begin();iter!=this->Candlist.end();iter++)
cout<<(*iter).first<<endl;
for(it1=this->child.begin();it1!=this->child.end();it1++)
(*it1).second->printTree();
}
#endif