The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | May 2017 |
| Last Sign in: | 398 Weeks Ago, 2 Days Ago |
| Questions Answered: | 66690 |
| Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
For this programming assignment you are to deliver an implementation of a Java class named StringBinaryTree that adheres to theStringBinaryTree API specification provided. Note that you may, but need not, extend the published API to include apublic static void main(String[] args) method to be used for stand-alone testing and demonstration.
Your submission should be a demonstration of your best effort at this time. Aim for code that is easy to read as well as correct. Your submission must match the syntactic specification of the API. Note that this assignment has a companion Virtual Programming Lab (VPL) activity that is required and which performs basic checks (file naming, compilation, API, style convention):String Binary Tree - VPL
You must submit a single Java source code file whose name is StringBinaryTree.java
Â
###################################################
StringBinaryTree API Specification
StringBinaryTree API
Â
Class StringBinaryTree
public class StringBinaryTreeextends Object
A simple binary tree whose root holds a non-null String value.
The StringBinaryTree datatype is defined recursively as being comprised of a value of type String, a reference to a left child of type StringBinaryTree, and a reference to a right child of type StringBinaryTree. As this datatype represents a binary tree, there must be no duplicate references nor any references to the root.
The empty tree is distinguished by the following properties:
numberOfNodes() == 0,
isEmpty() == true, and
height() == -1.
Terminology:
|
Constructor Summary |
|
|
StringBinaryTree() |
 |
|
StringBinaryTree(String rootvalue) |
 |
|
StringBinaryTree(String rootvalue,StringBinaryTree leftchild,StringBinaryTree rightchild) |
 |
Â
|
Method Summary |
|
|
StringBinaryTree |
getLeftChild() |
|
StringBinaryTree |
getRightChild() |
|
String |
getValue() |
|
int |
height() |
|
boolean |
isEmpty() |
|
boolean |
isLeaf() |
|
int |
numberOfLeaves() |
|
int |
numberOfNodes() |
|
void |
setLeftChild(StringBinaryTree child) |
|
void |
setRightChild(StringBinaryTree child) |
|
void |
setValue(String value) |
|
String |
toString() |
Â
|
Methods inherited from class |
|
clone,equals,finalize,getClass,hashCode,notify,notifyAll,wait,wait,wait |
Â
|
Constructor Detail |
StringBinaryTree
public StringBinaryTree()
Construct an empty tree.
StringBinaryTree
public StringBinaryTree(String rootvalue)
throwsIllegalArgumentException
Construct a tree with no children whose value is specified by the parameter.
Parameters:
rootvalue - the value stored at the root of the tree
Throws:
IllegalArgumentException - if parameter is null
StringBinaryTree
public StringBinaryTree(String rootvalue,
StringBinaryTree leftchild,
StringBinaryTree rightchild)
throwsIllegalArgumentException
Construct a tree with specified value, left child, and right child.
Parameters:
rootvalue - the value stored at the root of the tree
leftchild - the left child of the root;null if no such child
rightchild - the right child of the root;null if no such child
Throws:
IllegalArgumentException - ifrootvalue parameter is null
|
Method Detail |
getLeftChild
publicStringBinaryTree getLeftChild()
throwsNullPointerException
Return the left child of this tree.
Returns:
the left child; null if no such child
Throws:
NullPointerException - if this tree is empty
getRightChild
publicStringBinaryTree getRightChild()
throwsNullPointerException
Return the right child of this tree.
Returns:
the right child; null if no such child
Throws:
NullPointerException - if this tree is empty
getValue
publicString getValue()
throwsNullPointerException
Return the value of the root of this tree.
Returns:
the value of the root
Throws:
NullPointerException - if this tree is empty
height
public final int height()
Utility to determine the height of this tree; -1 if empty tree.
Returns:
the height of this tree, -1 if empty
isEmpty
public boolean isEmpty()
Empty tree predicate.
Returns:
true if this is an empty tree;false otherwise
isLeaf
public boolean isLeaf()
throwsNullPointerException
Leaf predicate.
Returns:
true if this is a leaf;false otherwise.
Throws:
NullPointerException - if this tree is empty
numberOfLeaves
public int numberOfLeaves()
throwsNullPointerException
Utility to determine the number of leaves of this tree.
Returns:
the number of leaves
Throws:
NullPointerException - if this tree is empty
numberOfNodes
public int numberOfNodes()
Return the number of nodes in this tree; 0 if empty tree.
Returns:
the number of nodes
setLeftChild
public void setLeftChild(StringBinaryTree child)
throwsNullPointerException
Replace the left child of the root of this tree.
Parameters:
child - the new left child for this tree
Throws:
NullPointerException - if this tree is empty
setRightChild
public void setRightChild(StringBinaryTree child)
throwsNullPointerException
Replace the right child of the root of this tree.
Parameters:
child - the new right child for this tree
Throws:
NullPointerException - if this tree is empty
setValue
public void setValue(String value)
throwsNullPointerException
Modify the value of the root of this tree.
Parameters:
value - the new value for the root
Throws:
NullPointerException - if this tree is empty
toString
publicString toString()
Render tree as a string.
Overrides:
toString in classObject
Returns:
string rendering of this object
Â
Here is the StringBinaryTree.java file
Â
1 // StringBinaryTree.java
2 /**
3 * A simple binary tree whose root holds a non-null String value.
4 *
Â
5 * The StringBinaryTree datatype is defined recursively as
6 * being comprised of a value of type String,
7 * a reference to a left child of type StringBinaryTree, and
8 * a reference to a right child of type StringBinaryTree.
9 * As this datatype represents a binary tree,
10 * there must be no duplicate references nor any references
11 * to the root.
12 *
13 *
Â
14 * The empty tree is distinguished by the following properties:
15 * numberOfNodes() == 0,
16 * isEmpty() == true, and
17 * height() == -1.
18 *
19 *
Terminology:
20 *
21 * great-grandparent, etc.
22 *
23 * great-grandchild, etc.
24 *
25 *
26 *
27 *
28 *
29 *
30 * @author YOURNAMEHERE
31 * @version YOURVERSIONHERE
32 */
33 public class StringBinaryTree {
34
35 }
Hel-----------lo -----------Sir-----------/Ma-----------dam-----------Tha-----------nk -----------You----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------and----------- ac-----------qui-----------sit-----------ion----------- of----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n.P-----------lea-----------se -----------pin-----------g m-----------e o-----------n c-----------hat----------- I -----------am -----------onl-----------ine----------- or----------- in-----------box----------- me----------- a -----------mes-----------sag-----------e I----------- wi-----------ll