LeetCode Tries Prefix Tree

 1 class TrieNode {
 2 public:
 3     const static int NR_FANOUT = 26;
 4     TrieNode* child[NR_FANOUT];
 5     int count;
 6     // Initialize your data structure here.
 7     TrieNode() {
 8         for (int i=0; i<NR_FANOUT; i++) {
 9             child[i] = NULL;
10         }
11         count = 0;
12     }
13 };
14
15 class Trie {
16 public:
17     Trie() {
18         root = new TrieNode();
19     }
20
21     // Inserts a word into the trie.
22     void insert(string s) {
23         insert(s, 0, root);
24     }
25
26     // Returns if the word is in the trie.
27     bool search(string key) {
28         return search(key, 0, root) != NULL;
29     }
30
31     // Returns if there is any word in the trie
32     // that starts with the given prefix.
33     bool startsWith(string prefix) {
34         return search_prefix(prefix, 0, root) != NULL;
35     }
36 private:
37     TrieNode* insert(string& s, int pos, TrieNode* current) {
38         int len = s.size();
39         if (pos > len) {
40             return NULL;
41         }
42         if (current == NULL) {
43             current = new TrieNode();
44         }
45         if (pos == len) {
46             current->count++;
47             return current;
48         }
49         int idx = s[pos] - ‘a‘;
50         current->child[idx] = insert(s, pos + 1, current->child[idx]);
51         return current;
52     }
53
54     TrieNode* search(string& s, int pos, TrieNode* current) {
55         if (current == NULL) {
56             return NULL;
57         }
58         int len = s.size();
59         if (pos > len) {
60             return NULL;
61         } else if (pos == len && current->count) {
62             return current;
63         }
64         return search(s, pos + 1, current->child[s[pos] - ‘a‘]);
65     }
66     TrieNode* search_prefix(string& s, int pos, TrieNode* current) {
67         if (current == NULL) {
68             return NULL;
69         }
70         int len = s.size();
71         if (pos >= len) {
72             return current;
73         }
74         return search_prefix(s, pos + 1, current->child[s[pos] - ‘a‘]);
75     }
76 private:
77     TrieNode* root;
78 };

mplement a trie with insertsearch, and startsWith methods.

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

时间: 2024-08-11 17:09:04

LeetCode Tries Prefix Tree的相关文章

leetcode Implement Trie (Prefix Tree)

题目连接 https://leetcode.com/problems/implement-trie-prefix-tree/ Implement Trie (Prefix Tree) Description Implement a trie with insert, search, and startsWith methods. 字典树.. class TrieNode { public: // Initialize your data structure here. bool vis; Tri

[LeetCode]Implement Trie(Prefix Tree),解题报告

目录 目录 概述 Trie树基本实现 定义Trie树节点 添加操作 查询word是否在Trie树中 AC完整代码 概述 Trie树,又称为字典树.单词查找树或者前缀树,是一种用于快速检索的多叉数结构.例如,英文字母的字典树是26叉数,数字的字典树是10叉树. Trie树的基本性质有三点,归纳为: 根节点不包含字符,根节点外每一个节点都只包含一个字符. 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串. 每个节点的所有子节点包含的字符串不相同. Trie树基本实现 我们通过Lee

[LeetCode][JavaScript]Implement Trie (Prefix Tree)

Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. https://leetcode.com/problems/implement-trie-prefix-tree/ 实现字典树,每个节点至多有26个子孙,代表26个字母. 每个节点都有三个属性,key, isWord以及字典(哈希表,提高访问速度,也可以用数组),因为JS可以扩展实例化的对象,直接用下标访问对象,不需

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

LeetCode:Symmetric Tree - 判断二叉树是否对称

1.题目名称 Symmetric Tree(判断二叉树是否对称) 2.题目地址 https://leetcode.com/problems/symmetric-tree/ 3.题目内容 英文:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 中文:给定一颗二叉树,检查它是否与自己的镜像是同一棵树(即围绕根节点对称). 4.解题方法 本题与题目"Same Tr

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 分析:求二叉树的中序

LeetCode:Same Tree - 判断两颗树是否相等

1.题目名称 Same Tree(判断两棵树是否相等) 2.题目地址 https://leetcode.com/problems/same-tree/ 3.题目内容 英文:Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes h

LeetCode OJ - Binary Tree Level Order Traversal 1 &amp;&amp; 2

BFS以及它的扩展,我发现栈是个很好用的数据结构,特别是对于顺序需要颠倒的时候!!! 这里有个重要的信息:可以用null来标识一个level的结束!!! 下面是AC代码: 1 /** 2 * Given a binary tree, return the bottom-up level order traversal of its nodes' values. 3 * (ie, from left to right, level by level from leaf to root). 4 *