|
| 1 | +import java.util.Arrays; |
| 2 | +public class TreeSort { |
| 3 | + |
| 4 | + public Node root; |
| 5 | + |
| 6 | + public TreeSort(Object x) { |
| 7 | + root = new Node(x); |
| 8 | + }//end TreeSort constructor |
| 9 | + |
| 10 | + public Node insert(Node node, Integer x) { |
| 11 | + if (node == null) { |
| 12 | + return node = new Node(x); |
| 13 | + }//end if |
| 14 | + if (x < (Integer) node.anElement) { |
| 15 | + node.less = insert(node.less, x); |
| 16 | + } //end if |
| 17 | + else { |
| 18 | + node.greater = insert(node.greater, x); |
| 19 | + }//end else |
| 20 | + return node; |
| 21 | + }//end insert |
| 22 | + |
| 23 | + |
| 24 | + public Node decimalInsert(Node node, Double x) { |
| 25 | + if (node == null) { |
| 26 | + return node = new Node(x); |
| 27 | + }//end if |
| 28 | + if (x < (Double) node.anElement) { |
| 29 | + node.less = decimalInsert(node.less, x); |
| 30 | + } //end if |
| 31 | + else { |
| 32 | + node.greater = decimalInsert(node.greater, x); |
| 33 | + }//end else |
| 34 | + return node; |
| 35 | + }//end insert |
| 36 | + |
| 37 | + |
| 38 | + public void treeSort(Node node) { |
| 39 | + if (node != null) { |
| 40 | + treeSort(node.less); |
| 41 | + System.out.print(((Object) node.anElement) + ", "); |
| 42 | + treeSort(node.greater); |
| 43 | + }//end if |
| 44 | + }//end TreeSort class |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +public static void main(String args[]) { |
| 49 | + int[] intArray = {12, 40, 9, 3, 19, 74, 7, 31, 23, 54, 26, 81, 12 }; |
| 50 | + TreeSort ts = new TreeSort(new Integer(intArray[0])); |
| 51 | + for (int i = 1; i < intArray.length; i++) { //sorts every index of the list one at a time |
| 52 | + ts.insert(ts.root, new Integer(intArray[i])); //builds on the tree from a root node |
| 53 | + }//end for |
| 54 | + System.out.print("Integer Array Sorted in Increasing Order: "); |
| 55 | + ts.treeSort(ts.root); |
| 56 | + System.out.println(); //To sort a test array of integers |
| 57 | + |
| 58 | + Double[] decimalArray = {8.2, 1.5, 3.14159265, 9.3, 5.1, 4.8, 2.6}; |
| 59 | + TreeSort dts = new TreeSort(new Double(decimalArray[0]).doubleValue()); |
| 60 | + for (int i = 1; i < decimalArray.length; i++) { //sorts every index of the list one at a time |
| 61 | + dts.decimalInsert(dts.root, new Double(decimalArray[i]).doubleValue()); //builds on the tree from a root node |
| 62 | + }//end for |
| 63 | + System.out.print("Decimal Array, Sorted in Increasing Order: "); |
| 64 | + dts.treeSort(dts.root); |
| 65 | + System.out.println(); |
| 66 | + |
| 67 | + String[] stringArray = {"c", "a", "e", "b","d", "dd","da","zz", "AA", "aa","aB","Hb", "Z"}; |
| 68 | + int last = stringArray.length; |
| 69 | + Arrays.sort(stringArray); //Uses an imported arrays method to automatically alphabetize |
| 70 | + System.out.print("String Array Sorted in Alphabetical Order: "); |
| 71 | + ts.insert(ts.root, last); |
| 72 | + for(int i=0; i<last; i++){ |
| 73 | + System.out.print(stringArray[i]+"\t"); |
| 74 | + //To sort a test array of strings hard coded in the main method |
| 75 | + //Please Note that Capital letters always come before lower case |
| 76 | + //I tried to make the test array show its behavior clearly |
| 77 | + }//end for |
| 78 | +}//end Main method |
| 79 | +}//end TreeSort class |
| 80 | + |
0 commit comments