Uploaded by Amnan Mirza

javaproject

advertisement
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;
}
public void setLeft(TreeNode<T> t) {
left = t;
}
public void setRight(TreeNode<T> t) {
right = t;
}
public void preorder(TreeNode<T> 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());
}
public String morseEncode(char x)
{
// refer to the Morse table
// image attached in the article
switch (x) {
case 'a':
return "o -";
case 'b':
return "-ooo";
case 'c':
return "-o-o";
case 'd':
return "-oo";
case 'e':
return "o";
case 'f':
return "oo-o";
case 'g':
return "--o";
case 'h':
return "oooo";
case 'i':
return "oo";
case 'j':
return "o---";
case 'k':
return "-o-";
case 'l':
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<T> node) {
if (node == null) {
return;
}
postorder(node.getLeft());
postorder(node.getRight());
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("");
//}
}
public int height() {
if (this.isLeaf()) {
return 0;
}
else if (this.getLeft() != null && this.getRight() == null){
return this.getLeft().height() + 1;
}
else if (this.getLeft() == null && this.getRight() != null){
return this.getRight().height() + 1;
}
else {
return Math.max(this.getLeft().height(), this.getRight().height())+1;
}
}
public void insert(T element) {
TreeNode<T> insertTarget = new TreeNode<T>(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<T>(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<T>(element));
}
else{
System.out.println("Tried to insert a node overwriting an existing node on the right!");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeNode obj=new TreeNode();
Scanner sc= new Scanner(System.in);
System.out.print("Enter a string: ");
String str= sc.nextLine();
for (int i = 0; i < str.length(); i++) {
//t System.out.print( str.charAt(i));
obj.insert( str.charAt(i));
}
// obj.insert("t");
// obj.insert("h");
// obj.insert("e");
System.out.print("Post Order of string"+"\n");
obj.postorder(obj);
System.out.print("Pre Order of string"+"\n");
obj.preorder(obj);
System.out.print("Morse Code of string"+"\n");
obj.preorder(obj);
}
}
Download