Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

148 lines
4.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
id: 587d8259367417b2b2512c84
title: 创建Trie搜索树
challengeType: 1
videoUrl: ''
dashedName: create-a-trie-search-tree
---
# --description--
在这里我们将继续从二叉搜索树开始看看另一种称为trie的树结构。 trie是一种常用于保存字符串的有序搜索树或者更通用的关联数组或其中键是字符串的动态数据集。当许多键具有重叠前缀时它们非常擅长存储数据集例如字典中的所有单词。与二叉树不同节点不与实际值相关联。相反节点的路径表示特定的键。例如如果我们想将字符串代码存储在trie中我们将有四个节点每个字母对应一个节点c - o - d - e。然后通过所有这些节点的路径将创建代码作为字符串 - 该路径是我们存储的密钥。然后如果我们想要添加字符串编码它将在d之后分支之前共享前三个代码节点。通过这种方式可以非常紧凑地存储大型数据集。此外搜索可以非常快因为它实际上限于您存储的字符串的长度。此外与二叉树不同节点可以存储任意数量的子节点。正如您可能从上面的示例中猜到的那样一些元数据通常存储在保存密钥结尾的节点上以便在以后的遍历中仍可以检索密钥。例如如果我们在上面的示例中添加了代码我们需要某种方式来知道代码中的e代表先前输入的密钥的结尾。否则当我们添加代码时这些信息将会丢失。说明让我们创建一个存储单词的trie。它将通过add方法接受单词并将它们存储在trie数据结构中。它还允许我们查询给定字符串是否是带有isWord方法的单词并使用print方法检索输入到trie中的所有单词。 isWord应该返回一个布尔值print应该将所有这些单词的数组作为字符串值返回。为了让我们验证这个数据结构是否正确实现我们为树中的每个节点提供了一个Node结构。每个节点都是一个具有keys属性的对象该属性是JavaScript Map对象。这将保存作为每个节点的有效密钥的各个字母。我们还在节点上创建了一个end属性如果节点表示单词的终止则可以将其设置为true。
# --hints--
Trie有一个add方法。
```js
assert(
(function testTrie() {
var test = false;
if (typeof Trie !== 'undefined') {
test = new Trie();
} else {
return false;
}
return typeof test.add == 'function';
})()
);
```
Trie有一种打印方法。
```js
assert(
(function testTrie() {
var test = false;
if (typeof Trie !== 'undefined') {
test = new Trie();
} else {
return false;
}
return typeof test.print == 'function';
})()
);
```
Trie有一个isWord方法。
```js
assert(
(function testTrie() {
var test = false;
if (typeof Trie !== 'undefined') {
test = new Trie();
} else {
return false;
}
return typeof test.isWord == 'function';
})()
);
```
print方法将添加到trie的所有项目作为数组中的字符串返回。
```js
assert(
(function testTrie() {
var test = false;
if (typeof Trie !== 'undefined') {
test = new Trie();
} else {
return false;
}
test.add('jump');
test.add('jumps');
test.add('jumped');
test.add('house');
test.add('mouse');
var added = test.print();
return (
added.indexOf('jump') != -1 &&
added.indexOf('jumps') != -1 &&
added.indexOf('jumped') != -1 &&
added.indexOf('house') != -1 &&
added.indexOf('mouse') != -1 &&
added.length == 5
);
})()
);
```
isWord方法仅对添加到trie的单词返回true对所有其他单词返回false。
```js
assert(
(function testTrie() {
var test = false;
if (typeof Trie !== 'undefined') {
test = new Trie();
} else {
return false;
}
test.add('hop');
test.add('hops');
test.add('hopped');
test.add('hoppy');
test.add('hope');
return (
test.isWord('hop') &&
!test.isWord('ho') &&
test.isWord('hopped') &&
!test.isWord('hopp') &&
test.isWord('hoppy') &&
!test.isWord('hoping')
);
})()
);
```
# --seed--
## --seed-contents--
```js
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
var Node = function() {
this.keys = new Map();
this.end = false;
this.setEnd = function() {
this.end = true;
};
this.isEnd = function() {
return this.end;
};
};
var Trie = function() {
// Only change code below this line
// Only change code above this line
};
```
# --solutions--
```js
// solution required
```