Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
43 views

Package: .Java - Representation of A Generic Node For Use W Withab Binary o Object

This document contains code for testing a binary tree data structure implemented using generic types in Java. It defines classes for a tree node (TreeNode), binary tree (Tree), and test class (TreeTest). The test class creates linked lists of different data types, fills them with random values, constructs trees from the lists, and performs various tree operations like search, traversal, cloning to test the tree implementation.

Uploaded by

sans
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Package: .Java - Representation of A Generic Node For Use W Withab Binary o Object

This document contains code for testing a binary tree data structure implemented using generic types in Java. It defines classes for a tree node (TreeNode), binary tree (Tree), and test class (TreeTest). The test class creates linked lists of different data types, fills them with random values, constructs trees from the lists, and performs various tree operations like search, traversal, cloning to test the tree implementation.

Uploaded by

sans
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

/** required package class namespace */

package collections;

/** required imports */


import java.
.io.Serializable;

/**
.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
{

/** the class data property */


public T data;
/** self reference to the left leaf node object */
public TreeNode left;
/** self reference to the right leaf node object */
public TreeNode right;

/** default constructor */


public TreeNode() {
this(
(null,null,null);
}

/**
* 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;

/** required imports */


import java.
.io.Serializable;

/**
.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
{

/** class properties */


private TreeNode<T> root;
private LinkedList<T> list;
private LinkedList<T> order;

/** default constructor */


public Tree() {
root = null;
list = new LinkedList<T>();
order = new LinkedList<T>();
}

/**
* 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
{

/** default constructor */


public TreeTest() {
System.
.out.println("\nTree Testing Starting..........\n");
// size used for testing
int size = 10;
// create 6 lists (different data types)
LinkedList<
<Boolean> list1 = new LinkedList<>();
LinkedList<
<Integer> list2 = new LinkedList<>();
LinkedList<
<Double> list3 = new LinkedList<>();
LinkedList<
<Character> list4 = new LinkedList<>();
LinkedList<
<String> list5 = new LinkedList<>();
LinkedList<
<SubTestClass> list6 = new LinkedList<>();
// fill the lists with random values
for (int i = 0; i < size; i++) {
list1.add(Random.generate());
list2.add(Random.generate(0,size-11));
list3.add(Random.generate(0d,((double)((size-
-1)));
list4.add(Random.generate('a',,'z'));
list5.add(Random.generate(5));
list6.add(new SubTestClass());
}
// output the lists
System.
.out.pprintln(
(list1)
);
System.
.out.pprintln(
(list2)
);
System.
.out.pprintln(
(list3)
);
System.
.out.pprintln(
(list4)
);
System.
.out.pprintln(
(list5)
);
System.
.out.pprintln(
(list6)
);
// create 6 Trees from the lists (matching data types)
Tree<<Boolean> tree1 = new Tree<>(list1);
Tree<<Integer> tree2 = new Tree<>(list2);
Tree<<Double> tree3 = new Tree<>(list3);
Tree<<Character> tree4 = new Tree<>(list4);
Tree<<String> tree5 = new Tree<>(list5);
Tree<<SubTestClass> tree6 = new Tree<>(list6);
// output the Trees
System..out.p
println(("Tree 1: " + tree1);
System..out.p
println(("Tree 2: " + tree2);
System..out.p
println(("Tree 3: " + tree3);
System..out.p
println(("Tree 4: " + tree4);
System..out.p
println(("Tree 5: " + tree5);
System..out.p
println(("Tree 6: " + tree6);
// make new Trees from clones of the old Trees
Tree<<Boolean> tree7 = tree1.clone();
Tree<<Integer> tree8 = tree2.clone();
Tree<<Double> tree9 = tree3.clone();
Tree<<Character> tree10 = tree4.clone();
Tree<<String> tree11 = tree5.clone();
Tree<<SubTestClass> tree12 = tree6.clone();
// output the new Trees
System..out.p
println(("Tree 7: " + tree7);
System..out.p
println(("Tree 8: " + tree8);
System..out.p
println(("Tree 9: " + tree9);
System..out.p
println(("Tree 10: " + tree10);
System..out.p
println(("Tree 11: " + tree11);
System..out.p
println(("Tree 12: " + tree12);
// compare them to be equal (or not)
boolean result = true; // assume they are all equal
if (!tree1.equals(tree7)) result = false;
if (!tree2.equals(tree8)) result = false;
if (!tree3.equals(tree9)) result = false;
if (!tree4.equals(tree10)) result = false;
if (!tree5.equals(tree11)) result = false;
if (!tree6.equals(tree12)) result = false;
if (result) System.out.p println("All trees equal"));
else System.out.p
println("All trees not equal"));
// insert random values into the trees
for (int i = 0; i < size; i++) {
tree1.insert(Random.generate());
tree2.insert(Random.generate(00,size--1));
tree3.insert(Random.generate(00d,
,(double))(size-
-1)));
tree4.insert(Random.generate(''a','z'));
tree5.insert(Random.generate(5));
tree6.insert(new SubTestClass());
}
// compare them to be equal (or not)
result = true; // assume they are all equal
if (!tree1.equals(tree7)) result = false;
if (!tree2.equals(tree8)) result = false;
if (!tree3.equals(tree9)) result = false;
if (!tree4.equals(tree10)) result = false;
if (!tree5.equals(tree11)) result = false;
if (!tree6.equals(tree12)) result = false;
if (result) System.out.pprintln("All trees equal") );
else System.out.p
println("All trees not equal"));
// output all trees
System.
.out.p
println(("Tree 1: " + tree1);
System.
.out.p
println(("Tree 2: " + tree2);
System.
.out.p
println(("Tree 3: " + tree3);
System.
.out.p
println(("Tree 4: " + tree4);
System.
.out.p
println(("Tree 5: " + tree5);
System.
.out.p
println(("Tree 6: " + tree6);
System.
.out.p
println(("Tree 7: " + tree7);
System.
.out.p
println(("Tree 8: " + tree8);
System.
.out.p
println(("Tree 9: " + tree9);
System.
.out.p
println(("Tree 10: " + tree10);
System.
.out.p
println(("Tree 11: " + tree11);
System.
.out.p
println(("Tree 12: " + tree12);
// search for items in the tree
int location = Random.generate(0,ssize-
-1);
System.
.out.p
println(("Search 1: " + tree1.search(list1.get(location)));
System.
.out.p
println(("Search 2: " + tree2.search(list2.get(location)));
System.
.out.p
println(("Search 3: " + tree3.search(list3.get(location)));
System.
.out.p
println(("Search 4: " + tree4.search(list4.get(location)));
System.
.out.p
println(("Search 5: " + tree5.search(list5.get(location)));
System.
.out.p
println(("Search 6: " + tree6.search(list6.get(location)));
// search for items possibly in the tree
System.
.out.p
println(("Search 7: " +
tree7.search(Random.generate()));
System.
.out.p
println(("Search 8: " +
tree8.search(Random.generate(00,size--1)));
System.
.out.p
println(("Search 9: " +
tree9.search(Random.generate(00d,,(double)
)(size-
-1))));
System.
.out.p
println(("Search 10: " +
tree10.search(Random.generate(' 'a','z')));
System.
.out.p
println(("Search 11: " +
tree11.search(Random.generate(5)));
System.
.out.p
println(("Search 12: " +
tree12.search(new SubTestClass()));
System.
.out.println("\nTree Testing Ending............\n");
}

You might also like