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. 给一 ...
随机推荐
- IE浏览器中设为首页
<a onclick="this.style.behavior='url(#default#homepage)';this.setHomePage('<%=Configurati ...
- eclise -The method onClick(View) of type new View.OnClickListener(){} must override a superclass method
在做arcgis android开发的时候,突然遇到这种错误,The method onClick(View) of type new View.OnClickListener(){} must ov ...
- (转帖) 如何將值delay n個clock? (SOC) (Verilog)
来源:http://www.cnblogs.com/oomusou/archive/2009/06/15/verilog_dly_n_clk.html /* (C) OOMusou 2009 http ...
- C语言的运行机制
目的:通过分析c语言转换成汇编代码后的执行过程对汇编语言和X86构架有一个初步认识 实验代码 1 #include <stdio.h> 2 3 int g(int x) 4 { 5 ret ...
- AFN发送请求失败
发送请求出现这个错误 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Coc ...
- 通过dbcp链接池对数据库操作报 Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)--解决方案
org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for ...
- Wireshark抓包、过滤器
查阅于http://blog.sina.com.cn/s/blog_5d527ff00100dwph.html 1.捕捉过滤器 设置捕捉过滤器的步骤是:- 选择 capture -> optio ...
- Ubuntu安装google chrome过程
Ubuntu安装google chrome过程: # wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd6 ...
- POJ 2411.Mondriaan's Dream 解题报告
题意: 给出n*m (1≤n.m≤11)的方格棋盘,用1*2的长方形骨牌不重叠地覆盖这个棋盘,求覆盖满的方案数. Solution: 位运算+状态压缩+dp ...
- 请给出异步加载js方案
请给出异步加载js方案,不少于两种 默认情况javascript是同步加载的,也就是javascript的加载时阻塞的,后面的元素要等待javascript加载完毕后才能进行再加载,对于一些意义不是很 ...