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

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

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