Question: import java.util.Random; public class Tree { public static void main(String[] args) { Random rand = new Random(); Tree t = new Tree(); for(int i=0; i

import java.util.Random;
public class Tree {
public static void main(String[] args) {
Random rand = new Random();
Tree t = new Tree();
for(int i=0; i
int x = rand.nextInt(100);
System.out.print(x + " ");
t.add(x);
}
System.out.println(" Size: " + t.size());
t.print();
String[] dwarves = {"Happy", "Sleepy", "Dopey", "Doc", "Bashful", "Sneezy", "Grumpy"};
}
private class Node {
int element;
Node left;
Node right;
}
private Node root;
private int size;
public Tree() {
root = null;
size = 0;
}
public int size() {
return size;
}
public boolean contains(int x) {
return contains(x, root);
}
private boolean contains(int x, Node current) {
if (current == null) {
return false;
} else if (current.element == x) {
return true;
} else if (x
return contains(x, current.left);
} else {
return contains(x, current.right);
}
}
public void print() {
print(root);
System.out.println();
}
private void print(Node current) {
if (current != null) {
print(current.left);
System.out.print(current.element + " ");
print(current.right);
}
}
public void add(int e) {
root = add(e, root);
size++;
}
private Node add(int e, Node current) {
if(current == null) {
Node n = new Node();
n.element = e;
return n;
} else if (e
current.left = add(e, current.left);
} else {
current.right = add(e, current.right);
}
return current;
}
}
 import java.util.Random; public class Tree { public static void main(String[] args)
T DESIGN PAGE LAYOUT REFERENCES MAILINGS REVIEW VIEW ADD-INS AaBbcood AaBbccod AaBbc AaBbCol AaB imes New Re 16 EB 1 Normal INo spac... Heading 1 Heading 2 Title 3 I U abc xa x Styles Paragraph Generic Binary Search Tree Problem 1: write a class that implements a generic binary search tree including the methods add, contains and er. (We will do this as a group) Problem 2: Add a toString method to the class. This method returns a string ofthe form "[el, e2, enl" containing all of the elements in the tree in order Problem 3: Add a method to the class that returns the number of leaves in the tree. Problem 4: Add a clone method to the class that returns a copy of the tree. Remember that we have three different ways of traversing a tree: preorder, inorder and po Does it matter which one you use in this method? Problem 5: Add an equals method to the class. Two trees are equal if they include the same elements. The structure of the trees does not have to be the same. T DESIGN PAGE LAYOUT REFERENCES MAILINGS REVIEW VIEW ADD-INS AaBbcood AaBbccod AaBbc AaBbCol AaB imes New Re 16 EB 1 Normal INo spac... Heading 1 Heading 2 Title 3 I U abc xa x Styles Paragraph Generic Binary Search Tree Problem 1: write a class that implements a generic binary search tree including the methods add, contains and er. (We will do this as a group) Problem 2: Add a toString method to the class. This method returns a string ofthe form "[el, e2, enl" containing all of the elements in the tree in order Problem 3: Add a method to the class that returns the number of leaves in the tree. Problem 4: Add a clone method to the class that returns a copy of the tree. Remember that we have three different ways of traversing a tree: preorder, inorder and po Does it matter which one you use in this method? Problem 5: Add an equals method to the class. Two trees are equal if they include the same elements. The structure of the trees does not have to be the same

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!