using namespace std;
const int KIND = 26;
struct treeNode
{
int count;
treeNode *next[KIND];
treeNode()
{
count = 1;
for ( int i = 0; i < KIND; i++)
{
next[i] = NULL;
}
}
};
void (treeNode *&root, char *word)
{
treeNode *location = root;
int i = 0;
int branch = 0;
if (location == NULL)
{
location = new treeNode();
root = location;
}
while(word[i])
{
branch = word[i] - 'a';
if ( location->next[branch] )
{
location->next[branch]->count++;
} else
{
location->next[branch] = new treeNode();
}
i++;
location = location->next[branch];
}
}
int search(treeNode *root, char *word)
{
treeNode *location = root;
int i = 0;
int branch = 0;
int result;
if (location == NULL)
{
return 0;
}
while (word[i])
{
branch = word[i] - 'a';
if ( ! location->next[branch])
{
return 0;
}
i++;
location = location->next[branch];
result = location->count;
}
return result;
}
int main(int argc, char *argv[])
{
char word[10];
char result[10];
treeNode *root = NULL;
insert(root,"new");
insert(root,"new");
insert(root,"newword");
int res = search(root,"news");
cout << "get result:" << res << "n" << endl;
return 0;
}
下面这部分是带了一个show命令的
using namespace std;
const int KIND = 26;
const int MAX = 100;
class stack {
private:
unsigned int num;
int arr[MAX];
public:
stack(){
num = 0;
}
void clear(){
num = 0;
}
void push(const int number){
arr[num++] = number;
}
void pop(){
num--;
}
int top(){
return arr[num-1];
}
unsigned int size(){
return num;
}
bool empty(){
return (num == 0);
}
};
struct trieNode {
int number;
trieNode *next[KIND];
trieNode(){
number = 1;
for (int i = 0; i < KIND;i++){
next[i] = NULL;
}
}
};
void (trieNode *&root, char *word){
trieNode *localRoot = root;
int branch = 0;
if (localRoot == NULL){
localRoot = new trieNode();
root = localRoot;
}
int wordlen = strlen(word);
for ( int j = 0; j < wordlen;j++){
int branch = word[j] - 'a';
if ( localRoot->next[branch] != NULL){
localRoot->next[branch]->number++;
} else {
localRoot->next[branch] = new trieNode();
}
localRoot = localRoot->next[branch];
}
}
void showAll(trieNode *root,int level){
trieNode *localRoot = root;
stack *deepList = new stack();
level++;
if ( localRoot == NULL){
cout<<"the list is null, input new wordn"<<endl;
}
for ( int i = 0; i < KIND;i++){
if ( localRoot->next[i] != NULL ){
deepList->push(i);
}
}
if ( ! deepList->empty()){
while ( ! deepList->empty() ){
int child = deepList->top();
cout<<"at level "<<level<<" get a child " <<char(child + 'a')<<endl;
deepList->pop();
trieNode *childRoot = localRoot->next[child];
showAll(childRoot,level);
}
}
}
int search(trieNode *root, char *word){
trieNode *localRoot = root;
int search = 0;
int result;
if ( localRoot == NULL){
return 0;
} else {
int wordlen = strlen(word);
for ( int j = 0; j < wordlen; j++){
search = word[j] - 'a';
if ( localRoot->next[search] == NULL ){
return 0;
} else {
result = localRoot->next[search]->number;
}
localRoot = localRoot->next[search];
}
}
return result;
}
int main (int argc, char *argv[]){
trieNode *root = new trieNode();
char test1[10] = "bachelor";
char test2[10] = "badge";
char test3[10] = "baby";
char test4[10] = "try";
char check[10] = "baby";
insert(root,test1);
insert(root,test2);
insert(root,test3);
insert(root,test4);
showAll(root,0);
int number = search(root,check);
return 0;
}
近期评论