Package: .Java - Representation of A Generic Node For Use W Withab Binary o Object
Package: .Java - Representation of A Generic Node For Use W Withab Binary o Object
package collections;
/**
.java - representation of a generic node for use with a binary
* TreeNode.
* tree object
* @author Mr. Wachs
* @param <T> the generic object used in this class
* @since Feb 9, 2017
* @instructor Mr. Wachs
*/
public class TreeNode <T extends Comparable<T>> implements Serializable
{
/**
* class constructor sets class property data
* @param data the generic data for the class
*/
public TreeNode(T data) {
this(
(data,null,null);
}
/**
* class constructor sets class property data
* @param data the generic data for the class
* @param left reference to the left sub-tree
* @param right reference to the right sub-tree
*/
public TreeNode(T
T data,
, TreeNode left, TreeNode right) {
this.
.data = data;
this.
.left = left;
this.
.right = right;
}
/**
* insets data (recursively) into the binary tree at its correct position
* @param data the generic data to insert into the tree
* @return the operation was successful (true) or not (false)
*/
public boolean insert(TT data)
) {
if (data == null) return false;
if (data.compareTo(this.data) < 0) {
if (this.left == null) this.left = new TreeNode<T>(data);
else this.left.insert(data);
}
else if (data.compareTo(this.data) >= 0) {
if (this.right == null) this.right = new TreeNode<T>(data);
else this.right.insert(data);
}
return true;
}
/**
* string representation of this object
* @return the string version of this object
*/
@Override
public String toString() {
return data.toString();
}
/**
* determine if 2 objects are "equal" in this context
* @param treeNode the object to compare to
* @return equal (t
true)
) or not (false)
*/
public boolean equals(TTreeNode treeNode) {
return data.equals(treeNode.data);
}
/**
* returns a duplicate object
* @return a shallow clone of the object
*/
public TreeNode clone() {
TreeNode treeNode = new TreeNode<T>(this.data);
return treeNode;
}
}
/** required package class namespace */
package collections;
/**
.java - a representation of a binary tree ADT object
* Tree.
* @author Mr. Wachs
* @param <T> the generic object used in this class
* @since Feb 9, 2017
* @instructor Mr. Wachs
*/
public class Tree <T extends Comparable<T>> implements Serializable
{
/**
* constructor builds from
m a linked
d list object
* @param linkedList the linked list object to use
*/
public Tree(LLinkedList linkedList) {
root = null;
list = new LinkedList<T>();
order = new LinkedList<T>();
for (int i = 0; i < linkedList.size(); i++) {
T data = (T)linkedList.get(i);
insert(data);
}
}
/**
* inserts data recursively into the tree in order
* @param data the data type to insert
*/
public void insert(T data) {
order.
.add(data);
if (root == null) root = new TreeNode(data);
else root.insert(data);
}
/**
* searches the tree to see if the data exists in the tree
* @param data the data to search for
* @return data has been found (true) or not (false)
*/
public boolean search(T data) {
return recursiveSearch(data,root);
}
/**
* recursive search starting at the root of the tree
* @param data the data to search for
* @param current the current tree node reference
* @return found (ttrue)
) or not (false)
*/
private boolean recursiveSearch(T T data, TreeNode current)
) {
if (current == null)
return false;
else if (data.compareTo((T)current.data) < 0)
return recursiveSearch(data,current.left);
else if (data.compareTo((T)current.data) > 0)
return recursiveSearch(data,current.right);
else
return true;
}
/**
* the string representation of this object
* @return the object represented as a string
*/
@Override
public String toString()) {
String output = "";
try {
output = (T)
(root.data).getClass().getComponentType().getSimpleName()
+ " Tree traversals:";
}
catch((NullPointerException error) {
output = "Tree traversals:";
}
output += "\n(1) Pre-Order: " + preOrder().toString();
output += "\n(2) Post-Order: " + postOrder().toString();
output += "\n(3) In-Order: " + inOrder().toString();
return output;
}
/**
* a pre-order traversal of the tree nodes
* @return a linked list containing all the data in pre-order
*/
public LinkedList preOrder() {
list = new LinkedList<T>();
preOrderRecursive(root);
return list;
}
/**
* a post-order traversal of the tree nodes
* @return a linked list containing all the data in post-order
*/
public LinkedList postOrder() {
list = new LinkedList<T>();
postOrderRecursive(root);
return list;
}
/**
* a in-order traversal of the tree nodes
* @return a linked list containing all the data in-order
*/
public LinkedList inOrder() {
list = new LinkedList<T>();
inOrderRecursive(root);
return list;
}
/**
* recursive pre-order traversal starting at the root
* @param current the reference to the current tree node
*/
private void preOrderRecursive(TTreeNode<T
T> current)
) {
if (current == null) return; // base case
list.
.add((T)current.data);
preOrderRecursive(current.left);
preOrderRecursive(current.right);
}
/**
* recursive post-order traversal starting at the root
* @param current the reference to the current tree node
*/
private void postOrderRecursive(TTreeNode<T
T> current)
) {
if (current == null) return; // base case
postOrderRecursive(current.left);
postOrderRecursive(current.right);
list.
.add((T)current.data);
}
/**
* recursive in-order traversal starting at the root
* @param current the reference to the current tree node
*/
private void inOrderRecursive(T TreeNode<T
T> current)
) {
if (current == null) return; // base case
inOrderRecursive(current.left);
list.
.add((T)current.data);
inOrderRecursive(current.right);
}
/**
* determine if 2 objects are "equal" in this context
* @param object the object to compare to
* @return equal (t
true)
) or not (false)
*/
public boolean equals(OObject object) {
LinkedList<
<T> inOrder1 = this.inOrder();
LinkedList<
<T> inOrder2 = ((Tree)object).inOrder();
return inOrder1.equals(inOrder2);
}
/**
* returns a duplicate object
* @return a deep clone of the object
*/
public Tree clone() {
Tree<
<T> tree = new Tree<>();
for (int i = 0; i < order.size(); i++) {
T data = (T)order.get(i);
tree.insert(data);
}
return tree;
}
}
/** required package class namespace */
package testing.
.collections;
import collections.
.LinkedList;
import collections.
.Tree;
import numbers.
.Random;
import testing.
.testclass.SubTestClass;
/**
.java - description
* TreeTest.
* @author lawrence.
.wachs
* @since Feb 9, 2017
* @version 1.0
* @instructor Mr. Wachs
*/
public class TreeTest
{