有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

我可以用java将变量传递回递归树吗?

我正在尝试用Java构建一个Trie实现,我已经成功地做到了这一点。实现的主要部分已经完成,我有方法添加单词或检查结构是否包含单词,但是我在打印trie时遇到了问题

我试图以.dot格式打印trie,以便能够使用GraphViz创建trie的.pdf文件

目前,我为TrieNode类提供了一个print方法,它以正确的格式正确打印所有节点,除了索引。我似乎无法获得为节点连接找到正确索引号的函数

下面是一个简单的工作示例:

Trie类:

package trie;

import trie.*;

public class Trie {

   private TrieNode root;

   public Trie () {
      this.root = new TrieNode();
   }

   public TrieNode getRoot() {
     return this.root;
   }

   public void add(String word) {

     this.getRoot().add(word);

   }

   public boolean contains(String word) {

      int current;
      int length = word.length();

      TrieNode crawler = this.getRoot();

      for (current = 0; current < length; current++) {
        char currentChar = word.charAt(current);
        if (!(crawler.getChildren().containsKey(currentChar))) {
          return false;
        }
        crawler = crawler.getChildren().get(currentChar);
      }

      return (crawler.isWord());
   }

   public static void main(String[] args) {
     Trie t = new Trie();
     t.add("apples");
     t.add("bananas");
     System.out.println(t.getRoot().print(0, 1));

   }

}

TrieNode类:

package trie;

import trie.*;
import java.util.*;

public class TrieNode {

  private HashMap<Character, TrieNode> children;
  private String content;
  private boolean isWord;

  public TrieNode() {
    this.children = new HashMap<Character, TrieNode>();
    this.content = "";
    this.isWord = false;
  }

  public TrieNode(String word, Character charToAdd) {
    this.children = new HashMap<Character, TrieNode>();
    this.content = word + String.valueOf(charToAdd);
    this.isWord = true;
  }

  public String getContent() {
    return this.content;
  }

  public HashMap<Character, TrieNode> getChildren() {
    return this.children;
  }

  public boolean isWord() {
    return this.isWord;
  }

  public void makeWord() {
    this.isWord = true;
  }

  public void addChild(Character charToAdd) {
    TrieNode child = new TrieNode(this.getContent(), charToAdd);
    child.isWord = false;
    this.getChildren().put(charToAdd, child);
  }

  public void add(String word) {
    if (word.length() == 0) {
      this.makeWord();
    } else {
      char first = word.charAt(0);
      if (!(this.getChildren().containsKey(first))) {
        this.addChild(first);
      }
      this.getChildren().get(first).add(word.substring(1));
    }
  }

  public String print(int from, int to) {
    String res = "";
    if (this.getChildren().isEmpty()) {
      from = 0;
      to = to + 1;
      return "";
    } else {
      for (char c : this.getChildren().keySet()) {
        res += String.format("\t %d -> %d [label=\" %s\"]\n", from, to, c);

        res += this.getChildren().get(c).print(to, to+1);
      }
    }
    return res;
  }

}

执行main程序的结果给出:

     0 -> 1 [label=" a"]
     1 -> 2 [label=" p"]
     2 -> 3 [label=" p"]
     3 -> 4 [label=" l"]
     4 -> 5 [label=" e"]
     5 -> 6 [label=" s"]
     0 -> 1 [label=" b"]
     1 -> 2 [label=" a"]
     2 -> 3 [label=" n"]
     3 -> 4 [label=" a"]
     4 -> 5 [label=" n"]
     5 -> 6 [label=" a"]
     6 -> 7 [label=" s"]

我正试图让它返回:

     0 -> 1 [label=" a"]
     1 -> 2 [label=" p"]
     2 -> 3 [label=" p"]
     3 -> 4 [label=" l"]
     4 -> 5 [label=" e"]
     5 -> 6 [label=" s"]
     0 -> 7 [label=" b"]
     7 -> 8 [label=" a"]
     8 -> 9 [label=" n"]
     9 -> 10 [label=" a"]
     10 -> 11 [label=" n"]
     11 -> 12 [label=" a"]
     12 -> 13 [label=" s"]

索引应该返回到根索引所在的位置,但我找不到阻止它重置的方法

编辑:我必须更改的唯一变量是to变量,但它似乎不能在上下文之外重新定义

你有什么想法吗


共 (0) 个答案