Question
*****Please help using JAVA and show the OUTPUT I have also attached the main string******* Problem: Complete intcoll6 as discussed in class and run our
*****Please help using JAVA and show the OUTPUT
I have also attached the main string*******
Problem:
Complete intcoll6 as discussed in class and run our intcoll6client using intcoll6. In this 6th version, intcoll6, the integers in a collection are stored in a binary search tree whose name is the private member c as discussed in class (using the inner class btNode). Also, howmany is still a private member.
// The inner class for btNode
private static class btNode
{
private int info;
private btNode left, right;
public btNode() { info=0; left=right=null; }
public btNode(int i, btNode lt, btNode rt) { info=i; left=lt; right=rt; }
}
***********
import java.util.*;
import java.io.*;
public class Intcoll6
{
private int howmany;
private btNode c;
public Intcoll6()
{c = null; howmany = 0;}
public Intcoll6(int i)
{c = null; howmany = 0;}
private static btNode copytree(btNode t)
{
btNode root=null;
if (t!=null)
{
root=new btNode();
root.info=t.info;
root.left=copytree(t.left);
root.right=copytree(t.right);
}
return root;
}
public void copy(Intcoll6 obj)
{
if (this!=obj)
{
howmany=obj.howmany;
c=copytree(obj.c);
}
}
public void insert(int i)
{
if (i>0)
{
btNode pred=null, p=c;
while ((p!=null)&&(p.info!=i))
{
pred=p;
if (p.info>i) p=p.left;
else p=p.right;
}
if (p==null)
{
howmany++; p=new btNode(i, null, null);
if (pred!=null)
{
if (pred.info>i) pred.left=p;
else pred.right=p;
}
else c=p;
}
}
}
public void omit(int i)
{
// Missing
}
public boolean belongs(int i)
{
btNode p=c;
while ((p!=null)&&(p.info!=i))
{
if (p.info>i) p=p.left;
else p=p.right;
}
return (p!=null);
}
public int get_howmany() {return howmany;}
public void print()
{
printtree(c);
}
public boolean equals(Intcoll6 obj)
{
int j = 0; boolean result = (howmany==obj.howmany);
if (result)
{
//Missing
}
return result;
}
private static void printtree(btNode t)
{
if (t!=null)
{
printtree(t.left);
System.out.println(t.info);
printtree(t.right);
}
}
private static int toarray(btNode t, int[] a, int i)
{
int num_nodes=0;
if (t!=null)
{
num_nodes=toarray(t.left, a, i);
a[num_nodes+i]=t.info;
num_nodes=num_nodes+1+toarray(t.right, a, num_nodes+i+1);
} return num_nodes;
}
private static class btNode
{
int info; btNode left; btNode right;
private btNode(int s, btNode lt, btNode rt)
{
info=s; left=lt; right=rt;
}
private btNode()
{
info=0; left=null; right=null;
}
}
}
//*********************************************************************
import java.util.*;
import java.io.*;
public class BSTIntcollTest
{
public static void main(String[] args)
{
System.out.println("Enter # of random integers to be generated");
Scanner keyboard=new Scanner(System.in);
int n=keyboard.nextInt();
Intcoll6 BST=new Intcoll6(n);
Random gen=new Random(); long lapse=0, begin, end;
int k=0;
while (k { int i=5*(gen.nextInt(n)+1); begin=System.currentTimeMillis(); BST.insert(i); end=System.currentTimeMillis(); lapse=lapse+(end-begin); k++; } System.out.println("The # of integers in BST is "+BST.get_howmany()); System.out.println("Time in milliseconds "+lapse); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started