实现哈希树的Java程序
特里不是 CS 学生在大学里可能会花那么多时间做的事情,但它对于面试来说真的非常重要。 trie 是一种数据结构,实际上是一种树,但它通常用于存储关联数组或动态集,其中键通常是字符或字符串,它在树中的位置定义了与之关联的键.它的工作方式是,一个节点的每个后继节点都有一个与该节点相关联的字符串的公共前缀,这意味着每个节点可能存储一个字符作为其数据,但是如果我们查看从根向下到那个节点,那个音符实际上代表一个词或一个词的一部分,所以我们可以做的是非常快速地查找特定种类的词或字符。
Java代码:
- 导入所需模块:
Java
import java.io.*;
import java.util.*;
Java
class TrieHash {
// implementing a HashMap
private HashMap origin;
// implementing a zero-argument constructor
public TrieHash()
{
// creating a new HashMap
origin = new HashMap();
}
// implementing another constructor
// with an array as a parameter
public TrieHash(String[] array)
{
origin = new HashMap();
// attaching that array string in the trie
for (String c : array)
attach(c);
}
// attach function to add character to the trie
public void attach(String str)
{
HashMap node = origin;
int i = 0;
while (i < str.length()) {
// if node already contains that key,
// we will simply point that node
if (node.containsKey(str.charAt(i))) {
node = node.get(str.charAt(i));
}
else {
// else we will make a new hashmap with
// that character and then point it.
node.put(str.charAt(i),
new HashMap());
node = node.get(str.charAt(i));
}
i++;
}
// putting 0 to end the string
node.put('\0', new HashMap(0));
}
// function to search for the specific
// string in the hash trie
public boolean search(String str)
{
HashMap presentNode = origin;
int i = 0;
while (i < str.length()) {
// will search for that character if it exists
if (presentNode.containsKey(str.charAt(i))) {
presentNode
= presentNode.get(str.charAt(i));
}
else {
// if the character does not exist
// that simply means the whole string
// will also not exists, so we will
// return false if we find a character
// which is not found in the hash trie
return false;
}
i++;
}
// this will check for the end string,
// and if the whole string is found,
// it will return true else false
if (presentNode.containsKey('\0')) {
return true;
}
else {
return false;
}
}
}
Java
public class Main {
// unreported exception IOException must be caught or
// declared to be thrown
public static void main(String[] args)
throws IOException
{
BufferedReader br
= new BufferedReader(new InputStreamReader(
System.in)); // this will accepts the words
// for the hash trie
System.out.println(
"Enter the words separated with space to be entered into trie");
// will read the line which user will
// provide with space separated words
String input = br.readLine();
// it will split all the words
// and store them in the string array
String[] c = input.split(" ");
// now we will use constructor with string array as
// parameter and we will pass the user entered
// string in that constructor to construct the hash
// trie
TrieHash trie = new TrieHash(c);
System.out.println(
"\nEnter the word one by one to be searched in hash-trie");
String word = br.readLine();
// this will search for the word in out trie
if (trie.search(word)) {
System.out.println("Word Found in the trie");
}
else {
System.out.println(
"Word NOT Found in the trie!");
}
}
}
Java
import java.io.*;
import java.util.*;
class TrieHash {
// implementing a HashMap
private HashMap origin;
// implementing a zero-argument constructor
public TrieHash()
{
// creating a new HashMap
origin = new HashMap();
}
// implementing another constructor
// with an array as a parameter
public TrieHash(String[] array)
{
origin = new HashMap();
// attaching that array string in the trie
for (String c : array)
attach(c);
}
// attach function to add
// character to the trie
public void attach(String str)
{
HashMap node = origin;
int i = 0;
while (i < str.length()) {
// if node already contains thatkey,
// we will simply point that node
if (node.containsKey(str.charAt(i))) {
node = node.get(str.charAt(i));
}
else {
// else we will make a new hashmap with
// that character and then point it.
node.put(str.charAt(i),
new HashMap());
node = node.get(str.charAt(i));
}
i++;
}
// putting 0 to end the string
node.put('\0', new HashMap(0));
}
// function to search for the
// specific string in the hash trie
public boolean search(String str)
{
HashMap presentNode = origin;
int i = 0;
while (i < str.length()) {
// will search for that character
// if it exists
if (presentNode.containsKey(str.charAt(i))) {
presentNode
= presentNode.get(str.charAt(i));
}
else {
// if the character does notexist that
// simply means the whole string will
// also not exists, so we will return
// false if we find a character which
// is not found in the hash trie
return false;
}
i++;
}
// this will check for the end string,
// and if the whole string is found,
// it will return true else false
if (presentNode.containsKey('\0')) {
return true;
}
else {
return false;
}
}
}
public class Main {
// unreported exception IOException
// must be caught or declared to be thrown
public static void main(String[] args)
throws IOException
{
// this will accepts the words for the hash trie
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println(
"Enter the words separated with space to be entered into trie");
// will read the line which user will
// provide with space separated words
String input = br.readLine();
// it will split all the words and
// store them in the string array
String[] c = input.split(" ");
// now we will use constructor with string
// array as parameter and we will pass the
// user entered string in that constructor
// to construct the hash trie
TrieHash trie = new TrieHash(c);
System.out.println(
"How many words you have to search in the trie");
String count = br.readLine();
int counts = Integer.parseInt(count);
for (int i = 0; i < counts; i++) {
System.out.println(
"\nEnter the word one by one to be searched in hash-trie ");
String word = br.readLine();
// this will search for the word in out trie
if (trie.search(word)) {
System.out.println(
"Word Found in the trie");
}
else {
System.out.println(
"Word NOT Found in the trie!");
}
}
}
}
- 现在,我们将创建一个类TrieHash ,我们将在其中实现一个 HashMap,它还将包含两个带有零和单个数组参数的构造函数,一个向哈希树添加字符的函数和一个用于在哈希树中搜索特定字符串的函数.
Java
class TrieHash {
// implementing a HashMap
private HashMap origin;
// implementing a zero-argument constructor
public TrieHash()
{
// creating a new HashMap
origin = new HashMap();
}
// implementing another constructor
// with an array as a parameter
public TrieHash(String[] array)
{
origin = new HashMap();
// attaching that array string in the trie
for (String c : array)
attach(c);
}
// attach function to add character to the trie
public void attach(String str)
{
HashMap node = origin;
int i = 0;
while (i < str.length()) {
// if node already contains that key,
// we will simply point that node
if (node.containsKey(str.charAt(i))) {
node = node.get(str.charAt(i));
}
else {
// else we will make a new hashmap with
// that character and then point it.
node.put(str.charAt(i),
new HashMap());
node = node.get(str.charAt(i));
}
i++;
}
// putting 0 to end the string
node.put('\0', new HashMap(0));
}
// function to search for the specific
// string in the hash trie
public boolean search(String str)
{
HashMap presentNode = origin;
int i = 0;
while (i < str.length()) {
// will search for that character if it exists
if (presentNode.containsKey(str.charAt(i))) {
presentNode
= presentNode.get(str.charAt(i));
}
else {
// if the character does not exist
// that simply means the whole string
// will also not exists, so we will
// return false if we find a character
// which is not found in the hash trie
return false;
}
i++;
}
// this will check for the end string,
// and if the whole string is found,
// it will return true else false
if (presentNode.containsKey('\0')) {
return true;
}
else {
return false;
}
}
}
现在所有必需的功能都已编码,现在我们将使用用户输入测试这些功能。为此,我们将再次创建一个新类来测试我们的哈希 trie,它将提示用户输入要搜索的单词和要搜索的单词。
Java
public class Main {
// unreported exception IOException must be caught or
// declared to be thrown
public static void main(String[] args)
throws IOException
{
BufferedReader br
= new BufferedReader(new InputStreamReader(
System.in)); // this will accepts the words
// for the hash trie
System.out.println(
"Enter the words separated with space to be entered into trie");
// will read the line which user will
// provide with space separated words
String input = br.readLine();
// it will split all the words
// and store them in the string array
String[] c = input.split(" ");
// now we will use constructor with string array as
// parameter and we will pass the user entered
// string in that constructor to construct the hash
// trie
TrieHash trie = new TrieHash(c);
System.out.println(
"\nEnter the word one by one to be searched in hash-trie");
String word = br.readLine();
// this will search for the word in out trie
if (trie.search(word)) {
System.out.println("Word Found in the trie");
}
else {
System.out.println(
"Word NOT Found in the trie!");
}
}
}
下面是问题陈述的实现:
Java
import java.io.*;
import java.util.*;
class TrieHash {
// implementing a HashMap
private HashMap origin;
// implementing a zero-argument constructor
public TrieHash()
{
// creating a new HashMap
origin = new HashMap();
}
// implementing another constructor
// with an array as a parameter
public TrieHash(String[] array)
{
origin = new HashMap();
// attaching that array string in the trie
for (String c : array)
attach(c);
}
// attach function to add
// character to the trie
public void attach(String str)
{
HashMap node = origin;
int i = 0;
while (i < str.length()) {
// if node already contains thatkey,
// we will simply point that node
if (node.containsKey(str.charAt(i))) {
node = node.get(str.charAt(i));
}
else {
// else we will make a new hashmap with
// that character and then point it.
node.put(str.charAt(i),
new HashMap());
node = node.get(str.charAt(i));
}
i++;
}
// putting 0 to end the string
node.put('\0', new HashMap(0));
}
// function to search for the
// specific string in the hash trie
public boolean search(String str)
{
HashMap presentNode = origin;
int i = 0;
while (i < str.length()) {
// will search for that character
// if it exists
if (presentNode.containsKey(str.charAt(i))) {
presentNode
= presentNode.get(str.charAt(i));
}
else {
// if the character does notexist that
// simply means the whole string will
// also not exists, so we will return
// false if we find a character which
// is not found in the hash trie
return false;
}
i++;
}
// this will check for the end string,
// and if the whole string is found,
// it will return true else false
if (presentNode.containsKey('\0')) {
return true;
}
else {
return false;
}
}
}
public class Main {
// unreported exception IOException
// must be caught or declared to be thrown
public static void main(String[] args)
throws IOException
{
// this will accepts the words for the hash trie
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println(
"Enter the words separated with space to be entered into trie");
// will read the line which user will
// provide with space separated words
String input = br.readLine();
// it will split all the words and
// store them in the string array
String[] c = input.split(" ");
// now we will use constructor with string
// array as parameter and we will pass the
// user entered string in that constructor
// to construct the hash trie
TrieHash trie = new TrieHash(c);
System.out.println(
"How many words you have to search in the trie");
String count = br.readLine();
int counts = Integer.parseInt(count);
for (int i = 0; i < counts; i++) {
System.out.println(
"\nEnter the word one by one to be searched in hash-trie ");
String word = br.readLine();
// this will search for the word in out trie
if (trie.search(word)) {
System.out.println(
"Word Found in the trie");
}
else {
System.out.println(
"Word NOT Found in the trie!");
}
}
}
}
输出 1:
输出 2: