Question

In: Computer Science

A 2-3-4 tree can be used as a sorting machine. Write a sort() method that has...

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

Solutions

Expert Solution

1). ANSWER :

GIVENTHAT :

Executable Code

//my234TreeNode.java
//Package Name.
package my234tree;

//Class.
class my234TreeNode
{
   
    //Declare the needed node variables.
    my234TreeNode tLeft, tRight;
    int tData;

    //Empty Constructor.
    public my234TreeNode()
    {
       
        //Initialize left node as NULL.
        tLeft = null;
       
        //Initialize right node as NULL.
        tRight = null;
       
        //Initialize data as NULL.
        tData = 0;
    }
   
    //Constructor with one parameter.
    public my234TreeNode(int d)
    {
       
        //Initialize left node as NULL.
        tLeft = null;
       
        //Initialize right node as NULL.
        tRight = null;
       
        //Assign d to data.
        tData = d;
    }
   
    //Setter method for node left.
    public void setTLeft(my234TreeNode d)
    {
       
        //Set d to left node.
        tLeft = d;
    }
   
    //Setter method for node right.
    public void setTRight(my234TreeNode d)
    {
       
        //Set d to right node.
        tRight = d;
    }
   
    //Getter method for node left.
    public my234TreeNode getTLeft()
    {
       
        //Return the left node value.
        return tLeft;
    }
   
    //Getter method for node right.
    public my234TreeNode getTRight()
    {
       
        //Return the right node value.
        return tRight;
    }
   
    //Setter method for node data.
    public void setTData(int d)
    {
        //Set d to data.
        tData = d;
    }
   
    //Getter method for node data.
    public int getTData()
    {
       
        //Return the data.
        return tData;
    }
}

//my234TreeNode.java
//Package Name.
package my234tree;

//Class.
class my234Tree
{
   
    //Declare the needed tree node variable for root.
    private my234TreeNode root;
   
    //Constructor.
    public my234Tree()
    {
        root = null;
    }
   
    //Method insertDate to insert data.
    public void insertTData(int tData)
    {
       
        //Insert the data to root.
        root = insertTData(root, tData);
    }
   
    //Method insertDate to insert data.
    private my234TreeNode insertTData(my234TreeNode node, int tData)
    {
       
        //Check for root emptiness.
        if (node == null)
           
            //Insert node data.
            node = new my234TreeNode(tData);
       
        //Otherwise.
        else
        {
           
            //Condition check to insert in left node.
            if (tData <= node.getTData())
               
                //Insert in left.
                node.tLeft = insertTData(node.tLeft, tData);
           
            //Otherwise.
            else
               
                //Insert in left.
                node.tRight = insertTData(node.tRight, tData);
        }
       
        //Return the node.
        return node;
    }
   
    //Method to display the tree data in data inserted order.
    public void displayTData()
    {
       
        //Display the root.
        displayTData(root);
    }
   
    //Method to display the tree data in data inserted order.
    private void displayTData(my234TreeNode r)
    {
       
        //Check for non emptiness of root.
        if (r != null)
        {
           
            //Display.
            System.out.print(r.getTData() + " ");
           
            //Recursive call to method displayTData().
            displayTData(r.getTLeft());
           
            //Recursive call to method displayTData().
            displayTData(r.getTRight());
        }
    }
   
    //Method sort to display the data in sorted order.
    public void sort()
    {
       
        //Display the root.
        sort(root);
    }   
   
    //Method sort to display the data in sorted order.
    private void sort(my234TreeNode r)
    {
       
        //Check for non emptiness of root.
        if (r != null)
        {
           
            //Recursive call to sort() method.
            sort(r.getTLeft());
           
            //Display.
            System.out.print(r.getTData() + " ");
           
            //Recursive call to sort() method.
            sort(r.getTRight());
        }
    }   
}

//my234TreeDriver.java
//Package Name.
package my234tree;

//Include the needed variable.
import java.util.Random;

//Class.
public class my234TreeDriver
{
   
    //Declare the input size.
    public static int N = 10;
   
