TRIE树

一个能够实现查找元素、增加元素和删除元素三种操作的数据结构称为字典。TRIE树,又称键树,可以用来构造字典,适合所有元素都是由字母和数字标记的情形。下面是TRIE树的一种C++实现:

#include <stdio.h>
#include <iostream>
#include <string.h>

using namespace std;
const int num_chars = 26;

class Trie {
  protected:
    struct Trie_node {
      char* word;
      Trie_node* branch[num_chars];
      Trie_node();
    };
    Trie_node* root;

  public:
    Trie();
    int search(const char* word) const;
    int insert(const char* word);
    int remove(const char* word);

    void printall(char *pre, Trie_node *p);
    void printpre(char *pre);
};

Trie::Trie():root(NULL)
{
}

Trie::Trie_node::Trie_node()
{
  word = NULL;
  for (int i=0; i<num_chars; ++i)
    branch[i] = NULL;
}

int Trie::search(const char* word) const
{
  char char_code;
  Trie_node *location = root;

  while( location!=NULL && *word!=0 ) {
    if (*word>='A' && *word<='Z')
      char_code = *word-'A';
    else if (*word>='a' && *word<='z')
      char_code = *word-'a';
    else
      return 0;

    location = location->branch[char_code];
    word++;
  }
  if ( location != NULL && location->word != NULL ) {
    return 1;
  } else
    return 0;
}

int Trie::insert(const char* word)
{
  int result = 1;
  if ( root == NULL ) root = new Trie_node;
  char char_code;
  Trie_node *location = root;

  while( location!=NULL && *word!=0) {
    if (*word>='A' && *word<='Z')
      char_code = *word-'A';
    else if (*word>='a' && *word<='z')
      char_code = *word-'a';
    else
      return 0;

    if( location->branch[char_code] == NULL )
       location->branch[char_code] = new Trie_node;
    location = location->branch[char_code];
    word++;
  }
  if (location->word != NULL)
    result = 0;
  else {
    location->word = new char[strlen(word)+1];
    strcpy(location->word, word);
  }
  return result;
}

int Trie::remove(const char* word)
{
  int result = 1;
  char char_code;
  Trie_node *location = root;

  while( location!=NULL && *word!=0) {
    if (*word>='A' && *word<='Z')
      char_code = *word-'A';
    else if (*word>='a' && *word<='z')
      char_code = *word-'a';
    else
      return 0;

    location = location->branch[char_code];
    word++;
  }
  if (location) {
    delete location->word;
    location->word = NULL;
  } else {
    result = 0;
  }
  return result;
}

void Trie::printall(char *pre, Trie_node *p)
{
  if (p->word != NULL) {
      cout<<pre<<endl;
  }

  for (int i=0; i<num_chars; ++i) {
    if (p->branch[i] != NULL) {
      char ch = 'a' + i;
      char *prestr = (char *)malloc(strlen(pre)+2);
      sprintf(prestr, "%s%c", pre, ch);
      printall(prestr, p->branch[i]);
      free(prestr);
    }
  }
}

void Trie::printpre(char *pre)
{
    char char_code;
    char *p = pre;
    Trie_node *location = root;

    while( location!=NULL && *pre!=0 ) {
      if (*pre>='A' && *pre<='Z')
        char_code = *pre-'A';
      else if (*pre>='a' && *pre<='z')
        char_code = *pre-'a';
      else
        return;

      location = location->branch[char_code];
      pre++;
     }

     if (location != NULL)
        printall(p, location);

     return;
}

int main(int argc, char *argv[])
{
  Trie t;

  t.insert("liu");
  t.insert("aigui");
  t.insert("liuag");
  t.insert("china");
  t.insert("trie");

  t.remove("aigui");
  t.remove("abc");
  t.printpre("");

  if (t.search(argv[1]))
    cout<<argv[1]<<" was found"<<endl;
  else
    cout<<argv[1]<<" was not found"<<endl;

  if (argv[2])
    t.printpre(argv[2]);

  return 0;
}

 (刘爱贵 / Aiguille LIU)

你可能感兴趣的:(数据结构,null,search,insert,iostream,branch)