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 t开发者_开发知识库he 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!
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.
精彩评论