Morse Code Tree Implementation in Java, Summaries of Operating Systems

The implementation of a Morse Code tree in Java using a TreeNode class. The tree is constructed by inserting Morse codes of characters and traversing the tree to print Morse codes of strings in pre-order and post-order. The document also includes methods for height calculation and insertion of nodes.

Typology: Summaries

2021/2022

Uploaded on 11/19/2022

amnan-mirza-1
amnan-mirza-1 🇵🇰

1 document

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
package tree;
import java.util.*;
public class TreeNode<T> {
public T element;
private TreeNode<T> left;
private TreeNode<T> right;
public TreeNode(T element){
this.element = element;
}
public TreeNode() {
this.element = null;
}
public T getElement() {
return element;
}
public TreeNode<T> getLeft(){
return left;
}
public TreeNode<T> getRight(){
return right;
}
public boolean isLeaf() {
return this.getLeft() == null && this.getRight() == null;
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Morse Code Tree Implementation in Java and more Summaries Operating Systems in PDF only on Docsity!

package tree; import java.util.*; public class TreeNode { public T element; private TreeNode left; private TreeNode right; public TreeNode(T element){ this.element = element; } public TreeNode() { this.element = null; } public T getElement() { return element; } public TreeNode getLeft(){ return left; } public TreeNode getRight(){ return right; } public boolean isLeaf() { return this.getLeft() == null && this.getRight() == null;

public void setLeft(TreeNode t) { left = t; } public void setRight(TreeNode t) { right = t; } public void preorder(TreeNode node) { if (node == null) { return; } System.out.println(node.getElement()); try { String s= node.getElement().toString(); char c=s.charAt(0);//returns h System.out.println( "|"+ morseEncode(c)+"|"); } catch(NullPointerException e) { System.out.println(""); } preorder(node.getLeft()); preorder(node.getRight()); }

return "o-oo"; case 'm': return "--"; case 'n': return "- oo"; case 'o': return "---"; case 'p': return "o--o"; case 'q': return "--o-"; case 'r': return "o-o"; case 's': return "o o o"; case 't': return "-"; case 'u': return "o o-"; case 'v': return "oooo"; case 'w': return "o--"; case 'x': return "-oo-"; case 'y': return "-o--"; case 'z': return "--oo";

case '1': return "o----"; case '2': return "oo---"; case '3': return "ooo--"; case '4': return "oooo-"; case '5': return "ooooo"; case '6': return "-oooo"; case '7': return "--ooo"; case '8': return "--oo"; case '9': return "----o"; case '0': return "-----"; default: return ""; } } public void postorder(TreeNode node) { if (node == null) { return; } postorder(node.getLeft());

public void insert(T element) { TreeNode insertTarget = new TreeNode(element); // while(!temp.isLeaf()) { // temp = temp.getLeft(); // } // temp.setLeft(insertTarget); // if (this.isLeaf()) { // this.setLeft(insertTarget); // } // else if (this.getLeft() == null) { // this.setLeft(insertTarget); // } // else if (this.getRight() == null){ // this.setRight(insertTarget); // } // else { // this.getLeft().insert(element); // } // if (height() == 0) { // return; // } if (height() == 0) { this.setLeft(insertTarget); } else {

if (this.getLeft() == null){ this.setLeft(insertTarget); } else if (this.getRight() == null){ this.setRight(insertTarget); } else{ if (this.getLeft().height() <= this.getRight().height()){ this.getLeft().insert(element); } else if (this.getLeft().height() > this.getRight().height()){ this.getRight().insert(element); } } } } public void insertLeft(T element){ if (this.getLeft() == null){ this.setLeft(new TreeNode(element)); } else{ System.out.println("Tried to insert a node overwriting an existing node on the left!"); } } public void insertRight(T element){ if (this.getRight() == null){ this.setRight(new TreeNode(element));