递归填充树
我有一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个奇怪的递归版本:
希望以上两者都有效。
结果可能类似于这个。
递归通常用于遍历和二叉搜索树,但这棵树更类似于Trie,字母表中只有 2 个字符(即
. 和
-
)。树的构造规则(左为.
,右为-
)使得不需要使用递归。A weird recursive version:
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.