Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

BinaryTree.java: public class BinaryTree > { private Node root; public BinaryTree (){ root = null; } private BinaryTree (Node r){ root = r; // null

image text in transcribed

BinaryTree.java:

public class BinaryTree> { private Node root;

public BinaryTree (){ root = null; }

private BinaryTree (Node r){ root = r; // null or not is controlled inside of Node. }

public BinaryTree (E data, BinaryTree leftT, BinaryTree rightT){ root = new Node(data); if (leftT!=null) root.left = leftT.root; else root.left = null; if (rightT != null) root.right = rightT.root; else root.right = null; }

public String toString(){ String s = ""; return rec_toString(root, 1, s); }

private String rec_toString(Node node, int depth, String s){ s+=" "; if (node == null) return s+="null "; else { String str =""; str = rec_toString(node.left, depth+1, s); str+=s+""+node.data+" "; str += rec_toString(node.right, depth+1, s); return str; } }

private static class Node { private E data; private Node left; private Node right;

private Node(E item){ data = item; left = null; right = null; }

private Node(E item, Node refLeft, Node refRight){ data = item; left = refLeft; right = refRight; }

} //end of private class }

BinaryTreeTest.java:

import javax.swing.JOptionPane;

import java.util.*;

import java.io.*;

public class BinaryTreeTest {

public static void main(String args[]){

BinaryTree a = new BinaryTree();

String filename =

JOptionPane.showInputDialog("Enter the file name: ");

File inputFile = new File (filename);

try {

Scanner input = new Scanner(inputFile);

while(input.hasNext()){

Integer val = input.nextInt();

a.add(val);

}

input.close();

}

catch (FileNotFoundException e)

{

System.out.println("file reading fails.");

}

System.out.println(a);

Scanner kb = new Scanner(System.in);

System.out.println("1: select the deletion");

System.out.println("2: select the search");

System.out.println("3: find_min");

System.out.println("4: find_max");

System.out.println("5: insert another record");

System.out.println("6: display the tree in infix travel");

System.out.println("7: display the tree in postfix travel");

System.out.println("8: display the tree in prefix travel");

System.out.println("9: display the tree with toString");

System.out.println("0/others: QUiT");

int ans = kb.nextInt();

while(1

String str;

switch(ans){

case 1:

str =

JOptionPane.showInputDialog("Key in the item (i.e., content of tree node) to delete:");

a.delete(Integer.parseInt(str));

break;

case 2:

str =

JOptionPane.showInputDialog("Key in the item (i.e., content of tree node) to search:");

System.out.println("Search result: "+a.search(Integer.parseInt(str)));

break;

case 3:

System.out.println("Minimum: "+a.find_min());

break;

case 4:

System.out.println("Maximum: "+a.find_max());

break;

case 5:

str =

JOptionPane.showInputDialog("Key in the item (i.e., content of tree node) to insert");

a.add(Integer.parseInt(str));

break;

case 6:

System.out.println("Display the tree in the infix travel");

System.out.println(a.infixString());

break;

case 7:

System.out.println("Display the tree in the postfix travel");

System.out.println(a.postfixString());

break;

case 8:

System.out.println("Display the tree in the prefix travel");

System.out.println(a.prefixString());

break;

case 9:

System.out.println("The tree display with reserved spaces");

System.out.println(a);

break;

}

System.out.println("1: select the deletion");

System.out.println("2: select the search");

System.out.println("3: find_min");

System.out.println("4: find_max");

System.out.println("5: insert another record");

System.out.println("6: display the tree in infix travel");

System.out.println("7: display the tree in postfix travel");

System.out.println("8: display the tree in prefix travel");

System.out.println("9: display the tree with toString");

System.out.println("0/others: QUiT");

ans = kb.nextInt();

}

}

}

DictionaryTreeTest.java:

import javax.swing.JOptionPane;

import java.util.*;

import java.io.*;

public class DictionaryTreeTest {

public static void main(String args[]){

BinaryTree a = new BinaryTree();

String filename =

JOptionPane.showInputDialog("Enter the file name: ");

File inputFile = new File (filename);

try {

Scanner input = new Scanner(inputFile);

while(input.hasNext()){

String val = input.nextLine();

a.add(val);

}

input.close();

}

catch (FileNotFoundException e)

{

System.out.println("file reading fails.");

}

System.out.println(a);

Scanner kb = new Scanner(System.in);

System.out.println("1: select the deletion");

System.out.println("2: select the search");

System.out.println("3: find_min");

System.out.println("4: find_max");

System.out.println("5: insert another record");

System.out.println("6: display the tree in infix travel");

System.out.println("7: display the tree in postfix travel");

System.out.println("8: display the tree in prefix travel");

System.out.println("9: display the tree with toString");

System.out.println("0/others: QUiT");

int ans = kb.nextInt();

while(1

String str;

switch(ans){

case 1:

str =

JOptionPane.showInputDialog("Key in the item (i.e., content of tree node) to delete:");

a.delete(str);

break;

case 2:

str =

JOptionPane.showInputDialog("Key in the item (i.e., content of tree node) to search:");

System.out.println("Search result: "+a.search(str));

break;

case 3:

System.out.println("Minimum: "+a.find_min());

break;

case 4:

System.out.println("Maximum: "+a.find_max());

break;

case 5:

str =

JOptionPane.showInputDialog("Key in the item (i.e., content of tree node) to insert");

a.add(str);

break;

case 6:

System.out.println("Display the tree in the infix travel");

System.out.println(a.infixString());

break;

case 7:

System.out.println("Display the tree in the postfix travel");

System.out.println(a.postfixString());

break;

case 8:

System.out.println("Display the tree in the prefix travel");

System.out.println(a.prefixString());

break;

case 9:

System.out.println("The tree display with reserved spaces");

System.out.println(a);

break;

}

System.out.println("1: select the deletion");

System.out.println("2: select the search");

System.out.println("3: find_min");

System.out.println("4: find_max");

System.out.println("5: insert another record");

System.out.println("6: display the tree in infix travel");

System.out.println("7: display the tree in postfix travel");

System.out.println("8: display the tree in prefix travel");

System.out.println("9: display the tree with toString");

System.out.println("0/others: QUiT");

ans = kb.nextInt();

}

}

}

2. Implement the class BinarvTree to support the program BinaryTreeTest.java. You can test the program with the input files BT1 and BT2 from the above zip file jGRASP exec: java Bin st null null null null null null null 1: select the deletion 2: select the search 3: find min 4: find max 5: insert another record 6: display the tree in infix travel 7: display the tree in postfix travel 8: display the tree in prefix travel 9:display the tree with toString 0/Others : QUT Display the tree in the infix travel 1: select the deletion 2: select the search 3: find min 4: find max 5: insert another record 6: display the tree in infix travel 7: display the tree in postfix travel 8: display the tree in prefix travel 9: display the tree with tostring 0/Others: QUT The same binary tree class should also support the DictionaryTree.java with the input file BTdictionary

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions

Question

Explain the uses and limitations of a balance sheet.

Answered: 1 week ago

Question

This cell is [Select] For this cell, 2n= [Select] coco Cell A

Answered: 1 week ago