CSuffixTree(先弄个头上去^_^|||)
CreatTree:通过两层循环查找目标字符串的每一个前缀的所有后缀,
通过调用Search方法将结果分为四种。通过switch分别处理四种情况。
Search:搜索函数,通过遍历搜索参数传入的字符串。
将搜索结果通过point指针及其指向的节点的breakpoint变量,left字符串,
以及函数返回值影响CreatTree的处理方法。
************************************/
#include <vector>
#include <iostream>
#include <string>
#include <stack>
#include <deque>
using namespace std;
typedef struct node//声明节点的结构
{
string strdata;//存储节点上的字符串
vector<node*> Child;//存储该节点的子节点的地址
int flag;//辅助标志位,用0和1表示该节点是否有子节点
int breakpoint;//副主变量,当该节点需要分裂时,用于记录分裂点的位置
}*mynode;
class CSuffixTree
{
private:
string data,left;//data源字符串变量,在构造函数中初始化
//left用于记录每次搜索结束后,目标字符串中的剩余字符串
public:
mynode ST,point;//ST生成的后缀树的根节点
//point节点指针,搜索时指向搜索节点的父节点,搜索结束时根据搜索//结果指向要操作的节点
CSuffixTree(string str);
int Search(string str);
void CreatTree();
void Show(mynode ST);
void PrintNode(mynode p, int c, vector<bool>& isend);
~CSuffixTree(void);
};
CSuffixTree::CSuffixTree(string str)
//构造函数,初始化data变量和ST,point指针并产个根节
//点的第一个子节点,ST的flag置1
{
data=str;
ST=(mynode) new node;
point=(mynode) new node;
point->strdata = data[0];
point->flag = 0;
ST->Child.push_back(point);
ST->flag=1;
}
CSuffixTree::~CSuffixTree(void)//析构函数
{
}
浙公网安备 33010602011771号