    //Main.
    public static void main(String args[])
    {
       
        //Create a new instance for Random.
        Random random = new Random();
       
        //Create a new instance for my234Tree.
        my234Tree my234t = new my234Tree();
       
        //Display.
        System.out.println("\nSorting randomly generated numbers using 2-3-4 Tree");
       
        //For loop to get randomly generated numbers.
        for (int i = 0; i < N; i++)
           
            //Function call to insertTData().
            my234t.insertTData(Math.abs(random.nextInt(100)));
           
            //Display.
            System.out.println("\nThe data of the 2-3-4 Tree: ");
           
            //Function call to displayTData().
            my234t.displayTData();
           
            //Display.
            System.out.println("\n\nThe sorted data of 2-3-4 Tree: ");
           
            //Function call to sort().
            my234t.sort();
           
            //Print new line.
            System.out.println();
    }
}


Related Solutions

Using the buildHeap method, write a sorting function that can sort a list in O(nlogn) time....
Using the buildHeap method, write a sorting function that can sort a list in O(nlogn) time. def buildHeap(self,alist): i = len(alist) // 2 self.currentSize = len(alist) self.heapList = [0] + alist[:] while (i > 0): self.percDown(i) i = i - 1 Base on this code please
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the...
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the list after each outer loop. Do his manually, i.e. step through the algorithm yourself without a computer. This question is related to data structure and algorithm in javascript (.js). Please give your answer keeping this in your mind.
Sorting with Binary Search Tree This assignment asks you to sort the lines of an input...
Sorting with Binary Search Tree This assignment asks you to sort the lines of an input file (or from standard input) and print the sorted lines to an output file (or standard output). Your program, called bstsort (binary search tree sort), will take the following command line arguments: % bstsort [-c] [-o output_file_name] [input_file_name] If -c is present, the program needs to compare the strings case sensitive; otherwise, it's case insensitive. If the output_file_name is given with the -o option,...
Write a method for binary tree in Python that can determine whether a binary tree is...
Write a method for binary tree in Python that can determine whether a binary tree is a binary search tree or not. The input should be a binary tree. The output should be true or false. True= binary tree meets the criteria to be a binary search tree. False= does not meet the criteria to be a binary search tree.
Implement heap sort by using the bottom-up insertion method. Add this sort to your sorting framework....
Implement heap sort by using the bottom-up insertion method. Add this sort to your sorting framework. Evaluate its performance in terms of the numbers of comparisons and exchanges, and compare it to the performance of the two advanced sorting methods that you have previously implemented. Submit your report with detailed empirical results and a thorough explanation of these results. Which of the three advanced sorting method is the best choice for a) ordered data, b) data in reverse order, and...
In Java: Write two advanced sorting methods of your choice: (Shell Sort OR Radix Sort) AND...
In Java: Write two advanced sorting methods of your choice: (Shell Sort OR Radix Sort) AND (Merge Sort OR Quick Sort). If you choose Shell Sort, experiment with different incremental sequences to see how they affect the algorithm's run time efficiency (count the number of comparisons and exchanges). If you choose to implement Radix Sort, answer the following question as well: Can you write a version of Radix Sort for String objects? If yes, explain how. If not, explain why....
write modules to sort by 2 field then 3 fields then 4 fields Use the data...
write modules to sort by 2 field then 3 fields then 4 fields Use the data Structure to guide you. //HW sort by 5 of these fields and ask the user which field to sort by !!! // Use a custom comparator. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; class Emprec { String name; String address; double hours; double rate; int dependents; char gender; boolean degree; // This is the classes's constructor !!!! Emprec(String name, String address, String hours,String...
WRITE MODULES TO SORT BY 2 FIELD THEN 3 FIELD, THEN 4 FIELD use data structure...
WRITE MODULES TO SORT BY 2 FIELD THEN 3 FIELD, THEN 4 FIELD use data structure to guide you. please i need help. im so glad if anyone help me. thank you a lot and be safe // Use a custom comparator. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; class Emprec { String name; String address; double hours; double rate; int dependents; char gender; boolean degree; // This is the classes's constructor !!!! Emprec(String name, String address, String hours,String...
Write a very general sort method that can sort any type of data arrays/lists. For example,...
Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language doesn’t support...
Exercise 4–Timing Sorting AlgorithmCollect the run times for either selection sort or insertion sort (use random...
Exercise 4–Timing Sorting AlgorithmCollect the run times for either selection sort or insertion sort (use random values for an array and sorted values; sorted the same list twice and collect time each time) for the following array sizes: 1000, 2000, and 10000. You should be able to confirm that the runtime is n^2 for unsorted list (i.e., going from 1000 to 2000 should be about 4 times slower and going from 1000 to 10000 should be about 100times slower). Question...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT