# Question: Fullness Experiment Part 1: in JAVA! a) design and implement a method height for BinarySearchTree……

Fullness Experiment Part 1: in JAVA!

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

Don't use plagiarized sources. Get Your Custom Essay on
Question: Fullness Experiment Part 1: in JAVA! a) design and implement a method height for BinarySearchTree……
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!

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) {

} 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();

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)

if (newnode.right != null)

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);

}

}

Solution-
Method to calculate height of tree

int Height(Node node)

{

if (node == null)

return 0;

else

{

/* compute the depth of each subtree */

int lDepth = maxDepth(node.left);

int rDepth = maxDepth(node.right);

/* use the larger one */

if (lDepth > rDepth)

return (lDepth + 1);

else

return (rDepth + 1);

}

}

Order NOW For a 10% Discount!
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

## Calculate the price of your order

Total price:
\$0.00

How it works?

Follow these simple steps to get your paper done

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

## Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.