Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Can anyone help me to solve this problem? I'm stuck. Two Three Trees (have to pass the JUnit test, I will provide a basic test

Can anyone help me to solve this problem? I'm stuck. Two Three Trees (have to pass the JUnit test, I will provide a basic test below. )

1. You will need a Tree class, with a Tree constructor which takes no parameters. Once again, use the default package (no package statement).

2.Tree will need an insert(int x) method, which will insert the value x into your tree. For this tree, duplicate insertions should be discarded. That is, if I insert a value into the tree which is already in the tree, do not change your tree. Your method should return true if the element is added (which should happen if it isn't already in the tree), and false if it is not added (if the number was already in the tree).

3.Tree will need a size(int x) method, will return the int size of the subtree rooted at the node that contains integer x. If x is not in the tree, it should return 0

4. Please consider the following: (a) How well do your tests cover your code? That is, is every line of your code tested by your test cases? For every conditional in your code, is it tested once when true, and once when false? (b) Does your code pass all of your tests? If not, why not? (c) Do you think your code works on cases you haven't tested? What might go wrong on a large test case, in which you don't want to work out the answer by hand? How can you test some of the things that might go wrong, without working it out by hand?

import static org.junit.Assert.*; import org.junit.Test; public class GivenTreeTests { @Test public void singleNodeTree() { Tree t = new Tree(); t.insert(9); assertEquals(1, t.size(9)); assertEquals(0, t.size(8)); assertEquals(0, t.size(10)); t.insert(15); assertEquals(2, t.size(9)); assertEquals(0, t.size(8)); assertEquals(0, t.size(10)); assertEquals(2, t.size(15)); assertEquals(0, t.size(18)); t = new Tree(); t.insert(15); t.insert(9); assertEquals(2, t.size(9)); assertEquals(0, t.size(8)); assertEquals(0, t.size(10)); assertEquals(2, t.size(15)); assertEquals(0, t.size(18)); } @Test public void oneSplitLeft() { Tree t = new Tree(); t.insert(9); t.insert(15); t.insert(1); assertEquals(3, t.size(9)); assertEquals(1, t.size(15)); assertEquals(0, t.size(17)); assertEquals(0, t.size(11)); assertEquals(1, t.size(1)); assertEquals(0, t.size(0)); assertEquals(0, t.size(3)); } @Test public void oneSplitRight() { Tree t = new Tree(); t.insert(1); t.insert(9); t.insert(15); assertEquals(3, t.size(9)); assertEquals(1, t.size(15)); assertEquals(0, t.size(17)); assertEquals(0, t.size(11)); assertEquals(1, t.size(1)); assertEquals(0, t.size(0)); assertEquals(0, t.size(3)); } @Test public void oneSplitMiddle() { Tree t = new Tree(); t.insert(1); t.insert(15); t.insert(9); assertEquals(3, t.size(9)); assertEquals(1, t.size(15)); assertEquals(0, t.size(17)); assertEquals(0, t.size(11)); assertEquals(1, t.size(1)); assertEquals(0, t.size(0)); assertEquals(0, t.size(3)); } @Test public void testDuplicates() { Tree t = new Tree(); t.insert(1); t.insert(9); t.insert(15); t.insert(13); t.insert(20); t.insert(7); t.insert(4); t.insert(1); t.insert(9); t.insert(15); t.insert(1); t.insert(9); t.insert(15); t.insert(13); t.insert(20); t.insert(7); t.insert(4); t.insert(13); t.insert(20); t.insert(7); t.insert(4); assertEquals(7, t.size(9)); assertEquals(3, t.size(4)); assertEquals(3, t.size(15)); assertEquals(0, t.size(12)); assertEquals(1, t.size(13)); assertEquals(0, t.size(14)); assertEquals(0, t.size(19)); assertEquals(1, t.size(20)); assertEquals(0, t.size(21)); assertEquals(1, t.size(1)); assertEquals(0, t.size(0)); assertEquals(0, t.size(3)); assertEquals(0, t.size(6)); assertEquals(1, t.size(7)); assertEquals(0, t.size(8)); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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