在 Java 中实现 Patricia Trie

Patricia Trie(或前缀树、基数树)是一种有序的结构化树,它通常利用其存储的数据特性来进行应用。树中节点的位置定义了与该节点关联的键,这使得 Trie(字典树)与二叉搜索树不同,后者节点存储的键仅对应于该节点本身。

每个节点都有一个作为字符串的前缀,而另一个则是空字符串。

Patricia trie 的通用操作包括:

  • 插入
  • 搜索
  • 删除

!image

方法思路:

  • 首先,我们简单地创建一个类 PatriciaTrieNode,并在其中声明该类的所有变量。
  • 现在,我们声明另一个类 INLINECODEba09baaa,并在其中构建 INLINECODE32878c73 构造函数。
  • 我们声明 INLINECODEd7e2b165 或 INLINECODE8a696a0e 等函数来检查节点的状态。
  • 我们将声明一个函数 bit,它将帮助我们存储节点中的元素。

– 我们首先检查其长度是否不等于最大位数。

– 然后我们编写代码以从左侧获取键 k 的第 i 位。

  • 现在,我们将编写一个布尔函数 search,它将帮助我们查找元素是否存在于节点中。

– 布尔搜索将接收一个数字,这将帮助我们搜索根节点。

PatriciaTrieNode search Node 将搜索给定的数据元素是否存在。

– 如果存在则返回 yes,否则返回 no。

  • PatriciaTrieNode search 是一个用于搜索元素的函数。

– 它将有两个元素:INLINECODEa6525f71(当前节点)和 INLINECODE47e9ebc1(下一个节点)。

– 下一个节点将保持为元素 INLINECODE6e06eb29 的左子节点,而当前节点是 INLINECODEc8d266dc。

– 借助 while 循环,我们将检查下一个节点是否大于当前节点。

– 如果满足条件,我们将检查当前节点是否等于下一个 ‘Node‘。

– 返回下一个节点。

  • 现在,我们将创建一个函数 insert PatriciaTrieNode

– 在这里,我们将声明 INLINECODEd8e24232、INLINECODE988f04f0、INLINECODEa173aeed、INLINECODEbeb4e440。

– 我们将相应地设置参数,如数据、左子节点和右子节点。

– 我们还将检查是否已经输入了相同的键。

– 如果尚未输入,我们将把键存储在不同的变量中。

– 在这里,我们将将其设置为数据、右子节点、左子节点等。

– 如果父节点匹配左子节点,则它是 NewNode,或者右子节点变为 NewNode。

  • 现在,我们将声明主类。

– 我们将声明扫描器。

– 我们还将为 PatriciaTest 创建一个对象。

– 我们将声明一个字符。

– 现在,我们将声明 switch 关键字。

– 可以使用字符访问此 switch 关键字。

– 我们可以选择插入、搜索、置空或检查是否为空。

– 我们可以根据满足 while 循环的给定输入继续循环。

实现:
案例 1

Patricia Trie
Patricia Trie Operations
1. Insert
2. Search
3. Check Empty
4. Make Empty
Make your choice
1
Enter element to insert
10
Do you want to continue (Type y or n)
y
Patricia Trie Operations
1. Insert
2. Search
3. Check Empty
4. Make Empty
Make your choice
1
Enter element to insert
20
Do you want to continue (Type y or n)
y
Patricia Trie Operations
1. Insert
2. Search
3. Check Empty
4. Make Empty
Make your choice
1
Enter element to insert
30
Do you want to continue (Type y or n)
y
Patricia Trie Operations
1. Insert
2. Search
3. Check Empty
4. Make Empty
Make your choice
1
Enter element to insert
10
Key already Present
Do you want to continue (Type y or n)
y
Patricia Trie Operations
1. Insert
2. Search
3. Check Empty
4. Make Empty
Make your choice
2
Enter element to search
20
Search result : true
Do you want to continue (Type y or n)
y
Patricia Trie Operations
1. Insert
2. Search
3. Check Empty
4. Make Empty
Make your choice
4
Patricia Trie Cleared
Do you want to continue (Type y or n)
n

案例 2

Patricia Trie
Patricia Trie Operations
1. Insert
2. Search
3. Check Empty
4. Make Empty
Make your choice
3
Empty status : true
Do you want to continue (Type y or n)
y
Patricia Trie Operations
1. Insert
2. Search
3. Check Empty
4. Make Empty
Make your choice
1
Enter element to insert
5
Do you want to continue (Type y or n)
y
Patricia Trie Operations
1. Insert
2. Search
3. Check Empty
4. Make Empty
Make your choice
1
Enter element to insert
10
Do you want to continue (Type y or n)
y
Patricia Trie Operations
1. Insert
2. Search
3. Check Empty
4. Make Empty
Make your choice
1
Enter element to insert
15
Do you want to continue (Type y or n)
y
Patricia Trie Operations
1. Insert
2. Search
3. Check Empty
4. Make Empty
Make your choice
2
Enter element to search
10
Search result : true
Do you want to continue (Type y or n)
n

示例

Java


// Java Program to implement Patricia trie

// Importing input output classes

import java.io.*;

// Importing Scanner class to

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。如需转载,请注明文章出处豆丁博客和来源网址。https://shluqu.cn/30956.html
点赞
0.00 平均评分 (0% 分数) - 0