Ternary Search Tree Java实现
/**
* @author Edwin Chen
*
*/ //定义节点
class Node {
//存储字符串
char storeChar;
//是否完成单词
boolean isComplete; Node leftChild,centerChild,rightChild; //构造方法
public Node(char storeChar,boolean isComplete) {
this.storeChar = storeChar;
this.isComplete = isComplete;
}
} public class TernarySearchTree {
//根节点
public Node root; //存储结果
HashSet<String> result = new HashSet<String>(); //递归创建tree
public Node insert(String word,Node node,Integer index) {
if(word == null || "".equals(word))
return null; //将word转成char数组
char[] charArray = word.toCharArray(); //递归终止条件,当没有改字符时,创建新节点
if(node == null) {
node = new Node(charArray[index],false);
} if(charArray[index] < node.storeChar) {
node.leftChild = this.insert(word, node.leftChild,index);
} else if(charArray[index] > node.storeChar) {
node.rightChild = this.insert(word, node.rightChild,index);
} else {
//如果为word最后一个字符,那么设置为单词完结,如为最后一个字符,必定进入这一步
if(index + 1 == charArray.length) {
node.isComplete = true;
} else {
node.centerChild = this.insert(word, node.centerChild,++index);
}
} return node;
} //封装
public void insert(String word) {
root = this.insert(word,root,0);
} public String toString()
{
traverse(root, "");
return "\nTernary Search Tree : "+ result;
}
//遍历
private void traverse(Node node, String str)
{
if (node != null)
{
traverse(node.leftChild, str); str = str + node.storeChar;
if (node.isComplete)
result.add(str); traverse(node.centerChild, str);
str = str.substring(0, str.length() - 1); traverse(node.rightChild, str);
}
} public boolean search(String word)
{
return search(root, word.toCharArray(), 0);
} private boolean search(Node node, char[] word, int index)
{
if (node == null)
return false; if (word[index] < node.storeChar)
return search(node.leftChild, word, index);
else if (word[index] > node.storeChar)
return search(node.rightChild, word, index);
else
{
if (node.isComplete && index == word.length - 1)
return true;
else if (index == word.length - 1)
return false;
else
return search(node.centerChild, word, index + 1);
}
} public Node findNode(String prefix) {
return findNode(root,prefix.toCharArray(),0);
} public Node findNode(Node node, char[] word, int index) {
if (node == null)
return null; if (word[index] < node.storeChar)
return findNode(node.leftChild, word, index);
else if (word[index] > node.storeChar)
return findNode(node.rightChild, word, index);
else
{
if (index == word.length - 1)
return node.centerChild;
else
return findNode(node.centerChild, word, index + 1);
}
} //查找前缀相同的word
public HashSet<String> prefixSearch(String prefix,Node node) {
if(node != null) {
if(node.isComplete) {
result.add(prefix + node.storeChar);
} prefixSearch(prefix,node.leftChild);
prefixSearch(prefix + node.storeChar,node.centerChild);
prefixSearch(prefix,node.rightChild);
} if(search(prefix))
result.add(prefix); return result;
} public HashSet<String> prefixSearch(String prefix) {
Node node = findNode(prefix);
return prefixSearch(prefix,node);
} public static void main(String[] args) {
TernarySearchTree t = new TernarySearchTree();
t.insert("ab");
t.insert("abba");
t.insert("abcd");
t.insert("bcd"); HashSet<String> a = t.prefixSearch("ab");
for(String s : a) {
System.out.println(s);
} System.out.println(t);
}
}
Ternary Search Tree Java实现的更多相关文章
- 数据结构《17》---- 自动补齐之《二》----Ternary Search Tree
一. 序言 上一篇文章中,给出了 trie 树的一个实现.可以看到,trie 树有一个巨大的弊病,内存占用过大. 本文给出另一种数据结构来解决上述问题---- Ternary Search Tree ...
- Ternary Search Tree 应用--搜索框智能提示
前面介绍了Ternary Search Tree和它的实现,那么可以用Ternary Search Tree来实现搜索框的只能提示,因为Ternary Search Tree的前缀匹配效率是非常高的, ...
- 数据结构《17》---- 自己主动补齐之《二》----Ternary Search Tree
一. 序言 上一篇文章中,给出了 trie 树的一个实现. 能够看到,trie 树有一个巨大的弊病,内存占用过大. 本文给出还有一种数据结构来解决上述问题---- Ternary Search Tre ...
- 数据结构之Binary Search Tree (Java)
二叉查找树简介 二叉查找树(Binary Search Tree), 也成二叉搜索树.有序二叉树(ordered binary tree).排序二叉树(sorted binary tree), 是指一 ...
- leetcode 99 Recover Binary Search Tree ----- java
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- leetcode 98 Validate Binary Search Tree ----- java
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- Trie和Ternary Search Tree介绍
Trie树 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie树与二叉搜索树不同,键不是直接保存在节 ...
- leetcode 109 Convert Sorted List to Binary Search Tree ----- java
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- leetcode 108 Convert Sorted Array to Binary Search Tree ----- java
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 给一 ...
随机推荐
- Objective-C学习篇05—Foundation框架简介
iOS中所谓的框架,说到底就是一个目录,iOS提供了很多我们可以在应用程序中调用的框架.许多应用程序都使用了如Foundation.UIKit和Core Graphics这些框架.根据你为应用程序选择 ...
- 【USACO 2.2.4】派对灯
[描述] 在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码. 这些灯都连接到四个按钮: 按钮1:当按下此按钮,将改变所有的灯:本来亮着的灯就熄 ...
- jquery元素查找方法
$("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $("div&q ...
- MYSQL一对多,两表查询合并数据
select a.askid,a.title,GROUP_CONCAT(b.message SEPARATOR '----') as content from gg_ask as a join gg_ ...
- MYSQL管理之主从同步管理 转载
MYSQL主从同步架构是目前使用最多的数据库架构之一,尤其是负载比较大的网站,因此对于主从同步的管理也就显得非常重要,新手往往在出现主从同步错误的时候不知道如何入手,这篇文章就是根据自己的经验来详细叙 ...
- Pyhon + Django 1.7.2 tutorial + virtualenv简单使用
最近工作中要用到python,先前没怎么接触过,把python本身的语法以及特性撸过一边之后,这两天按着django官方的文档倒腾了几天, 文档非常详细,本人英语水平也就那样,大体没什么压力,建议像我 ...
- 【Maven实战】archetype的使用和eclipse的配置
1.之前在进行项目的构建时都是使用手工进行文件夹的建立,maven也给我们提供了一个参数archetype,可以用来进行项目骨架的建立.使用maven archetype:generate进行创建: ...
- cloudstack的ZONE删除不掉?来这招吧
老是提示什么物理网络被使用之类的, 可以进数据库去直接删除. I want to delete a zone,by this way:(1)delete all VMs in zone.(2)dele ...
- CLOUDSTACK我也来啦
最近,专业客户需要私有云和自助管理. 那我就倒一个CLOUDSTACK出来吧. WEB UI已搞定. 难点在于高级网络和主存储.再接再力. ... 因为要反复重装,在服务端有个快速重建步骤: serv ...
- 配置Session变量的生命周期
在Web.config文件中配置Session变量的生命周期是在<sessionState></sessionState>节中完成的,在配置Session的生命周期时,可以设置 ...