忘川睡着了zZ 2023-02-26 14:33 采纳率: 66.7%
浏览 15
已结题

我想知到申请的TrieNode数组的第index个位置是否为空,为空就创建结点,但是现在这样比较会报错,求说一下


#include <iostream>
#include <vector>
#include <string.h>
using namespace std;

class TrieNode;
class TrieNode
{
    public:
        int pass;
        int end;
        TrieNode* nexts;
        TrieNode()
        {
            pass=0;
            end=0;
            nexts=(TrieNode*)calloc(sizeof(TrieNode),26);
            }
};
class Trie
{
    public:
    TrieNode root;
    Trie()
    {
        root=*(new TrieNode);
    }
    void insert(string str)
    {
        if(str.size()==0)
        {
            return;
        } 
        TrieNode node=root;
        node.pass++;
        int index=0;
        for(int i = 0;i < str.size();i++) 
        {
            index=str[i]-'a';
            if(node.nexts[index]!=NULL)
            {
                
            }
            
        }
        
    }
};
int main(int argc, char** argv){
    string a ="abc";
    Trie t ;
  t.insert(a);
    return 0;
  }
  • 写回答

1条回答 默认 最新

  • GISer Liu 2023-02-26 15:04
    关注

    以下答案引用自GPT-3大模型,请合理使用:

    // 首先,你可以先在建立TrieNode时,将nexts改为vector<TrieNode*>
    // 然后,改写你的insert函数,代码可以参考如下:
    
    void insert(string str) {
        if (str.size() == 0)
            return;
        TrieNode node = root;
        node.pass++;
        int index = 0;
        for (int i = 0; i < str.size(); i++) {
            index = str[i] - 'a';
            if (node.nexts.size() <= index || node.nexts[index] == NULL) {    // 判断当前的位置是否为空,若为空则新建结点
                node.nexts[index] = new TrieNode();
            }
            node = node.nexts[index];
            node.pass++;
        }
        node.end++;
    }
    

    如果我的回答解决了您的问题,请采纳我的回答

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月7日
  • 已采纳回答 2月27日
  • 修改了问题 2月26日
  • 修改了问题 2月26日
  • 展开全部

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器