Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Description In this exercise you will implement the Set abstract data type using binary search trees. We will consider sets of integers where each integer
Description In this exercise you will implement the Set abstract data type using binary search trees. We will consider sets of integers where each integer will be stored in a tree node. Therefore, the number of nodes in the binary search tree must be equal to the size of the set. You will have to write a Java class BSTSet for this purpose. To implement the tree nodes, you must use the Java class TNade provided in this exercise. Additionaly, you may need to implement Java dasses MyStack and or MyQueue to perform non- recursive tree traversals of a BSTSet. You are not permitted to use any predefined Java methods or classes from Java APl, ather than for input and output, unless specified atherwise. You must also write a test class TestBSTSet. Please compute the asymptotic run time and space complexity of all methods Definitions .A set is an unordered collection of elements with no repetition, Examples are the set of real numbers, the set of integer numbers or the set consisting of numbers 1,2,30 For this exercise we will only be considering representing finite sets consisting of elements 4,5,890,65535], 10,1,2,.-,65534,65535. I} are all which are integers. Examples:,34,78,1000 valid sets The union of two sets, say A and B.written as AUB and is the set which contains al of the elements in either A or B or both. Example: 'f A- 3,8,14,15) and B-12,8,9,15,10 2,3,8,9,14,15,100 (notice that there are no repeated elements in a set). The intersection of two sets Aand B is written as AnB and is the set which contains the elements that are common to A and B. Examples: H A = 3,8,14,15) and B = {2,8,9,15, 1001, then A n B :15). If A = {17,20, 38} and B200), then A r B- which is termed the empty set. * , then AUB . . You must use the Java css TNode given below, to imple ment the tree nodes. Classes TNode and BSTSet must be contained in the same package pubic dass TNadel int element; TNode lett TNode right TNodelint L, TNode l, TNode r) element left- ;right-rlfend class .Class BSTSet must contain anly one private field named root, of type TNode, which is a reference to the root of the tree. No other fields are alowed. When the set is empty you should have root -null. .Class BSTSet must contain at least the following constructors o public BSTSet-initializes the BSTSet object to represent the empty set (an empty tree) o public BSTSetintl] input)-initiaites the BSTSet object to represent the set containing all elements in array input, without repettions. For example, if the array is: 5,7,4,5, the corresponding set is 15,7.4) o public boolean isInlint v: Retus true if integer v is an element of this BSTSet. It returns o public void add int vi Adds v to this BSTSet if was not already an element of this o public boolean removeint v): Removes v from this BSTSet if v was an element of this o public BSTSet union(BSTSet syRetums a new BSTSet which represents the union of this false atherwise. BSTSet. It does nothing otherwise BSTSet and returns true. Returns false if v was not an element of this BSTSet BSTSet and s. This method should not mcdify the input sets public BSTSet intersection,BSTSet s: Returns a new BSTSet which represents the intersection of this BSTSet and s. This method should not modify the input sets. o o public int sire): Returns the number af elements in this set. o public int height: Returns the height of this BSTSet. Height of empty set is -1. o public void printBSTSetfl: Outputs the elements of chis set to the console, in increasing order o private void printBSTSet TNode : Outputs to the console the elements stored in the subtree rooted int, in increasing order For the two printing methods spedified above yo u must use the following code o public waid printBSTSetDE in root-=null) System.out.printir"The set is empty else System.out.print"The set elements are: " printSTSetiroot Systemout.printn private void printBSTSet(TNode tH int !=null)( princBsTsetft leti: System.out.printt.element+ printBSTSett.right) publik vold printNonReo): Prints the htegers in this BSTSet in increasing order. This method is nonrecursive and uses a stack to implement the inorder traversal. o Notes * Compute the asymptotic run time and space complexity of all methods implemented as a function of the set size. Provide justification for your analysis Be sure to right a test class TestBSTSet to test al methods. .You are permittrd to implement other private methods inside cass BSTSet as required. However, you are not permitted to modify the class TNode. Additionally, class BSTSet must contain ony one field which is at TNoce reference named root. .Be sure to thoroughly and descriptively comment all code Description In this exercise you will implement the Set abstract data type using binary search trees. We will consider sets of integers where each integer will be stored in a tree node. Therefore, the number of nodes in the binary search tree must be equal to the size of the set. You will have to write a Java class BSTSet for this purpose. To implement the tree nodes, you must use the Java class TNade provided in this exercise. Additionaly, you may need to implement Java dasses MyStack and or MyQueue to perform non- recursive tree traversals of a BSTSet. You are not permitted to use any predefined Java methods or classes from Java APl, ather than for input and output, unless specified atherwise. You must also write a test class TestBSTSet. Please compute the asymptotic run time and space complexity of all methods Definitions .A set is an unordered collection of elements with no repetition, Examples are the set of real numbers, the set of integer numbers or the set consisting of numbers 1,2,30 For this exercise we will only be considering representing finite sets consisting of elements 4,5,890,65535], 10,1,2,.-,65534,65535. I} are all which are integers. Examples:,34,78,1000 valid sets The union of two sets, say A and B.written as AUB and is the set which contains al of the elements in either A or B or both. Example: 'f A- 3,8,14,15) and B-12,8,9,15,10 2,3,8,9,14,15,100 (notice that there are no repeated elements in a set). The intersection of two sets Aand B is written as AnB and is the set which contains the elements that are common to A and B. Examples: H A = 3,8,14,15) and B = {2,8,9,15, 1001, then A n B :15). If A = {17,20, 38} and B200), then A r B- which is termed the empty set. * , then AUB . . You must use the Java css TNode given below, to imple ment the tree nodes. Classes TNode and BSTSet must be contained in the same package pubic dass TNadel int element; TNode lett TNode right TNodelint L, TNode l, TNode r) element left- ;right-rlfend class .Class BSTSet must contain anly one private field named root, of type TNode, which is a reference to the root of the tree. No other fields are alowed. When the set is empty you should have root -null. .Class BSTSet must contain at least the following constructors o public BSTSet-initializes the BSTSet object to represent the empty set (an empty tree) o public BSTSetintl] input)-initiaites the BSTSet object to represent the set containing all elements in array input, without repettions. For example, if the array is: 5,7,4,5, the corresponding set is 15,7.4) o public boolean isInlint v: Retus true if integer v is an element of this BSTSet. It returns o public void add int vi Adds v to this BSTSet if was not already an element of this o public boolean removeint v): Removes v from this BSTSet if v was an element of this o public BSTSet union(BSTSet syRetums a new BSTSet which represents the union of this false atherwise. BSTSet. It does nothing otherwise BSTSet and returns true. Returns false if v was not an element of this BSTSet BSTSet and s. This method should not mcdify the input sets public BSTSet intersection,BSTSet s: Returns a new BSTSet which represents the intersection of this BSTSet and s. This method should not modify the input sets. o o public int sire): Returns the number af elements in this set. o public int height: Returns the height of this BSTSet. Height of empty set is -1. o public void printBSTSetfl: Outputs the elements of chis set to the console, in increasing order o private void printBSTSet TNode : Outputs to the console the elements stored in the subtree rooted int, in increasing order For the two printing methods spedified above yo u must use the following code o public waid printBSTSetDE in root-=null) System.out.printir"The set is empty else System.out.print"The set elements are: " printSTSetiroot Systemout.printn private void printBSTSet(TNode tH int !=null)( princBsTsetft leti: System.out.printt.element+ printBSTSett.right) publik vold printNonReo): Prints the htegers in this BSTSet in increasing order. This method is nonrecursive and uses a stack to implement the inorder traversal. o Notes * Compute the asymptotic run time and space complexity of all methods implemented as a function of the set size. Provide justification for your analysis Be sure to right a test class TestBSTSet to test al methods. .You are permittrd to implement other private methods inside cass BSTSet as required. However, you are not permitted to modify the class TNode. Additionally, class BSTSet must contain ony one field which is at TNoce reference named root. .Be sure to thoroughly and descriptively comment all code
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