|
| 1 | +importjava.util.Arrays; |
| 2 | +publicclassTreeSort { |
| 3 | + |
| 4 | +publicNoderoot; |
| 5 | + |
| 6 | +publicTreeSort(Objectx) { |
| 7 | +root =newNode(x); |
| 8 | +}//end TreeSort constructor |
| 9 | + |
| 10 | +publicNodeinsert(Nodenode,Integerx) { |
| 11 | +if (node ==null) { |
| 12 | +returnnode =newNode(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 | +returnnode; |
| 21 | +}//end insert |
| 22 | + |
| 23 | + |
| 24 | +publicNodedecimalInsert(Nodenode,Doublex) { |
| 25 | +if (node ==null) { |
| 26 | +returnnode =newNode(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 | +returnnode; |
| 35 | +}//end insert |
| 36 | + |
| 37 | + |
| 38 | +publicvoidtreeSort(Nodenode) { |
| 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 | +publicstaticvoidmain(Stringargs[]) { |
| 49 | +int[]intArray = {12,40,9,3,19,74,7,31,23,54,26,81,12 }; |
| 50 | +TreeSortts =newTreeSort(newInteger(intArray[0])); |
| 51 | +for (inti =1;i <intArray.length;i++) {//sorts every index of the list one at a time |
| 52 | +ts.insert(ts.root,newInteger(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 | +TreeSortdts =newTreeSort(newDouble(decimalArray[0]).doubleValue()); |
| 60 | +for (inti =1;i <decimalArray.length;i++) {//sorts every index of the list one at a time |
| 61 | +dts.decimalInsert(dts.root,newDouble(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 | +intlast =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(inti=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 | + |