Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

TwoThreeTree 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).

TwoThreeTree will need a search(int x) method, which will search for the value x in your tree. The method should return a String, formatted as follows. If the value x is in the tree, it will reside in a node with either one or two keys. For a one key node, the string will just be that node's key as a String. If it is a two key node, the string should be the two values, in increasing order, with a single space between them. Example: if we are searching for the number 17, and it is a key alone in a node, the search function should return "17". If it is in a node with 35, you should return "17 35". If it is in a node with 3, you should return "3 17". Do not format differently. No different spaces, no extra spaces at the end.

If the value searched for is not in the tree, your search should have terminated at a leaf node. In that case, print the contents of that leaf node, with the same formatting described above. That is, if 17 is not in the tree, and you end in a node that contains keys 18 and 20, return "18 20".

You should create your own JUnit tests as well, in a file with a name ending in Test.java or Tests.java.

In order to make some of your JUnit tests, you should build some 2-3 trees by hand, on paper, by calling insertion, and then search. Create some tests which make sure that your code gives you back exactly what you expect for those cases. It is really difficult to test your code for correctness if you don't actually test it. For correctness. Note: in your JUnit code, you can have "magic numbers" all over the place.

Heres the given tests:

import static org.junit.Assert.*; import org.junit.Test; public class TwoThreeTreeGivenTests { @Test public void singleNodeTree() { TwoThreeTree t = new TwoThreeTree(); int val = 9; t.insert(val); String expected = "9"; assertEquals(expected, t.search(val)); val = 8; assertEquals(expected, t.search(val)); val = 10; assertEquals(expected, t.search(val)); val = 15; t.insert(val); expected = "9 15"; val = 9; assertEquals(expected, t.search(val)); val = 8; assertEquals(expected, t.search(val)); val = 10; assertEquals(expected, t.search(val)); val = 15; assertEquals(expected, t.search(val)); val = 18; assertEquals(expected, t.search(val)); t = new TwoThreeTree(); val = 15; t.insert(val); val = 9; t.insert(val); val = 9; assertEquals(expected, t.search(val)); val = 8; assertEquals(expected, t.search(val)); val = 10; assertEquals(expected, t.search(val)); val = 15; assertEquals(expected, t.search(val)); val = 18; assertEquals(expected, t.search(val)); } @Test public void oneSplitLeft() { TwoThreeTree t = new TwoThreeTree(); t.insert(9); t.insert(15); t.insert(1); String expected = "9"; assertEquals(expected, t.search(9)); expected = "15"; assertEquals(expected, t.search(15)); assertEquals(expected, t.search(17)); assertEquals(expected, t.search(11)); expected = "1"; assertEquals(expected, t.search(1)); assertEquals(expected, t.search(0)); assertEquals(expected, t.search(3)); } @Test public void oneSplitRight() { TwoThreeTree t = new TwoThreeTree(); t.insert(1); t.insert(9); t.insert(15); String expected = "9"; assertEquals(expected, t.search(9)); expected = "15"; assertEquals(expected, t.search(15)); assertEquals(expected, t.search(17)); assertEquals(expected, t.search(11)); expected = "1"; assertEquals(expected, t.search(1)); assertEquals(expected, t.search(0)); assertEquals(expected, t.search(3)); } @Test public void oneSplitMiddle() { TwoThreeTree t = new TwoThreeTree(); t.insert(1); t.insert(15); t.insert(9); String expected = "9"; assertEquals(expected, t.search(9)); expected = "15"; assertEquals(expected, t.search(15)); assertEquals(expected, t.search(17)); assertEquals(expected, t.search(11)); expected = "1"; assertEquals(expected, t.search(1)); assertEquals(expected, t.search(0)); assertEquals(expected, t.search(3)); } @Test public void testDuplicates() { TwoThreeTree t = new TwoThreeTree(); 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); String expected = "9"; assertEquals(expected, t.search(9)); expected = "4"; assertEquals(expected, t.search(4)); expected = "15"; assertEquals(expected, t.search(15)); expected = "13"; assertEquals(expected, t.search(12)); assertEquals(expected, t.search(13)); assertEquals(expected, t.search(14)); expected = "20"; assertEquals(expected, t.search(19)); assertEquals(expected, t.search(20)); assertEquals(expected, t.search(21)); expected = "1"; assertEquals(expected, t.search(1)); assertEquals(expected, t.search(0)); assertEquals(expected, t.search(3)); expected = "7"; assertEquals(expected, t.search(6)); assertEquals(expected, t.search(7)); assertEquals(expected, t.search(8)); } } 

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

Advances In Spatial And Temporal Databases 10th International Symposium Sstd 2007 Boston Ma Usa July 2007 Proceedings Lncs 4605

Authors: Dimitris Papadias ,Donghui Zhang ,George Kollios

2007th Edition

3540735399, 978-3540735397

More Books

Students also viewed these Databases questions

Question

What are the challenges associated with tunneling in urban areas?

Answered: 1 week ago

Question

What are the main differences between rigid and flexible pavements?

Answered: 1 week ago

Question

What is the purpose of a retaining wall, and how is it designed?

Answered: 1 week ago

Question

How do you determine the load-bearing capacity of a soil?

Answered: 1 week ago

Question

what is Edward Lemieux effect / Anomeric effect ?

Answered: 1 week ago

Question

8. Explain the relationship between communication and context.

Answered: 1 week ago