前缀树md

This commit is contained in:
Wen 2022-09-01 11:44:00 +08:00
parent b7ce1362ba
commit 333b675349
2 changed files with 72 additions and 0 deletions

View File

@ -146,6 +146,77 @@ eg: [1,3,4,2,5]<br />
2字符放在边上节点上有专属的数据项常见的是pass和end值<br />
3样本添加方式每个字符串都从根节点开始加如果没有路就新建如果有路就复用<br />
4添加时沿途节点的pass值加1每个字符串结束时来到的节点end值加1<br />
```java
//将字符串插入前缀树中
public void insert(String str) {
if (str == null) {
return;
}
Node node = head;
node.pass++;
char[] chars = str.toCharArray();
for (char aChar : chars) {
int index = aChar - 'a';//在哪个节点上
if (node.next[index] == null) {
node.next[index] = new Node();
}
node.next[index].pass++;
node = node.next[index];
}
node.end++;
}
//str出现过几次
public Integer search(String str) {
if (str == null) {
return 0;
}
Node node = head;
char[] chars = str.toCharArray();
for (char aChar : chars) {
int index = aChar - 'a';//在哪个节点上
if (node.next[index] == null) {
return 0;
}
node = node.next[index];
}
return node.end;
}
public void delete(String str) {
if (search(str) > 0) {
Node node = head;
node.pass--;
char[] chars = str.toCharArray();
for (char aChar : chars) {
int index = aChar - 'a';//在哪个节点上
if (--node.next[index].pass == 0) {//pass为0后后面节点直接丢弃
node.next[index] = null;
return;
}
node = node.next[index];
}
node.end--;
}
}
//以prefix为前缀的字符串个数
public Integer prefixNumber(String prefix) {
if (prefix == null) {
return 0;
}
Node node = head;
char[] chars = prefix.toCharArray();
for (char aChar : chars) {
int index = aChar - 'a';//在哪个节点上
if (node.next[index] == null) {
return 0;
}
node = node.next[index];
}
return node.pass;
}
```
end值可查某个字符串出现几次,pass值可查以某个字符串为前缀的字符串出现过几次
### 单链表

View File

@ -1,6 +1,7 @@
package ds.tree;
/**
* 前缀树
* @author Wen
* @date 2022/8/22 22:26
*/