Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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;
}
}
image text in transcribed
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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

4. Describe the factors that influence self-disclosure

Answered: 1 week ago

Question

1. Explain key aspects of interpersonal relationships

Answered: 1 week ago