Question: Design and write a complete test program to test if the BST class in Listing 25.4 meets all requirements. Data from Listing 25.4 1 publ1c

Design and write a complete test program to test if the BST class in Listing 25.4 meets all requirements.

Data from Listing 25.4

1 publ1c class BST 1mplements Tree { protected TreeNode root: protected 1nt

size = 0: protected java.util.Comparator c: 4 1** Create a default BST

with a natural order comparator */ publ1c BST () { this.c =

(e1, e2) -> ((Comparable )e1).compareTo (e2): 6 7 8 10 /** Create

a BST with a specified comparator */ publ1c BST (java.util.Comparator c) {


1 publ1c class BST 1mplements Tree { protected TreeNode root: protected 1nt size = 0: protected java.util.Comparator c: 4 1** Create a default BST with a natural order comparator */ publ1c BST () { this.c = (e1, e2) -> ((Comparable )e1).compareTo (e2): 6 7 8 10 /** Create a BST with a specified comparator */ publ1c BST (java.util.Comparator c) { 11 12 13 this.c c: 14 15 ** Create a binary tree from an array of objects */ publ1c BST (E[] objects) { this.c = (et, e2) -> ((Comparable ) e1).compareTo (e2): for (1nt i = 0; i < objects.length: i++) add (objects[i]): } 16 17 18 19 20 21 22 e0verride /** Return true if the element is in the tree / publ1c boolean search(E e) { TreeNode current = root: // Start from the root 23 24 25 26 wh1le (current != nul1) { 1f (c.compare (e. current.element) < 0) { current = current.left: 27 28 29 30 else 1f (c.compare (e. current.element) > 0) { current = current.right: 31 32 33 else // element matches current.element return true: // Element is found 34 35 36 37 38 return false: 39 40 eOverride /** Insert element e into the binary tree * Return true if the element is inserted successfully */ pub11c boolean insert(E e) { 1f (root == nul1) root = createNewNode (e): I1 Create a new root else { Il Locate the parent node TreeNode parent = nul1; 41 42 43 44 45 46 47 48 49 TreeNode current = root: wh1le (current != nul1) 1f (c.compare(e, current.element) < 0) { parent = current; current = current.left: 50 51 52 53 54 else 1f (c.compare (e, current.element) > 0) { parent = current: current = current.right; 55 56 57 58 59 else

Step by Step Solution

3.44 Rating (163 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

public class Exercise2512 private BST tree new BST public Exer... View full answer

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 Introduction to Java Programming and Data Structure Questions!