以下语句在尝试执行中的作用是什么

标记

我一直在尝试通过博客工作实现特里数据结构插入的C ++实现,其中有些事情我无法理解http://theoryofprogramming.com/2015/01/16/trie-tree -执行/

#define ALPHABETS 26
#define CASE 'a'
#define MAX_WORD_SIZE 25
using namespace std;

struct Node
  {
   struct Node * parent;
   struct Node * children[ALPHABETS];
   vector<int> occurrences;
  };

  // Inserts a word 'text' into the Trie Tree
  // 'trieTree' and marks it's occurence as 'index'.

  void InsertWord(struct Node * trieTree, char  word[], int index)  
   {
   struct Node * traverse = trieTree;
   while (*word != '\0') { // Until there is something to process
    if (traverse->children[*word - CASE] == NULL) {

        // There is no node in 'trieTree' corresponding to this alphabet

        // Allocate using calloc(), so that components are initialised
      traverse->children[*word - CASE] = (struct Node *) calloc(1,  sizeof(struct Node));
        traverse->children[*word - CASE]->parent = traverse;  // Assigning parent
    }

    traverse = traverse->children[*word - CASE];
    ++word; // The next alphabet
}

  traverse->occurrences.push_back(index);      // Mark the occurence of the word
}

// Prints the 'trieTree' in a Pre-Order or a DFS manner
// which automatically results in a Lexicographical Order
void LexicographicalPrint(struct Node * trieTree, vector<char> word)
  {
  int i;
  bool noChild = true;
  if (trieTree->occurrences.size() != 0) {
    // Condition trie_tree->occurrences.size() != 0,
    // is a neccessary and sufficient condition to
    // tell if a node is associated with a word or not
    vector<char>::iterator charItr = word.begin();
    while (charItr != word.end()) {
        printf("%c", *charItr);
        ++charItr;
    }
    printf(" -> @ index -> ");

    vector<int>::iterator counter = trieTree->occurrences.begin();
    // This is to print the occurences of the word

    while (counter != trieTree->occurrences.end()) {
        printf("%d, ", *counter);
        ++counter;
    }

    printf("\n");
 }

 for (i = 0; i < ALPHABETS; ++i) {
    if (trieTree->children[i] != NULL) {
        noChild = false;
        word.push_back(CASE + i);   // Select a child

        // and explore everything associated with the cild
        LexicographicalPrint(trieTree->children[i], word);
        word.pop_back();
        // Remove the alphabet as we dealt
        // everything associated with it
     }
 }

  word.pop_back();
}

int main()
  {
   int n, i;
   vector<char> printUtil;       // Utility variable to print tree
   // Creating the Trie Tree using calloc
   // so that the components are initialised
   struct Node * trieTree = (struct Node *) calloc(1, sizeof(struct   Node));
  char word[MAX_WORD_SIZE];
  printf("Enter the number of words-\n");
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) {
    scanf("%s", word);
    InsertWord(trieTree, word, i);
  }

  printf("\n");   // Just to make the output more readable
  LexicographicalPrint(trieTree, printUtil);

 return 0;
}

我无法理解此语句的insertword作用:

     if (traverse->children[*word - CASE] == NULL)


同样,当我们在main函数中将所有元素初始化为1时,如何将其设置为null?

张ric

该函数InsertWord()动态地将一个新单词添加到该Trie中,并且在此过程中,只要该单词的前缀与该Trie中已添加的另一个单词的前缀不匹配,就会创建新节点。

这正是您的生产线正在测试的内容。从我所看到的,traverse是一个指向单词前缀的当前节点的指针。*word是前缀后面单词中的下一个字符。如果与k该单词的当前前缀相对应的节点没有一个子节点(指针为NULL),且其标签与下一个字符相对应,则意味着我们必须k+1为该单词的下一个前缀分配新的节点

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

gradle中NamedDomainObjectContainer的作用是什么?

来自分类Dev

该语句*(long *)0 = 0;的作用是什么?

来自分类Dev

JdbcDaoSupport的作用是什么?

来自分类Dev

IIS的作用是什么

来自分类Dev

在Clojure中,assocEx的作用是什么?

来自分类Dev

该命令的作用是什么?“执行bash -l”

来自分类Dev

IBuffer的作用是什么?

来自分类Dev

'\ n'的作用是什么?

来自分类Dev

OpenSSL中的引擎是什么,它的作用是什么?

来自分类Dev

edgeForExtendedLayout的作用是什么?

来自分类Dev

setTranslatesAutoresizingMaskIntoConstraints:NO的作用是什么?

来自分类Dev

在此特定的关闭语句中,=符号的作用是什么?

来自分类Dev

cudaDeviceReset()在Cuda中的作用是什么

来自分类Dev

mysqli准备语句中“ if”语句的作用是什么

来自分类Dev

在Julia中语法“ |>”的作用是什么?

来自分类Dev

我已经尝试在Microsoft Commos DB中执行以下NoSQL语句,但是它不起作用

来自分类Dev

MIPS中括号的作用是什么?

来自分类Dev

在此CASE语句中,THEN的作用是什么?

来自分类Dev

{:-9}在python中的作用是什么?

来自分类Dev

Angular中returnUrl的作用是什么

来自分类Dev

等效于nodejs中的以下代码。我想知道FirstorDefault方法的作用是什么?

来自分类Dev

gradle中NamedDomainObjectContainer的作用是什么?

来自分类Dev

在Mocha中describe()的作用是什么?

来自分类Dev

Spring中ResourceServlet的作用是什么?

来自分类Dev

$ {$}在PowerShell中的含义/作用是什么?

来自分类Dev

Java中的数学-“%”的作用是什么?

来自分类Dev

在Clojure中,assocEx的作用是什么?

来自分类Dev

awk命令中“ /”的作用是什么?

来自分类Dev

以下shell脚本的作用是什么?

Related 相关文章

热门标签

归档