Write a program in java to implement the adt binary tree

Assignment Help JAVA Programming
Reference no: EM13846898

Write a program in Java to implement the ADT Binary Tree part of whose definition is given below. You are also to write a driver program that demonstrates the correctness of your implementation by way of taking a series of commands from a text file and carrying them out. In each binary tree that you will deal with, the values are distinct (i.e. there are no duplicates).

Note: Code for binary tree that I discussed in class is available on Isidore. Feel free to use it but you will assume responsibility for the correctness.

The ADT Binary Tree

The only (private) data member you can use is root, as given below. However, feel free to use as many private member functions as you need.

/*

* Class BinaryTree<T> */

public class BinaryTree<T extends Comparable<T» { /*

* Class BinaryNode *1

static private class BinaryNode<T extends Comparable<T» f

T element; // data part

BinaryNode<T> left; // left child

BinaryNode<T> right; // right child

// Add appropriate constructors for BinaryNode

// end of BinaryNode<T>

private BinaryNode<T> root; // root of the tree private final int LCHILD = 1; // constant for left child private final int RCHILD = 2; // constant for right child

// constructor for BinaryTree public BinaryTree() root = null; // construct an empty tree }

// Below are the methods that you must implement // // Create a tree with the given item as the root with its // subtrees being empty. //

Post: Previous value of the tree is lost. // public void insertRoot (T item)

// If node containing parent exists, and if the specified // child is absent, create the specified child of parent // with item as data element and return true; otherwise

// return false.
//
// Pre: data items in the tree are distinct, i.e., no duplicates.
// item, if it can be successfully inserted, is
different from
// each value in the tree.
// Post: if insertion were successful, item is the specified child of
// parent.
// Parameters:
// item: data item to be inserted
// parent: data item of the potential parent
// child -- 1 -> item is to be inserted as left
child of parent
// child == 2 => item is to be inserted as right
child of parent
// Return Value:
// true, if insertion succeeded, and false
otherwise.
public boolean insertItem (T item, T parent, int child)
// Print the tree contents in inorder to the stream // associated with the printwriter output
public void printTree(PrintWriter output)
// Returns true if vall exists, val2 exists, and they are // siblings; otherwise, returns false
public boolean siblings (T vall, T val2)
// print to the given printwriter the frontier of the tree,
// i.e., all the leaves in a "right to left reading of the // leaves" order; consecutive items are separated by a space public void printFrontier (PrintWriter output)
// delete subtree rooted at item, if item is present; // otherwise, tree remains unchanged
public void deleteSubtree (T item)
// Post: if item is present, returns reference to a string that has
// all the values stored in the leaves of the subtree
rooted at
// item; consecutive values are separated by a space.
// otherwise, returns null
public String subtreeLeaves (T item)
// Check if the subtree rooted at x in this tree and the // subtree rooted at y in the OtherTree have the same
// NUMBER of nodes
// Pre: items in each tree are distinct
// Return Value:
// If x is in this tree, y is in otherTree, and
// the subtrees referred to have the same number
// of nodes, then return true; otherwise return false
public boolean equalSubtreeNodes (T x, BinaryTree<T> otherTree, T y)
// end of BinaryTree<T>

The driver program

The purpose of your "main class" is to provide the driver code using which your implementation of binary tree can be tested. This code should declare an array of 11 BinaryTrcc<Integer>. However, only [I] through [10] of the array elements will be used. Array elements [I] through [101 will be referred to as tree I through tree 10.

This part of code prompts the user for the name of the input and output files and then proceeds to process each command in the input file. For each command processed, you should output the command itself followed by any output generated by the command.

Consult homework] for the rest of the requirements for the driver program; this applies to all the future assignments as well.
The commands that the driver should process are shown below. In each command, each of <ni>, <n2> is a tree number and hence is an integer between 1 and 10 inclusive, <val>, <parent >, <yell>, and <val2> are integers, and <chi ld> is either I (left) or 2 (right).

For each of the following, consult the specification of the corresponding function as well as the sample input/output for the required actions.
print <nl>

Print the specified tree using inorder traversal; consult sample output for format.
insertroot <nl> <val>

Insert <val> as the root of tree <nl>,
insert <nl> <val> <parent> <child>
Insert <val> as the specified <child> of node containing <parent > in tree <nl>, if possible. Report only the failures.
frontier <nl>
Print the frontier of tree <nl>.
siblings <nl> <veil> <va12>
Check if <vall> and <val2> are siblings in tree <nl>. Report outcome.
subtreenodes <nl> <veil> <n2> <va12>

Check if the subtree of tree <nl> rooted at node containing <vall> and subtree of tree <n2> rooted at node containing <va12> have the same number of nodes. Report outcome.
deletesubtree <nl> <val>

Delete the subtree rooted at node containing <val> from tree <nl>, if possible.
leaves <nl> <vall>

Obtain a string containing all the leaves in the subtree of tree <n 1> rooted at <vall>, if present. Report outcome.

Reference no: EM13846898

Questions Cloud

What price will you receive a margin call : You buy 600 shares of stock at a price of $86 and an initial margin of 75 percent. If the maintenance margin is 40 percent, at what price will you receive a margin call?
Create business plan outline-fixed and variable costs : Create a BUSINESS PLAN OUTLINE for a Brewing company in California. The Economics of the Business. Revenue Drivers and Profit Margins. Fixed and Variable Costs. Operating Leverage, and Its Implications
Quantitative methods in health care : Exercises 7.2 and 7.6 on pages 183 and 185 in Quantitative Methods in Health Care Management Complete Exercises 7.2 and 7.6 on pages 183 and 185 in Quantitative Methods in Health Care Management. Use the Excel document below to record your answers. S..
Write an essay on behavior modification : Write an essay of 500-750 words on behavior modification in which you address the following behavioral issues: State the behavior you wish to change and state the importance of changing this behavior. State triggers of the behavior
Write a program in java to implement the adt binary tree : Write a program in Java to implement the ADT Binary Tree part of whose definition is given below. You are also to write a driver program that demonstrates the correctness of your implementation by way of taking a series of commands from a text fil..
Valuate the hazard and its associated risk : Following steps 1-11 of the Hazard Analysis and Risk Assessment Guide in the course textbook (pp. 171-174), develop a risk assessment for a serious workplace hazard with which you are familiar, or use Addendum D to Chapter 11 (pp. 202-206) to help yo..
After-tax cost of preferred stock : Calculate the after-tax cost of preferred stock for Bozeman-Western Airlines Inc. which is planning to sell $10 million of $6.50 cumulative preferred stock to the public at a price of $50 a share. Issuance costs are estimated to be $2 a share. The co..
Describe how they influenced the behaviors and the choices : Describe how they influenced the behaviors and the choices that you or others made, and relate these to the methods for changing criminal behavior that you have studied in this phase?
Modified duration and the capital gains formula method : A bond with 3 years remaining to maturity has an annual coupon rate of 8.5%, and a face value of $1,000. If interest rates fall 0.15% from the given YTM, by what percent will the bond change in value? Show these 2 ways (using modified duration and th..

Reviews

Write a Review

JAVA Programming Questions & Answers

  Write a java application that calculates and displays

The application should ask the user to enter the name of the property owner and the assessed value of the property for each owner (total N owners) and calculate the property tax.

  Write an application that takes in an array of 30 grades

Write an application that takes in an Array of 30 Grades (integer) and returns the maximum grade, minimum grade, average of all grades, and prints a Bar Chart to show the grade distribution.

  Write a program using java script to convert a currency

Write a program using Java Script to convert a currency from dollars onto euro. If the rate of conversion is for each Euro 1.8 dollars. The use shall continue to convers as long as he/she desire to do so. Also print output via a message box

  Complete the design details of the classes in the package

The design phase of the SRS project is in full swing and every developer on the team is assigned a group of packages to work on and to complete the design details of the classes in the package. To help speed up the design process, you-as the softw..

  Write a bag class with a generic type

Write a Bag class with a generic type. Write a class such as item that has a name and price. Add objects of item to the bag and find the average and the total of the price of all items in the bag.

  Prepare an overloaded constructor

Prepare an overloaded constructor that provides values for each field. Also provide get methods for each field. Save the file as Patient.java.

  Represent the relationship between parents - children class

Draw a class diagram that represents the relationship between parents, children, and grandparents. Take into account that a person can have both a parent and a child. Annotate associations with roles and multiplicities.

  Initialized the array of words by an assignment statement.

modify the following program . The program from Lab 4 initialized the array of words by an assignment statement.

  Write a client code that creates two objects

Write a Java program that (1) Defines a base/super class A, a subclass B (inherits from A), and a subclass C (inherits from B).

  Loop structure

Suggest one (1) example of a problematic programming situation or scenario that the use or implementation of a loop structure could resolve. Justify your response

  Write down the business needs for a computer program and

write the business requirements for a computer program and identify which entities are to be modeled using object

  Complete the classes cipher - caesar and transpose

Complete the classes Cipher, Caesar, and Transpose, and Reverser - Define an interface called Constants that will store the value 26 in the identifier WRAP_AROUND;

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd