leetcode笔记:Implement Trie (Prefix Tree)

本文介绍了如何实现一个字典树,包括插入、查找和前缀查找方法。题目要求所有输入仅包含小写字母,重点在于理解TrieNode的数据结构及其性质,以及如何用C++实现相关操作。
摘要由CSDN通过智能技术生成

一. 题目描述

Implement a trie with insert, search, and startsWith methods.

Note:

You may assume that all inputs are consist of lowercase letters a-z.

二. 题目分析

题目大意很简单,就是实现字典树,其中包括插入、查找和前缀查找三个方法。可以假设所有的输入只包含小写字母a-z。

本题考查字典树数据结构的基础知识。需要先定义TrieNode数据结构,TrieNode为字典树的节点,包含属性val、children和isWord。

其中val表示当前节点所存储的字符;children存储了当前节点的所有子节点的指针;isWord为布尔值,表示当前节点是否存储了一个单词。

Trie树的基本性质有以下几点:

  1. 根节点不包括字符,除根节点外,每个节点存储一个字符;
  2. 从根节点出发,经过一系列子节点,路径上经过的字符连接起来即为对应的字符串。

程序设计时,一个节点的子节点最多有26个,对应26个小写字母,因此简单用一个数组表示。

同时,在leetcode提供的函数基础上,增加了析构函数,因此涉及到new/delete操作。

三. 示例代码

class TrieNode {
public:
    // Initialize your data structure here.
    char val;
    bool isWord;
    TrieNode* children[26];
    // root node
    TrieNode(): val(0), isWord(false) {
        memset(children, 0x0, sizeof(TrieNode*) * 26);
    }
    // child node
    TrieNode(char c): val(c), isWord(false) {
        memset(children, 0x0, sizeof(TrieNode*) * 26);
    }
};

class Trie {
public:
    Trie() {
        root = new TrieNode();
    }

    // Inserts a word into the trie.
    void insert(string word) {
        int n = word.length();
        if (n == 0) return;
        TrieNode* p = root;
        for (int i = 0; i < n; ++i)
        {
            char c = word[i];
            if (p->children[c - 'a'] == 0)
                p->children[c - 'a'] = new TrieNode(c);

            p = p->children[c - 'a'];
        }
        p->isWord = true;
    }

    // Returns if the word is in the trie.
    bool search(string word) {
        int n = word.length();
        if (n == 0) return true;
        TrieNode *p = root;
        for (int i = 0; i < n; ++i)
        {
            char c = word[i];
            p = p->children[c - 'a'];
            if (p == NULL) return false;
        }
        return p->isWord;
    }

    // Returns if there is any word in the trie
    // that starts with the given prefix.
    bool startsWith(string prefix) {
        TrieNode *p = root;
        int n = prefix.length();
        if (n == 0) return true;
        for (int i = 0; i < n; ++i)
        {
            char c = prefix[i];
            p = p->children[c - 'a'];
            if (p == NULL) return false;
        }
        return true;
    }

    // Destructor
    ~Trie() {
        freeTrieNode(root);
    }

    void freeTrieNode(TrieNode *p){  
        if (p == NULL)  
            return;  
        for (int i = 0; i < 26; ++i)  
        {  
            TrieNode *pChild = p->children[i];  
            if (pChild != NULL)  
            {  
                freeTrieNode(pChild);  
            }  
        }  
        delete p;  
    }

private:
    TrieNode* root;
};

// Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");

四. 小结

巩固字典树数据结构。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值