如何在Java中打印Trie?
我有一个trie实现,我想打印我的trie,这样我就可以看到其中的内容。最好是深度优先遍历,这样单词才有意义。这是我的密码:如何在Java中打印Trie?,java,printing,trie,Java,Printing,Trie,我有一个trie实现,我想打印我的trie,这样我就可以看到其中的内容。最好是深度优先遍历,这样单词才有意义。这是我的密码: package trie; public class Trie { public TrieNode root; public Trie(){ root = new TrieNode(); } /* public Trie(char c){ TrieNode t = new TrieNode(c)
package trie;
public class Trie {
public TrieNode root;
public Trie(){
root = new TrieNode();
}
/*
public Trie(char c){
TrieNode t = new TrieNode(c);
root = t;
}*/
public void insert(String s, int phraseNb){
int i = 0;
TrieNode node = root;
char[] string = s.toCharArray();
TrieNode child = null;
while(i < string.length){
child = node.getChild(string[i]);
if(child == null){
child = new TrieNode(string[i]);
node.addChild(child);
}
else{
node = child;
}
i++;
}
node.endOfWord();
node.setNb(phraseNb);
}
public int[] search(char[] c){
TrieNode node = root;
for(int i = 0; i < c.length-1; i++){
node = root;
int s = 0;
while(i+s < c.length){
TrieNode child = node.getChild(c[i + s]);
if(child == null){
break;
}
if(child.isWord()){
return new int[] {i, s+1, node.getNb()};
}
node = child;
s++;
}
}
return new int[] {-1, -1, -1};
}
public void print(){
}
}
package trie;
import java.io.*;
import java.util.*;
public class TrieNode {
private boolean endOfWord;
private int phraseNb;
private char letter;
private HashSet<TrieNode> children = new HashSet<TrieNode>();
public TrieNode(){}
public TrieNode(char letter){
this.letter = letter;
}
public boolean isWord(){
return endOfWord;
}
public void setNb(int nb){
phraseNb = nb;
}
public int getNb(){
return phraseNb;
}
public char getLetter(){
return letter;
}
public TrieNode getChild(char c){
for(TrieNode child: children){
if(c == child.getLetter()){
return child;
}
}
return null;
}
public Set<TrieNode> getChildren(){
return children;
}
public boolean addChild(TrieNode t){
return children.add(t);
}
public void endOfWord(){
endOfWord = true;
}
public void notEndOfWord(){
endOfWord = false;
}
}
包trie;
公共类Trie{
公共三极根;
公共图书馆{
根=新的三节点();
}
/*
公共的黎波里(字符c){
三节点t=新三节点(c);
根=t;
}*/
公共void插入(字符串s,int短语nb){
int i=0;
三节点=根;
char[]string=s.toCharArray();
三节点子节点=空;
while(i
我只需要一个关于如何去做的解释或者一些伪代码。谢谢您的时间。我记得我的大学时代,当时我试图在控制台上打印树。在印刷方面,Trie是相同的。。。这就是我所做的,也是我建议你做的: 拿些纸在那儿画你的画。 现在想一想你想如何打印trie。 我认为trie是由N-tree(不是一个二叉树,而是一个有很多子树)组成的。除此之外,它是一个递归结构,就像一棵树。 所以你可以在这里应用深度优先的方法 假设您要打印这样的trie(节点“a”是根): a 这就像一个trie,包含以下单词: 公元 阿贝 abf abg 因此,从根开始,累积偏移量并递归遍历:
printTrie(Node node, int offset) {
print(node, offset)
// here you can play with the order of the children
for(Node child : node.getChildren()) {
printTrie(child, offset + 2)
}
}
从以下内容开始递归:
printTrie(root, 0)
你就完蛋了
我用2作为一个常数来处理偏移变化系数,把它改成3,4或者其他什么,看看会发生什么
希望这有帮助。
祝你好运 当处理Trie这样的复合数据结构时,设计模式通常很有用。它使开发人员能够将复合数据结构的遍历与每个节点检索到的信息解耦
可以使用访问者设计模式打印Trie。这可能会有所帮助
public void printTrie(TrieNode node,String s) {
String strSoFar = s;
strSoFar += String.valueOf(node.c);
if(node.isLeaf)
{
System.out.println(strSoFar);
return;
}
else
{
Stack<TrieNode> stack = new Stack<TrieNode>();
Iterator<TrieNode> itr = node.children.values().iterator();
while(itr.hasNext())
{
stack.add(itr.next());
}
while(!stack.empty()){
TrieNode t = stack.pop();
printTrie(t,strSoFar);
}
}
}
这里是我刚刚拼凑的一个例子。不是美丽的打印,只能打印一次,但它完成了工作。希望能有帮助
import java.util.*;
public class Trie
{
static TrieNode root = new TrieNode();
static char endMarker = '?';
public static void main(String[] args)
{
System.out.println(checkPresentsAndAdd("test"));
System.out.println(checkPresentsAndAdd("taser"));
System.out.println(checkPresentsAndAdd("tester"));
System.out.println(checkPresentsAndAdd("tasters"));
System.out.println(checkPresentsAndAdd("test"));
System.out.println(checkPresentsAndAdd("tester"));
printTrie(root);
}
public static boolean checkPresentsAndAdd(String word)
{
TrieNode node = root;
for(int i = 0; i < word.length(); i++)
{
char c = word.charAt(i);
if(node.checkIfNeighbor(c))
{
node = node.getNeighbor(c);
}
else
{
node.addNeighbor(c);
node = node.getNeighbor(c);
}
}
boolean nord = false;
if(!node.checkIfNeighbor(endMarker))
{
nord = true;
node.addNeighbor(endMarker);
}
return nord;
}
public static void printTrie(TrieNode node)
{
if(node == null || node.visited)
return;
LinkedList<TrieNode> q = new LinkedList<TrieNode>();
System.out.println(node);
node.visited = true;
q.add(node);
while (!q.isEmpty())
{
TrieNode x = q.removeFirst();
for(Map.Entry<Character,TrieNode> i : x.neighbors.entrySet())
{
if(i.getValue().visited == false)
{
System.out.println(i);
i.getValue().visited = true;
q.add(i.getValue());
}
}
}
}
}
class TrieNode
{
Map<Character, TrieNode> neighbors = new HashMap<Character, TrieNode>();
boolean visited = false;
public TrieNode(){}
public boolean checkIfNeighbor(char c)
{
return neighbors.containsKey(c);
}
public TrieNode getNeighbor(char c)
{
if(checkIfNeighbor(c))
{
return neighbors.get(c);
}
return null;
}
public void addNeighbor(char c)
{
TrieNode node = new TrieNode();
neighbors.put(c, node);
}
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("Node:[neighbors: ");
sb.append(neighbors);
sb.append("]\n");
return sb.toString();
}
}
import java.util.*;
公共类Trie
{
静态三节点根=新三节点();
静态字符endMarker='?';
公共静态void main(字符串[]args)
{
System.out.println(选中presents并添加(“测试”);
System.out.println(选中presents并添加(“taser”);
System.out.println(选中Presents并添加(“测试仪”);
System.out.println(选中presents并添加(“品尝者”);
System.out.println(选中presents并添加(“测试”);
System.out.println(选中Presents并添加(“测试仪”);
printTrie(根);
}
公共静态布尔校验PresentsAndAdd(字符串字)
{
三节点=根;
for(int i=0;itrieObject.printTrie(trieObject.getRoot(), "");
import java.util.*;
public class Trie
{
static TrieNode root = new TrieNode();
static char endMarker = '?';
public static void main(String[] args)
{
System.out.println(checkPresentsAndAdd("test"));
System.out.println(checkPresentsAndAdd("taser"));
System.out.println(checkPresentsAndAdd("tester"));
System.out.println(checkPresentsAndAdd("tasters"));
System.out.println(checkPresentsAndAdd("test"));
System.out.println(checkPresentsAndAdd("tester"));
printTrie(root);
}
public static boolean checkPresentsAndAdd(String word)
{
TrieNode node = root;
for(int i = 0; i < word.length(); i++)
{
char c = word.charAt(i);
if(node.checkIfNeighbor(c))
{
node = node.getNeighbor(c);
}
else
{
node.addNeighbor(c);
node = node.getNeighbor(c);
}
}
boolean nord = false;
if(!node.checkIfNeighbor(endMarker))
{
nord = true;
node.addNeighbor(endMarker);
}
return nord;
}
public static void printTrie(TrieNode node)
{
if(node == null || node.visited)
return;
LinkedList<TrieNode> q = new LinkedList<TrieNode>();
System.out.println(node);
node.visited = true;
q.add(node);
while (!q.isEmpty())
{
TrieNode x = q.removeFirst();
for(Map.Entry<Character,TrieNode> i : x.neighbors.entrySet())
{
if(i.getValue().visited == false)
{
System.out.println(i);
i.getValue().visited = true;
q.add(i.getValue());
}
}
}
}
}
class TrieNode
{
Map<Character, TrieNode> neighbors = new HashMap<Character, TrieNode>();
boolean visited = false;
public TrieNode(){}
public boolean checkIfNeighbor(char c)
{
return neighbors.containsKey(c);
}
public TrieNode getNeighbor(char c)
{
if(checkIfNeighbor(c))
{
return neighbors.get(c);
}
return null;
}
public void addNeighbor(char c)
{
TrieNode node = new TrieNode();
neighbors.put(c, node);
}
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("Node:[neighbors: ");
sb.append(neighbors);
sb.append("]\n");
return sb.toString();
}
}
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
class TrieNode{
private char c;
private Map<Character,TrieNode> children = new HashMap<>();
private boolean isLeaf = false;
TrieNode(){
}
/**
* @param c the c to set
*/
public void setC(char c) {
this.c = c;
}
/**
* @return the children
*/
public Map<Character, TrieNode> getChildren() {
return children;
}
/**
* @return the isLeaf
*/
public boolean isLeaf() {
return isLeaf;
}
/**
* @return the c
*/
public char getC() {
return c;
}
/**
* @param isLeaf the isLeaf to set
*/
public void setLeaf(boolean isLeaf) {
this.isLeaf = isLeaf;
}
}
class Trie{
TrieNode rootNode;
public Trie(){
rootNode = new TrieNode();
}
public void insertWord(String word){
TrieNode current = rootNode;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
Map<Character,TrieNode> children = current.getChildren();
if(children.containsKey(c)){
current = children.get(c);
}
else{
TrieNode trieNode = new TrieNode();
children.put(c, trieNode);
current = children.get(c);
}
}
current.setLeaf(true);
}
public boolean searchWord(String word){
TrieNode current = rootNode;
for (int i = 0; i < word.length(); i++) {
Map<Character,TrieNode> children = current.getChildren();
char c = word.charAt(i);
if(children.containsKey(c)){
current = children.get(c);
}
else{
return false;
}
}
if(current.isLeaf() && current!=null){
return true;
}
else{
return false;
}
}
public void print(TrieNode rootNode,int level, StringBuilder sequence) {
if(rootNode.isLeaf()){
sequence = sequence.insert(level, rootNode.getC());
System.out.println(sequence);
}
Map<Character, TrieNode> children = rootNode.getChildren();
Iterator<Character> iterator = children.keySet().iterator();
while (iterator.hasNext()) {
char character = iterator.next();
sequence = sequence.insert(level, character);
print(children.get(character), level+1, sequence);
sequence.deleteCharAt(level);
}
}
}
class TrieImplementation{
public static void main(String[] args) {
Trie trie = new Trie();
trie.insertWord("Done");
trie.insertWord("Dont");
trie.insertWord("Donor");
trie.insertWord("Sanjay");
trie.insertWord("Ravi");
trie.insertWord("RaviRaj");
TrieNode root = trie.rootNode;
trie.print(root,0,new StringBuilder(""));
System.out.println(trie.searchWord("Dont"));
System.out.println(trie.searchWord("Donor"));
System.out.println(trie.searchWord("Jay"));
System.out.println(trie.searchWord("Saviraj"));
System.out.println(trie.searchWord("RaviRaj"));
System.out.println(trie.searchWord("Aaviraj"));
}
}