SlideShare a Scribd company logo
JAVA - Design a data structure IntSet that can hold a set of integers. Hide the private
implementation: a Binary Search Tree of Integer objects. Provide the following public methods
only.
• Constructor to make an empty set,
• Constructor to create a set by initializing the root,
• void add(int x) to add x if it is not present,
• void remove(int x) to remove x if it is present,
• void print() to print all the elements currently in the set,
• boolean contains(int x) to test whether x is present,
• IntSet copy() to create a new BTS as a copy of the current one.
Note that the root of the BST is a private data member and there is no need to define getRoot(),
and setRoot() functions. All the above functions should be defined public and should call
recursive private functions to do the task. e.g.
// remove
private Node recRemove(Node loc, int x) { // To do }
public void remove(int x) { root= recRemove(root, x) ; }
// contains
private boolean recContains(Node loc, int x) { // to do }
public boolean contains(int x) { return recContains(root, x) ; }
Complete the class IntSet by supplying the intersection and union functions. Use the same format
as above i.e. each public function calls a private recursive function to do the task:
public IntSet union(IntSet other)
public IntSet intersection(IntSet other)
Solution
import java.util.Scanner; class BinarySearchExample { public static void main(String args[]) {
int counter, num, item, array[], first, last, middle; //To capture user input Scanner input = new
Scanner(System.in); System.out.println("Enter number of elements:"); num = input.nextInt();
//Creating array to store the all the numbers array = new int[num]; System.out.println("Enter "
+ num + " integers"); //Loop to store each numbers in array for (counter = 0; counter < num;
counter++) array[counter] = input.nextInt(); System.out.println("Enter the search value:"); item
= input.nextInt(); first = 0; last = num - 1; middle = (first + last)/2; while( first <= last ) { if (
array[middle] < item ) first = middle + 1; else if ( array[middle] == item ) {
System.out.println(item + " found at location " + (middle + 1) + "."); break; } else { last =
middle - 1; } middle = (first + last)/2; } if ( first > last ) System.out.println(item + " is not
found. "); } }

More Related Content

Similar to JAVA - Design a data structure IntSet that can hold a set of integers-.docx (20)

DOCX
UNIT V.docx
Revathiparamanathan
 
PDF
Save Write a program to implement Binary search using recursive algo.pdf
arihantmobileselepun
 
PPTX
8. Recursion.pptx
Abid523408
 
PDF
Here is what I got so far, I dont know how to write union, interse.pdf
arihantpatna
 
PDF
In the binary search, if the array being searched has 32 elements in.pdf
arpitaeron555
 
PDF
UNIT IV -Data Structures.pdf
KPRevathiAsstprofITD
 
DOCX
611+tutorial
Prudence Mashile
 
PDF
Using Arrays with Sorting and Searching Algorithms1) This program .pdf
f3apparelsonline
 
PDF
package lab7 public class SetOperations public static.pdf
syedabdul78662
 
PPTX
hash
tim4911
 
PPT
Data structures using c
Prof. Dr. K. Adisesha
 
PPT
Complete binary tree and heap
Nazir Ahmed
 
PDF
sort search in C
faizankhan260690
 
PDF
I need help creating a parametized JUnit test case for the following.pdf
fonecomp
 
DOCX
object oriented programming lab manual .docx
Kirubaburi R
 
PDF
Ee693 sept2014quizgt2
Gopi Saiteja
 
PDF
4. The size of instructions can be fixed or variable. What are advant.pdf
mumnesh
 
PPT
Algorithms Binary Search recursion ppt BSIT
emmanuelsolabo07
 
PPS
02 ds and algorithm session_02
yogeshjoshi362
 
UNIT V.docx
Revathiparamanathan
 
Save Write a program to implement Binary search using recursive algo.pdf
arihantmobileselepun
 
8. Recursion.pptx
Abid523408
 
Here is what I got so far, I dont know how to write union, interse.pdf
arihantpatna
 
In the binary search, if the array being searched has 32 elements in.pdf
arpitaeron555
 
UNIT IV -Data Structures.pdf
KPRevathiAsstprofITD
 
611+tutorial
Prudence Mashile
 
Using Arrays with Sorting and Searching Algorithms1) This program .pdf
f3apparelsonline
 
package lab7 public class SetOperations public static.pdf
syedabdul78662
 
hash
tim4911
 
Data structures using c
Prof. Dr. K. Adisesha
 
Complete binary tree and heap
Nazir Ahmed
 
sort search in C
faizankhan260690
 
I need help creating a parametized JUnit test case for the following.pdf
fonecomp
 
object oriented programming lab manual .docx
Kirubaburi R
 
Ee693 sept2014quizgt2
Gopi Saiteja
 
4. The size of instructions can be fixed or variable. What are advant.pdf
mumnesh
 
Algorithms Binary Search recursion ppt BSIT
emmanuelsolabo07
 
02 ds and algorithm session_02
yogeshjoshi362
 

More from olsenlinnea427 (20)

DOCX
Assume that X is a discrete random variable that can take on the follo.docx
olsenlinnea427
 
DOCX
Assume that in an economy- people hold $1000 of currency and $4000 of.docx
olsenlinnea427
 
DOCX
Assume a Poisson distribution with -4-5- Find the following probabilit.docx
olsenlinnea427
 
DOCX
Assume a member is selected at random trom the population represented.docx
olsenlinnea427
 
DOCX
Assume a list has the following element- write a function to interch.docx
olsenlinnea427
 
DOCX
Assume a company had no jobs in progress at the beginning of July and.docx
olsenlinnea427
 
DOCX
Assignment- Peg Game Create a web page that allows the user to play th.docx
olsenlinnea427
 
DOCX
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
olsenlinnea427
 
DOCX
As part of measures for containing the Covid-19 pandemic- vaccination.docx
olsenlinnea427
 
DOCX
As hematopoietic stem cells (HSC) mature and become differentiated the.docx
olsenlinnea427
 
DOCX
As human populations & ecological footprints approach earth's carrying.docx
olsenlinnea427
 
DOCX
Aries' description of an attitude toward death he called -tame death-.docx
olsenlinnea427
 
DOCX
Apple Inc- received $350-000 as dividends last year from its investmen.docx
olsenlinnea427
 
DOCX
Anthony has been living in Western WA for 3 years- After moving from C.docx
olsenlinnea427
 
DOCX
answer the questions in Blockchain list 5 point for all of the followi.docx
olsenlinnea427
 
DOCX
Answer the following questions- Question 1- You have implemented a sec.docx
olsenlinnea427
 
DOCX
Answer the following based on a Blockchain and Biometrics I- What t.docx
olsenlinnea427
 
DOCX
Anglin Company- a manufacturing firred has supplicd the following info.docx
olsenlinnea427
 
DOCX
Angela Lopez owns and manages a consulting firm called Metrixi which b.docx
olsenlinnea427
 
DOCX
Andrew finds that on his way to work his wait time for the bus is roug.docx
olsenlinnea427
 
Assume that X is a discrete random variable that can take on the follo.docx
olsenlinnea427
 
Assume that in an economy- people hold $1000 of currency and $4000 of.docx
olsenlinnea427
 
Assume a Poisson distribution with -4-5- Find the following probabilit.docx
olsenlinnea427
 
Assume a member is selected at random trom the population represented.docx
olsenlinnea427
 
Assume a list has the following element- write a function to interch.docx
olsenlinnea427
 
Assume a company had no jobs in progress at the beginning of July and.docx
olsenlinnea427
 
Assignment- Peg Game Create a web page that allows the user to play th.docx
olsenlinnea427
 
Assignment 2 Application Case 6-5 Efficient Image Recognition and Cate.docx
olsenlinnea427
 
As part of measures for containing the Covid-19 pandemic- vaccination.docx
olsenlinnea427
 
As hematopoietic stem cells (HSC) mature and become differentiated the.docx
olsenlinnea427
 
As human populations & ecological footprints approach earth's carrying.docx
olsenlinnea427
 
Aries' description of an attitude toward death he called -tame death-.docx
olsenlinnea427
 
Apple Inc- received $350-000 as dividends last year from its investmen.docx
olsenlinnea427
 
Anthony has been living in Western WA for 3 years- After moving from C.docx
olsenlinnea427
 
answer the questions in Blockchain list 5 point for all of the followi.docx
olsenlinnea427
 
Answer the following questions- Question 1- You have implemented a sec.docx
olsenlinnea427
 
Answer the following based on a Blockchain and Biometrics I- What t.docx
olsenlinnea427
 
Anglin Company- a manufacturing firred has supplicd the following info.docx
olsenlinnea427
 
Angela Lopez owns and manages a consulting firm called Metrixi which b.docx
olsenlinnea427
 
Andrew finds that on his way to work his wait time for the bus is roug.docx
olsenlinnea427
 
Ad

Recently uploaded (20)

PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Ad

JAVA - Design a data structure IntSet that can hold a set of integers-.docx

  • 1. JAVA - Design a data structure IntSet that can hold a set of integers. Hide the private implementation: a Binary Search Tree of Integer objects. Provide the following public methods only. • Constructor to make an empty set, • Constructor to create a set by initializing the root, • void add(int x) to add x if it is not present, • void remove(int x) to remove x if it is present, • void print() to print all the elements currently in the set, • boolean contains(int x) to test whether x is present, • IntSet copy() to create a new BTS as a copy of the current one. Note that the root of the BST is a private data member and there is no need to define getRoot(), and setRoot() functions. All the above functions should be defined public and should call recursive private functions to do the task. e.g. // remove private Node recRemove(Node loc, int x) { // To do } public void remove(int x) { root= recRemove(root, x) ; } // contains private boolean recContains(Node loc, int x) { // to do } public boolean contains(int x) { return recContains(root, x) ; } Complete the class IntSet by supplying the intersection and union functions. Use the same format as above i.e. each public function calls a private recursive function to do the task: public IntSet union(IntSet other) public IntSet intersection(IntSet other)
  • 2. Solution import java.util.Scanner; class BinarySearchExample { public static void main(String args[]) { int counter, num, item, array[], first, last, middle; //To capture user input Scanner input = new Scanner(System.in); System.out.println("Enter number of elements:"); num = input.nextInt(); //Creating array to store the all the numbers array = new int[num]; System.out.println("Enter " + num + " integers"); //Loop to store each numbers in array for (counter = 0; counter < num; counter++) array[counter] = input.nextInt(); System.out.println("Enter the search value:"); item = input.nextInt(); first = 0; last = num - 1; middle = (first + last)/2; while( first <= last ) { if ( array[middle] < item ) first = middle + 1; else if ( array[middle] == item ) { System.out.println(item + " found at location " + (middle + 1) + "."); break; } else { last = middle - 1; } middle = (first + last)/2; } if ( first > last ) System.out.println(item + " is not found. "); } }