Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA (BOOK: Data Structure and algorithms 2nd edition) QUESTION: 10.3 A 2-3-4 tr

ID: 3735662 • Letter: J

Question

JAVA (BOOK: Data Structure and algorithms 2nd edition)

QUESTION:

10.3 A 2-3-4 tree can be used as a sorting machine. Write a sort() method that’s passed an array of key values from main() and writes them back to the array in sorted order.

PLEASE USE THE MAIN METHOD (AND OTHER DIRECTIONS) GIVEN BELOW:

PLEASE MAKE SURE THE OUTPUT IS SAME.

DO IT AS SOON AS POSSIBLE. THANKS...

In the Tree234 you may add fields: longl sortArray; int sortindex; // ref to array for sort0) // index into sortArray and methods: sort(long[] arr, int size) and recSort(Node thisNode) public static void main(String[l args) Tree234 theTree new Tree2340); int size 20; longl] theArray (100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 5, 15, 25, 35, 45, 55, 65, 75, 85, 95 System.out.println("Unsorted:"); for(int j-0; j

Explanation / Answer

public static void sort(long[] theArray, int counter)

{

Tree234 sortingTree = new Tree234();

for(int j = 0; j < counter; j++)

sortingTree.insert(theArray[j]);

sortingTree.sortTraverse(theArray);

}

public static void main(String[] args) throws IOException

{

long value;

Tree234 theTree = new Tree234();

theTree.insert(50);

theTree.insert(40);

theTree.insert(60);

theTree.insert(30);

theTree.insert(70);

while(true)

{

System.out.print("Enter first letter of ");

System.out.print("show, insert, minimum, traverse, order, or find: ");

char choice = getChar();

switch(choice)

{

case 's':

theTree.displayTree();

break;

case 'i':

System.out.print("Enter value to insert: ");

value = getInt();

theTree.insert(value);

break;

case 'm':

theTree.findMinimum();

break;

case 'o':

value = 0;

long[] arrayToSort = new long[20];

int counter = 0;

while(value != -1 && counter < 20)

{

System.out.print("Enter value (enter '-1' to stop): ");

value = getInt();

if(value != -1)

{

arrayToSort[counter] = value;

counter++;

}

}

System.out.println("Before sorting...");

for(int j = 0; j < counter; j++)

System.out.print(arrayToSort[j] + " ");

sort(arrayToSort, counter);

System.out.println("After sorting...");

for(int j = 0; j < counter; j++)

System.out.print(arrayToSort[j] + " ");

System.out.println("");

break;

case 't':

theTree.traverseInOrder();

break;

case 'f':

System.out.print("Enter value to find: ");

value = getInt();

int found = theTree.find(value);

if(found != -1)

System.out.println("Found " + value);

else

System.out.println("Could not find " + value);

break;

default:

System.out.println("Invalid entry!");

}

}

}

public static String getString() throws IOException

{

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

String s = br.readLine();

return s;

}

public static char getChar() throws IOException

{

String s = getString();

return s.charAt(0);

}

public static int getInt() throws IOException

{

String s = getString();

return Integer.parseInt(s);

}

}