Create a ternary tree of characters, and print all strings which can be made by following branches of that tree.
This will prepare you for Q4. Be sure to place the practice quiz in your quizspace so that you may access it during the quiz.
Create a project called PQ4
in Eclipse and import PQ4-starter.jar.
Your directory should now look like this:
PQ4/ └── src ├── Node.java └── PQ4.java
-
Go to the
Node
class and finish theNode
data structure:-
The
Node
class is used to build ternary trees. A ternary tree is very similar to a binary tree, except that it can have up to 3 children: left, center, and right.
-
The rest of the code will be in PQ4.java
.
-
Implement the buildTree() method
The image below shows the tree, with leaf nodes shown as circles:
-
Implement the printTree() method
The total code to be written, based on the instructor solution (not including brackets, comments and empty lines):
-
3 lines in
Node
class -
12 lines for the
buildTree
method -
7 lines for
printTree
method
Here is the expected output from the test code provided:
Letters: [c, a, o, u, d, n, r, g, p, w, b, r, t] Words: cad can car cog cop cow cub cur cut
HINT: You can change the code in your main
method! This won’t effect our testing.
If you have time for additional testing:
-
Try changing the characters in the letter’s array. The placement of the letters should not be hard coded.
-
Add 3 additional characters to the end of the letters array:
-
Make sure your code still runs.
-
Make sure the additional words would be produced from the
printTree
method are correct.
-
Once complete, submit PQ4.java
to Checkin.