(In JAVA) (Use Binary Search Tree) Programming Steps: 1) Generate 20 random numb
ID: 3815249 • Letter: #
Question
(In JAVA) (Use Binary Search Tree)
Programming Steps:
1) Generate 20 random numbers from 1 to 100 inclusive. Write them to a text file called “p9in.txt”.
2) Read “p9in.txt” into a Binary Tree.
3) Write a menu similar to the following:
Choose the following:
1 – Add a number into the tree
2 – Delete a number from the tree
3 – Preorder Traversal
4 – Inorder Traversal
5 – Postorder Traversal
6 – Exit
4) Write the output to a file called “p9out.txt”
5) At least perform the following operations:
a. Write the content of the input file (from your generated random numbers)
b. Manually draw the tree using the numbers that you have generated.
c. Manually write 3 kinds of Traversal;
d. Have program print the results of 3 kinds of Traversal and compare them with the above.
e. Add a number to the tree and then write 3 kinds of Traversal;
f. Delete a number from the tree and then write 3 kinds of Traversal; Add some error handling if number not found.
III. The Sample Output File (Shown only for 10 random integers)
Result: (p9out.txt)
The generated input: 50 25 75 12 37 43 30 33 87 93 97
Preorder traversal: 50 25 12 37 30 33 43 75 87 93 97
Inorder traversal: 12 25 30 33 37 43 50 75 87 93 97
Postorder traversal: 12 33 30 43 37 25 97 93 87 75 50
Add 8:
(Followed by 3 kinds of Traversal)
Delete 30:
(Followed by 3 kinds of Traversal)
Delete 99:
** Number is not found **
Required programs:
BinaryTreeInterface.java:
TreeInterface.java:
TreeIteratorInterface.java:
BinaryNode.java:
BinaryTree.java:
Explanation / Answer
/*To generate a random number */
in java editor
Random r= new Random();
int x=0;
int y=100;
int z=r.nextInt(y-x)+x;
/*binary search tree*/
import java.util;
class treeA
{
ArrayList<Integer>a;
HashMap<Integer,Integer>hash;
public treeA()
{
a=new ArrayList<Integer>();
hash=new HasMap<Integer,Integer>();
void Addition(int x)
{
if(hash.get(x)!=null)
return;
int s=a.size();
a.Addition(x);
has.put(x,s);
}
void delete(int x)
{
Integer i=hash.get(x);
if(i==null)
return;
hash.delete(x);
intsize=a.size();
Integer l=a.get(size-1);
Collections.swap(a,i,size-1);
a.delete(size-1);
hassh.put(l,i);
}
int getRand()
{
Random r = new Random();
int x=r.nextInt(a.size));
return a.get(i);
}
Integer search(int x)
{
return hash.get(x);
}
}
class Main
{
public static void main(String [] args)
{
TreeA ab=new TreeA();
ab.add(20);
ab.add(30);
ab.add(35);
ab.add(40);
ab.add(50);
system.out.println(ab.search(30));
ab.delete(20);
ab.Addition(50);
system.out.println(ab.serach(50));
system.out.println(ab.getRand());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.