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

  We developed a stringlog adt it represents a log that holds

we developed a stringlog adt. it represents a log that holds objects of class string. suppose that instead of

  Create an employee class.

Create an Employee class. Items to include as data members are employee number, name, date of hire, job description, department, and monthly salary.

  1 ra row your boatprepare a world with a boat a person

1. ra row your boatprepare a world with a boat a person sitting in the boat an island and a pier located 25 meters from

  Adt for a two color double stack adt consists of two stacks

design an adt for a two color double stack adt that consists of two stacks one red and one blue and has its operations color coded versions of the regular stack adt operations.

  Program that computes and prints the value of 6!/5! using

Write a Java program that computes and prints the  value of 6!/5! using Scanner.

  Write the entry or record into a sequential file

Develop a Java based GUI application(using swing) that allows the user to enter Charity/Fund information: Write the entry/record into a sequential file

  What are the advantages and disadvantages of ood

In OOD, an object combines data and operations on that data into a single unit, a feature called encapsulation. Discuss the following question with your classmates:What are the advantages and disadvantages of OOD?

  Constructor that accepts a file name as its argument

Write a class with a constructor that accepts a file name as its argument. Assume the file contains a series of numbers, each written on a separate line. The class should read the contents of the file into an array, and then displays the following..

  A client or server prepare to use udp in a java application

How does a client or server prepare to use UDP in a Java application? Are there any differences in what the client does and what the server does?

  Java graphics-write an application that extends jframe and

java graphics-write an application that extends jframe and that displays a phrase in every font size from 6 through

  Design the appropriate accessor and mutator methods

Design an Employee class that has fields for the following pieces of information: Employee Name Employee Number Next, design a class named ProductionWorker that extends the Employee class.

  Punished for minor infractions given their mental illness

What if you a Disciplinary Hearing Officer in a unit with mentally ill inmates? How would you sanction offenders for infractions without punishing them for their illnesses? Do you think they should be punished for minor infractions given their mental..

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