Fullness Experiment Part 1: in JAVA!

a) design and implement a method height for BinarySearchTree that returns the height of the tree.

b) Define the fullness ratio of a binary tree to be the ratio between its minimum height and its height (given the number of nodes in the tree). For example, the tree in Figure 7.5a has a fullness ratio of 1.00 (its minimum height is 3 and its height is 3) and the tree in Figure 7.6c has a fullness ratio of 0.33 (its minimum height is 3 and its height is 9). Implement a method fRatio to be added to the BinarySearchTree class that returns the fullness ratio of the tree.

c) Create an application that generates 10 “random” trees, each with 1,000 nodes (random integers between 1 and 3,000). For each tree output its height, optimal height, and fullness ratio.

Binary Search and Binary Node below:

public class **BinaryTreeNode**<T extends Comparable<T>> {

public BinaryTreeNode<T> left; // the left child

public BinaryTreeNode<T> right; // the right child

public T data; // the data in this node

public BinaryTreeNode() {

this(null, null, null);

}

public BinaryTreeNode(T theData) {

this(theData, null, null);

}

public BinaryTreeNode(T theData, BinaryTreeNode<T> leftChild, BinaryTreeNode<T> rightChild) {

data = theData;

left = leftChild;

right = rightChild;

}

public T getData() {

return data;

}

public BinaryTreeNode<T> getLeft() {

return left;

}

public BinaryTreeNode<T> getRight() {

return right;

}

public void setLeft(BinaryTreeNode<T> newLeft) {

left = newLeft;

}

public void setRight(BinaryTreeNode<T> newRight) {

right = newRight;

}

public void setData(T newData) {

data = newData;

}

public void preOrder() {

System.out.println(data);

if (left != null) {

left.preOrder();

}

if (right != null) {

right.preOrder();

}

}

public int height() {

int leftHeight = 0; // Height of the left subtree

int rightHeight = 0; // Height of the right subtree

int height = 0; // The height of this subtree

// If we have a left subtree, determine its height

if (left != null) {

leftHeight = left.height();

}

// If we have a right subtree, determine its height

if (right != null) {

rightHeight = right.height();

}

// The height of the tree rooted at this node is one more than the

// height of the ‘taller’ of its children.

if (leftHeight > rightHeight) {

height = 1 + leftHeight;

} else {

height = 1 + rightHeight;

}

// Return the answer

return height;

}

/**

* @param pathString

* @return the tree nodes in pre-order traversal

*/

public String toStringPreOrder(String pathString) {

String treeString = pathString + ” : ” + data + “n”;

if (left != null) {

treeString += left.toStringPreOrder(pathString + “L”);

}

if (right != null) {

treeString += right.toStringPreOrder(pathString + “R”);

}

return treeString;

}

}

====================

import java.util.*;

public class BinarySearchTree<T extends Comparable<T>> {

private BinaryTreeNode<T> root;

public BinarySearchTree() {

root = null;

comparator = null;

}

public <T extends Comparable<T>> boolean Search(Collection<T> col ,T Target){

return true;

}

public static void main(String[] args) {

Integer[] a = { 1, 5, 2, 7, 4 };

BinarySearchTree<Integer> bst = new BinarySearchTree<Integer>();

for (Integer n : a)

bst.insert(n);

System.out.println(“InOrder Traversal is”);

bst.InorderTraversal();

System.out.println();

System.out.println(“Preorder Traversal is”);

bst.PreorderTraversal();

System.out.println();

System.out.println(“Height is :” + bst.height());

System.out.println(“Height is :” + bst.heightWithoutRecursion());

System.out.println(“Left Count is: ” + bst.LeafCount());

System.out.println(“Is the element Present: ” + bst.find(2));

System.out.println();

}

public int LeafCount() {

return LeafCount(root);

}

private int LeafCount(BinaryTreeNode<T> root) {

if (root == null)

return 0;

else if (root.left == null && root.right == null)

return 1;

else

return LeafCount(root.left) + LeafCount(root.right);

}

private Comparator<T> comparator;

private int compare(T x, T y) {

if (comparator == null)

return x.compareTo(y);

else

return comparator.compare(x, y);

}

public void insert(T data) {

root = insert(root, data);

}

private BinaryTreeNode<T> insert(BinaryTreeNode<T> root, T toInsert) {

if (root == null)

return new BinaryTreeNode<T>(toInsert);

if (compare(toInsert, root.getData()) == 0)

return root;

if (compare(toInsert, root.getData()) <= 0)

root.left = insert(root.getLeft(), toInsert);

else

root.right = insert(root.right, toInsert);

return root;

}

public int getSizeUtil() {

return getSize(root);

}

public int getSize(BinaryTreeNode<T> root) {

if (root == null)

return 0;

else

return 1 + getSize(root.getRight()) + getSize(root.getLeft());

}

public void SearchUtil(T key) {

List<T> list = new ArrayList<T>();

Search(root, key, list);

System.out.println(list.size());

}

public void Search(BinaryTreeNode<T> root, T key, List<T> list) {

if (root == null)

return;

if (root.data.compareTo(key) == 0) {

list.add(root.data);

} else if (root.data.compareTo(key) <= 0)

Search(root.getLeft(), key, list);

else

Search(root.getRight(), key, list);

}

public void InorderTraversal() {

inOrderHelper(root);

}

private void inOrderHelper(BinaryTreeNode<T> root) {

if (root != null) {

inOrderHelper(root.getLeft());

System.out.print(root.getData() + ” “);

inOrderHelper(root.getRight());

}

}

public int height() {

return height(root);

}

private int height(BinaryTreeNode<T> root) {

if (root == null)

return -1;

else

return 1 + Math.max(height(root.left), height(root.right));

}

public int heightWithoutRecursion() {

return heightWithoutRecursion(root);

}

int heightWithoutRecursion(BinaryTreeNode<T> node)

{

if (node == null)

return 0;

Queue<BinaryTreeNode> q = new LinkedList();

q.add(node);

int height = -1;

while (1 == 1)

{

int nodeCount = q.size();

if (nodeCount == 0)

return height;

height++;

while (nodeCount > 0)

{

BinaryTreeNode newnode = q.peek();

q.remove();

if (newnode.left != null)

q.add(newnode.left);

if (newnode.right != null)

q.add(newnode.right);

nodeCount–;

}

}

}

public void PreorderTraversal() {

preOrderHelper(root);

}

private void preOrderHelper(BinaryTreeNode root) {

if (root != null) {

System.out.print(root.data + ” “);

preOrderHelper(root.left);

preOrderHelper(root.right);

}

}

public boolean find(T key) {

return find(root, key);

}

private boolean find(BinaryTreeNode<T> root, T key) {

if (root == null)

return false;

else if (compare(key, root.data) == 0)

return true;

else if (compare(key, root.data) < 0)

return find(root.left, key);

else

return find(root.right, key);

}

}