返回搜索功能中的trie节点

时间:2017-01-25 05:45:08

标签: c trie

我正在使用trie编写程序。我的搜索函数返回一个整数,但我需要它返回一个trie节点。

public JsonResult Save(ExamModelList model)
    { 
        // Process Model Here 
        return Json(new {   Message = "response Message : Alhamdulillah worked "});
    }

但是当我想要它返回结构TrieNode时,它会返回错误的结果,如果它返回struct TrieNode *,则会出现运行时错误。

struct TrieNode{
   struct TrieNode *children[ALPHABET_SIZE];
   int repeat;
   int groupNumber;
   int groupName;
   int  isLeaf;
};

int CHAR_TO_INDEX(int c){
   if(c >= 97 && c <= 122){
       c -= 97;
   }
   else if(c >= 65 && c <= 90){
       c = c - 65 + 32;
   }
   else
       return -1;
   return c;
}

int search(struct TrieNode *root, const char *key,int ask)
{
   int level;
   int length = strlen(key);
   int index;
   struct TrieNode *pCrawl = root;

   for (level = 0; level < length; level++)
   {
       index = CHAR_TO_INDEX(key[level]);
       if(index == -1)
           continue;

       if (!pCrawl->children[index])
           return 0;

       pCrawl = pCrawl->children[index];
    }

    if(ask == 0)
        return (pCrawl->repeat)*(pCrawl != NULL && pCrawl->isLeaf);
    return (pCrawl->groupNumber)*(pCrawl != NULL && pCrawl->isLeaf);
}

struct TrieNode search(struct TrieNode *root, const char *key)
{
   int level;
   int length = strlen(key);
   int index;
   struct TrieNode *pCrawl = root;

   for (level = 0; level < length; level++)
   {
       index = CHAR_TO_INDEX(key[level]);
       if(index == -1)
           continue;

       if (!pCrawl->children[index])
           return ;

       pCrawl = pCrawl->children[index];
   }
   return *pCrawl;
}

0 个答案:

没有答案