递归填充树

发布于 2024-10-23 20:14:57 字数 1175 浏览 3 评论 0原文

我有一个Java类:BinaryTree< t>我从文件中填充如下:

E .
T -
I ..
N -.
M --
A .-
W .--
R .-.
S ...
etc (to end of alphabit)

BinaryTree has:

setRight(BinaryTree) -sets the right element
setLeft(BinaryTree) -sets the left element   
setRootElement(t) -sets the root element
getRight() -gets the right element
getLeft()  -gets the left element 
getRootElement() -gets the root element of the node IE/ a Character
size() -returns the size of the tree

这些是我得到的 BinaryTree 类中唯一可用的方法

所以我想做的是我想逐一读取文件的每一行,获取字母和“莫尔斯电码”字符串。注意:我只能使用 Scanner 类来读取文件!

然后我想从文件的内容和一些规则递归地填充这棵树:

一个“。”意味着向左定位,因此文件的第一部分意味着将带有“E”字符的节点定位到根的左侧

“-”表示向右定位,因此文件中的第二行表示将带有“T”字符的节点定位到根右侧。

因此,“W .--”表示从根开始用“W”固定节点,向左移动一个节点,向右移动一个节点,然后固定到该节点的右侧。

最后树看起来像:

tree http://i56.tinypic.com/339tuys.png< /a>

由于我是递归的新手,因此在使用扫描仪读取文件时,我很难想象如何递归地填充树。

我是否必须在其他地方读取文件并将信息传递到递归方法中???

或者我可以直接用递归方法读取文件吗?这似乎不可能。

另外,您将使用什么作为基本案例,我很想使用 t.size() == 27,因为这是最终树的大小。

任何建议或意见将不胜感激!

谢谢你!

I have a Java class: BinaryTree< t > that I am filling from a file as follow:

E .
T -
I ..
N -.
M --
A .-
W .--
R .-.
S ...
etc (to end of alphabit)

BinaryTree has:

setRight(BinaryTree) -sets the right element
setLeft(BinaryTree) -sets the left element   
setRootElement(t) -sets the root element
getRight() -gets the right element
getLeft()  -gets the left element 
getRootElement() -gets the root element of the node IE/ a Character
size() -returns the size of the tree

These are the only methods available in the BinaryTree class I was given

So what i want to do is I want to read each line of the file one by one getting the Letter and the string of "morse code". NOTE: I can only use the Scanner class for reading the File!

Then i want to recursively fill this tree from the contents of the file and a few rules:

A "." means tack to left so the first part of the file would mean tack node with 'E' character to the Left of the root

A "-" means tack to right so the second line in the file would mean tack node with 'T' character to the Right of the root.

So "W .--" would mean tack node with 'W' from root One node to the left, then one node to the right then tack on to the right of that Node.

In the end the tree would look like:

tree http://i56.tinypic.com/339tuys.png

Since i'm new to Recursion I am having a lot of trouble visualizing how a tree could be filled recursively while reading from a file using a scanner.

Would I have to read the file elsewhere and pass the information into the recursive method???

Or could I read the file right in the recursive method? Which doesn't seem possible.

Also, what would you use as a Base Case, i'm tempted to use t.size() == 27, because that is the size of the final tree.

Any suggestions or comments would be greatly appreciated!!

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2024-10-30 20:14:57
Scanner sc = new Scanner(new File(...));
while (sc.hasNext()) {
  String letter = sc.next();
  String morse = sc.next();
  BinaryTree forPosition = theBinaryTree;
  for(int i = 0; i < morse.length(); i++) {
    if (morse.charAt(i) == '.') {
      if(forPosition.getLeft() == NULL) {
        forPosition.setLeft() = new BinaryTree();
      }
      forPosition = forPosition.getLeft();
    }
    else {
      // similar
    }
  }
  forPostion.setRootElement(letter);
}

一个奇怪的递归版本:

Scanner sc = new Scanner(new File(...));
while (sc.hasNext()) {
  String letter = sc.next();
  String morse = sc.next();
  findTheNode (theBinaryTree, letter, morse);
  }
  forPostion.setRootElement(letter);
}

findTheNode (BinaryTree node, String letter, String morse) {
  if (morse.length() == 0) {
    node.setRootElement(letter);
    return;
  } // found

  if (morse.charAt(0) == '.') {
    if (node.getLeft() == NULL) {
      node.setLeft() = new BinaryTree();
    }
    findTheNode (node.getLeft(), letter, morse.substring(1));
  }
  else {
    // similar
  }   
}

希望以上两者都有效。

结果可能类似于这个


递归通常用于遍历二叉搜索树,但这棵树更类似于Trie,字母表中只有 2 个字符(即 . 和 -)。树的构造规则(左为.,右为-)使得不需要使用递归。

Scanner sc = new Scanner(new File(...));
while (sc.hasNext()) {
  String letter = sc.next();
  String morse = sc.next();
  BinaryTree forPosition = theBinaryTree;
  for(int i = 0; i < morse.length(); i++) {
    if (morse.charAt(i) == '.') {
      if(forPosition.getLeft() == NULL) {
        forPosition.setLeft() = new BinaryTree();
      }
      forPosition = forPosition.getLeft();
    }
    else {
      // similar
    }
  }
  forPostion.setRootElement(letter);
}

A weird recursive version:

Scanner sc = new Scanner(new File(...));
while (sc.hasNext()) {
  String letter = sc.next();
  String morse = sc.next();
  findTheNode (theBinaryTree, letter, morse);
  }
  forPostion.setRootElement(letter);
}

findTheNode (BinaryTree node, String letter, String morse) {
  if (morse.length() == 0) {
    node.setRootElement(letter);
    return;
  } // found

  if (morse.charAt(0) == '.') {
    if (node.getLeft() == NULL) {
      node.setLeft() = new BinaryTree();
    }
    findTheNode (node.getLeft(), letter, morse.substring(1));
  }
  else {
    // similar
  }   
}

Hope both of the above work.

The result may look like this.


Recursive is usually used for traversal and binary search tree, but this tree is more similar to Trie, of only 2 character in alphabet (i.e. . and -). The rule of the construction of the tree (. for left, and - for right) makes it unnecessary to use recursion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文