【问题标题】:Deserialization of Binary Decision Tree二叉决策树的反序列化
【发布时间】:2014-02-07 03:11:04
【问题描述】:

我正在尝试在创建文件之前从文件中读取决策树。我有一个节点类,它包含 3 个变量节点(消息、yesNode、noNode)。 Message 代表要问的问题或答案。如果答案是肯定的,yesNode 代表到下一个节点的链接,如果答案不是,noNode 代表到下一个节点的链接。我尝试读取的文件示例如下:

Are you a mammal?
Are you bigger than a cat?
does it have tusks
elephant
#
#
Kangaroo
#
#
Mouse
#
#
Do you live underwater?
Trout
#
#
Robin
#
#

文件存储在preOrder遍历中,#代表空值。我不知道我应该如何尝试实现这一点,有什么建议吗?

【问题讨论】:

标签: java deserialization decision-tree


【解决方案1】:

有几种方法,这是一种伪java:

尝试类似:

class Node {
  String message;
  Node yes;
  Node no;
}

Reader myFile = new FileReader("datafile.txt"); // reads your file
Node myTree = parseNode(); // will point to the root of your tree

// This recursive function traverses (and builds) your tree.
Node parseNode() {
  Node newNode;
  String input = myFile.readline();
  if (input.equals("#)) {
    return null;
  } else {
    newNode.message = input;
    newNode.yes = parseNode();
    newNode.no = parseNode();
}

(我所说的伪 Java 是指它 [主要是] Java 语法,但它不会按原样编译,而是为了展示一般想法。)

【讨论】:

    猜你喜欢
    • 2020-04-09
    • 2021-11-14
    • 2013-01-07
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多