(Solved Homework): Please respond the correct questions. Explicitly I want answers fro c, d, e !!!

Please respond the correct questions. Explicitly I want answers fro c, d, e !!!

1. BST, 40 pts The purpose of this questions is to observe the behavior of binary search trees When adding random integers. You have already given a code for binary search tree (see zip file BST) a Add the function insert (int id) to the BST class. b Add a function height(Node x) to the BST class. This function will compute the height of a tree rooted at Node x. c Add a function function search Next (int k) to the BST class that find smallest id that is greater than k in the BST d et n denote the number of nodes Construct binary search trees for n 10, n 100, n 500, n 1000, n 2000, n 5000, n 10000 n 100000, n 1million. For each n you will pick uniformly random keys in the range 31 2 31 1 For each n repeat the experiment several time and calculate the average height of the tree for each n e Compare the average height to loga (n 1) 1 for each n. Calculate constants that relate the average height to log2(n 1) 1 for each n Is there any relationship betweens constants for each nHere is my code for a and b !

package BST;

public class BinarySearchTree {
public static Node root;

public BinarySearchTree() { // Constructor
this.root = null;
}

public boolean search(int id) {
Node current = root;
while (current != null) {
if (current.key == id) {
return true;
} else if (current.key > id) {
current = current.left;
} else {
current = current.right;
}
}
return false;
}

/** The insert method was added
*
* @param id integer.
*/
public void insert(int id) {

Node newNode = new Node(id);

if (root == null) {

root = newNode;

return;

}

Node current = root;

Node parent = null;

while (true) {

parent = current;

if (id < current.key) {

current = current.left;

if (current == null) {

parent.left = newNode;

return;

}

} else {

current = current.right;

if (current == null) {

parent.right = newNode;

return;

}

}

}

}

private int HeightUtil(Node root) {

if (root == null)

return -1;

else

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

}

public double height() { // Compute the height of Binary Search

return HeightUtil(root);

}

public void InorderTraversal() {

inOrderHelper(root);

}

private void inOrderHelper(Node root) {

if (root != null) {

inOrderHelper(root.left);

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

inOrderHelper(root.right);

}

}

public static void main(String arg[]) {

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

BinarySearchTree bst = new BinarySearchTree();

for (Integer n : a)

bst.insert(n);

bst.InorderTraversal();

}

}

Another Node class

package BST;
public class Node{
int key;
Node left;
Node right;

public Node(int data){
key = data;
left = null;
right = null;
}
}

Expert Answer

 Here is the answer for the question. Please do not forget to rate the answer if it helped. Thank you very much.

part c

package BST;

public class BinarySearchTree {

public static Node root;

public BinarySearchTree() { // Constructor

this.root = null;

}

public boolean search(int id) {

Node current = root;

while (current != null) {

if (current.key == id) {

return true;

} else if (current.key > id) {

current = current.left;

} else {

current = current.right;

}

}

return false;

}

/** The insert method was added

*

* @param id integer.

*/

public void insert(int id) {

Node newNode = new Node(id);

if (root == null) {

root = newNode;

return;

}

Node current = root;

Node parent = null;

while (true) {

parent = current;

if (id < current.key) {

current = current.left;

if (current == null) {

parent.left = newNode;

return;

}

} else {

current = current.right;

if (current == null) {

parent.right = newNode;

return;

}

}

}

}

private int HeightUtil(Node root) {

if (root == null)

return -1;

else

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

}

public double height() { // Compute the height of Binary Search

return HeightUtil(root);

}

public void InorderTraversal() {

inOrderHelper(root);

}

private void inOrderHelper(Node root) {

if (root != null) {

inOrderHelper(root.left);

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

inOrderHelper(root.right);

}

}

 

public Integer search_Next(Integer k)

{

if(root == null)

return null;

else

{

Node current = root;

while(current != null)

{

if(current.key == k) //found a matching key

{

//now go to the left most node in the right subtree

if(current.right == null) //there is right subtree?

return null;

else

{

current = current.right;

while(current.left != null) //no more left node?

{

current = current.left;

}

return current.key;

}

}

else if(k < current.key)

current = current.left;

else

current = current.right;

}

return null;//if no matching key was found

}

}

 

public static void main(String arg[]) {

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

BinarySearchTree bst = new BinarySearchTree();

for (Integer n : a)

bst.insert(n);

bst.InorderTraversal();

System.out.println(“nsearch_Next(2) = “+bst.search_Next(2));

}

}

output

1 2 4 5 7

search_Next(2) = 4

part D

package BST;

import java.util.Random;

public class CompareHeights {

private static void generateAndCompare(int nodeSize)

{

Random numRandom=new Random();

Random signRandom = new Random();

BinarySearchTree bst = new BinarySearchTree();

int num,sign;

 

System.out.println(“Generating tree with “+nodeSize +” nodes”);

for(int i = 0; i < nodeSize; i++)

{

sign = signRandom.nextInt(2);

 

num = numRandom.nextInt(Integer.MAX_VALUE);

 

if(sign == 0)//whenever sign is 0 , make the number -ve

num = -num;

 

bst.insert(num);

}

System.out.println(“Generated tree , its height is “+bst.height());

 

double h = Math.log(nodeSize + 1 ) / Math.log(2); //calculating log base 2

System.out.println(“Expected minimum height of tree is log (” + (nodeSize+1) + “) = ” + h);

System.out.println(“—————“);

 

}

public static void main(String[] args) {

int n[] = {10, 100, 500, 1000, 2000, 5000, 10000, 100000, 1000000};

for(int i = 0; i<n.length; i++)

{

generateAndCompare(n[i]);

}

}

}

output

Generating tree with 10 nodes

Generated tree , its height is 6.0

Expected minimum height of tree is log (11) = 3.4594316186372978

—————

Generating tree with 100 nodes

Generated tree , its height is 13.0

Expected minimum height of tree is log (101) = 6.6582114827517955

—————

Generating tree with 500 nodes

Generated tree , its height is 20.0

Expected minimum height of tree is log (501) = 8.968666793195208

—————

Generating tree with 1000 nodes

Generated tree , its height is 19.0

Expected minimum height of tree is log (1001) = 9.967226258835993

—————

Generating tree with 2000 nodes

Generated tree , its height is 24.0

Expected minimum height of tree is log (2001) = 10.966505451905741

—————

Generating tree with 5000 nodes

Generated tree , its height is 30.0

Expected minimum height of tree is log (5001) = 12.288000889707574

—————

Generating tree with 10000 nodes

Generated tree , its height is 30.0

Expected minimum height of tree is log (10001) = 13.287856641840545

—————

Generating tree with 100000 nodes

Generated tree , its height is 39.0

Expected minimum height of tree is log (100001) = 16.60965490131509

—————

Generating tree with 1000000 nodes

Generated tree , its height is 47.0

Expected minimum height of tree is log (1000001) = 19.931570012018494

—————

part E

The minimum height of a BST with n nodes is log (n+1). So we see from the output above that the height of generated tree is atleast as much as log(n+1)

iWriteHomework
Order NOW for a 10% Discount
Pages (550 words)
Approximate price: -

Why Us?

Top Quality and Well-Researched Papers

All ourbpapers are written from scratch. In fact, Clients who ask for paraphrasing services are highly discouraged. We have writers ready to craft any paper from scratch and deliver quality ahead of time.

Professional and Experienced Academic Writers

Our writers keeps you posted on your papers progress - providing you with paper outline/draft. You are also at liberty to communicate directly with your writer.

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 many cases quite ahead of 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.

24/7 Customer Support

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

Place your order

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.

Essays

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.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents