Question
public class TreeNode { private T element; // data field private TreeNode left; // reference to left child node private TreeNode right; // reference to
public class TreeNode { private T element; // data field private TreeNode left; // reference to left child node private TreeNode right; // reference to right child node public TreeNode() // POST: empty tree node { element = null; left = right = null; } public TreeNode (T elem) // POST: tree node with element set { element = elem; left = right = null; }
public TreeNode (T elem, TreeNode left, TreeNode right) // POST: tree node with element and child nodes { element = elem; this.left = left; this.right = right; } // accessors and mutators public T getElement() { return element; } public void setElement(T element) { this.element = element; } public TreeNode getLeft() { return left; } public void setLeft(TreeNode left) { this.left = left; } public TreeNode getRight() { return right; } public void setRight(TreeNode right) { this.right = right; } public boolean isLeaf () // POST: return true if node is a leaf node { return (left == null && right == null); } }
morsecode.txt***********************************************************************
e o t - i o o a o - n - o m - - s o o o u o o - r o - o w o - - d - o o k - o - g - - o o - - - h o o o o v o o o - f o o - o l o - o o p o - - o j o - - - b - o o o x - o o - c - o - o y - o - - z - - o o q - - o -
Part II(0 pts.) The Morse code is used to encode messages where each letter is coded as a unique series of dashes and dots. This problem will practice building a binary tree to store the alphabet letters Code will use the TreeNodejava class built in lecture. The text file below lists each letter followed by its code Files are on First Class. morsecode txt-Notepad Edit Format View Help a 0 WO O O h o o o o f o o 1 o OO PO o o Store each letter of the alphabet in a binary tree. The root node stores no letter. Travel to the left represents a dot and travel to the right represents a dash. Build the tree using the Morse code to travel left or right and then insert the letter a. Initially the tree has a root node with no element. b. Read the first line of the text file e o Letter e is represented by a single dot Travel begins from the root. Dot means travel left. There is no left child encountered so make a node and insert letter e. c. Read the next line of the text file: t- Letter tis represented by a single dash. Travel begins from the root. Dash means travel right. There is no right child so make a node and insert letter t d. Read the next line of the text file: io o Letteriis represented by a two dots. Dot means travel left. The first left goes to node e. There is no left child of e so make a node and insert letter e. Repeat this process until all letters have been processed from the file and inserted into the tree. 2. Use a container to store the code for each letter. Store the code for the letter at the index using its ASCII code to determine position. The ASCII sequencefor a begins at code 97 Ex. Morse code for "a is stored in index 0. Morse code for 'b' is stored in index 1. etc. 3. After the two data structures are filled. run an interactive program where the user enters an English message using words in lower case with no punctuation. report the Morse code equivalent using the amsy with character to separate codes, and then decode the M e code back to English using the tree Report the preorder traversal of the tree. Turn in the following test cases to ensure all letters work. Preorder tree contents e i s h v u f a r 1 w p j t n d b x k c y m g z q o Enter English message: the quick fox c k f a the q Preorder tree content ei s h v u f a r l w p j t n d b x k c y m g z q o Enter English nessage jumps over Message: j p s o ver Preorder tree content e i s h v u f a r l w p j t n d b x k c y m g z q o Enter English nessage: the la dog the l a z y d o g MessageStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started