From 07d5d9ea6dafaa5885960d9421136d08b0706ca1 Mon Sep 17 00:00:00 2001 From: Joseph M Pignataro Date: Mon, 21 Aug 2017 13:35:38 -0400 Subject: [PATCH 1/4] Create Node.java A node class to be used for the binary tree sorting algorithm. I will put up the sorting algorithm ASAP. --- Misc/Node.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Misc/Node.java diff --git a/Misc/Node.java b/Misc/Node.java new file mode 100644 index 000000000..ab495b77d --- /dev/null +++ b/Misc/Node.java @@ -0,0 +1,16 @@ +public class Node { + public Object anElement; + public Node less; + public Node greater; + + public Node(Object theElement) { + this(theElement, null, null); //an empty node at the end will be by itself with no children, therefore the other 2 parameters are always null + //obviously the node can still be a child of other elements + } + + public Node(Object currentElement, Node lessSide, Node greaterSide) { + anElement = currentElement; + this.less = lessSide; + this.greater = greaterSide; + } +} From f16117e1092f4a8f2c10698772991d3480a2d774 Mon Sep 17 00:00:00 2001 From: Joseph M Pignataro Date: Mon, 21 Aug 2017 13:51:57 -0400 Subject: [PATCH 2/4] Tree search Uses Node class in misc. --- Sorts/BinaryTreeSearch.java | 80 +++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Sorts/BinaryTreeSearch.java diff --git a/Sorts/BinaryTreeSearch.java b/Sorts/BinaryTreeSearch.java new file mode 100644 index 000000000..e6a80f62a --- /dev/null +++ b/Sorts/BinaryTreeSearch.java @@ -0,0 +1,80 @@ +import java.util.Arrays; +public class TreeSort { + + public Node root; + + public TreeSort(Object x) { + root = new Node(x); + }//end TreeSort constructor + + public Node insert(Node node, Integer x) { + if (node == null) { + return node = new Node(x); + }//end if + if (x < (Integer) node.anElement) { + node.less = insert(node.less, x); + } //end if + else { + node.greater = insert(node.greater, x); + }//end else + return node; + }//end insert + + + public Node decimalInsert(Node node, Double x) { + if (node == null) { + return node = new Node(x); + }//end if + if (x < (Double) node.anElement) { + node.less = decimalInsert(node.less, x); + } //end if + else { + node.greater = decimalInsert(node.greater, x); + }//end else + return node; + }//end insert + + + public void treeSort(Node node) { + if (node != null) { + treeSort(node.less); + System.out.print(((Object) node.anElement) + ", "); + treeSort(node.greater); + }//end if + }//end TreeSort class + + + +public static void main(String args[]) { + int[] intArray = {12, 40, 9, 3, 19, 74, 7, 31, 23, 54, 26, 81, 12 }; + TreeSort ts = new TreeSort(new Integer(intArray[0])); + for (int i = 1; i < intArray.length; i++) { //sorts every index of the list one at a time + ts.insert(ts.root, new Integer(intArray[i])); //builds on the tree from a root node + }//end for + System.out.print("Integer Array Sorted in Increasing Order: "); + ts.treeSort(ts.root); + System.out.println(); //To sort a test array of integers + + Double[] decimalArray = {8.2, 1.5, 3.14159265, 9.3, 5.1, 4.8, 2.6}; + TreeSort dts = new TreeSort(new Double(decimalArray[0]).doubleValue()); + for (int i = 1; i < decimalArray.length; i++) { //sorts every index of the list one at a time + dts.decimalInsert(dts.root, new Double(decimalArray[i]).doubleValue()); //builds on the tree from a root node + }//end for + System.out.print("Decimal Array, Sorted in Increasing Order: "); + dts.treeSort(dts.root); + System.out.println(); + + String[] stringArray = {"c", "a", "e", "b","d", "dd","da","zz", "AA", "aa","aB","Hb", "Z"}; + int last = stringArray.length; + Arrays.sort(stringArray); //Uses an imported arrays method to automatically alphabetize + System.out.print("String Array Sorted in Alphabetical Order: "); + ts.insert(ts.root, last); + for(int i=0; i Date: Mon, 21 Aug 2017 13:53:15 -0400 Subject: [PATCH 3/4] Delete BinaryTreeSearch.java Wrong title --- Sorts/BinaryTreeSearch.java | 80 ------------------------------------- 1 file changed, 80 deletions(-) delete mode 100644 Sorts/BinaryTreeSearch.java diff --git a/Sorts/BinaryTreeSearch.java b/Sorts/BinaryTreeSearch.java deleted file mode 100644 index e6a80f62a..000000000 --- a/Sorts/BinaryTreeSearch.java +++ /dev/null @@ -1,80 +0,0 @@ -import java.util.Arrays; -public class TreeSort { - - public Node root; - - public TreeSort(Object x) { - root = new Node(x); - }//end TreeSort constructor - - public Node insert(Node node, Integer x) { - if (node == null) { - return node = new Node(x); - }//end if - if (x < (Integer) node.anElement) { - node.less = insert(node.less, x); - } //end if - else { - node.greater = insert(node.greater, x); - }//end else - return node; - }//end insert - - - public Node decimalInsert(Node node, Double x) { - if (node == null) { - return node = new Node(x); - }//end if - if (x < (Double) node.anElement) { - node.less = decimalInsert(node.less, x); - } //end if - else { - node.greater = decimalInsert(node.greater, x); - }//end else - return node; - }//end insert - - - public void treeSort(Node node) { - if (node != null) { - treeSort(node.less); - System.out.print(((Object) node.anElement) + ", "); - treeSort(node.greater); - }//end if - }//end TreeSort class - - - -public static void main(String args[]) { - int[] intArray = {12, 40, 9, 3, 19, 74, 7, 31, 23, 54, 26, 81, 12 }; - TreeSort ts = new TreeSort(new Integer(intArray[0])); - for (int i = 1; i < intArray.length; i++) { //sorts every index of the list one at a time - ts.insert(ts.root, new Integer(intArray[i])); //builds on the tree from a root node - }//end for - System.out.print("Integer Array Sorted in Increasing Order: "); - ts.treeSort(ts.root); - System.out.println(); //To sort a test array of integers - - Double[] decimalArray = {8.2, 1.5, 3.14159265, 9.3, 5.1, 4.8, 2.6}; - TreeSort dts = new TreeSort(new Double(decimalArray[0]).doubleValue()); - for (int i = 1; i < decimalArray.length; i++) { //sorts every index of the list one at a time - dts.decimalInsert(dts.root, new Double(decimalArray[i]).doubleValue()); //builds on the tree from a root node - }//end for - System.out.print("Decimal Array, Sorted in Increasing Order: "); - dts.treeSort(dts.root); - System.out.println(); - - String[] stringArray = {"c", "a", "e", "b","d", "dd","da","zz", "AA", "aa","aB","Hb", "Z"}; - int last = stringArray.length; - Arrays.sort(stringArray); //Uses an imported arrays method to automatically alphabetize - System.out.print("String Array Sorted in Alphabetical Order: "); - ts.insert(ts.root, last); - for(int i=0; i Date: Mon, 21 Aug 2017 13:54:34 -0400 Subject: [PATCH 4/4] Remake of TreeSort Uses Node class in misc files. Remade after a typo in the last file. --- Sorts/BinaryTreeSort.java | 80 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Sorts/BinaryTreeSort.java diff --git a/Sorts/BinaryTreeSort.java b/Sorts/BinaryTreeSort.java new file mode 100644 index 000000000..e6a80f62a --- /dev/null +++ b/Sorts/BinaryTreeSort.java @@ -0,0 +1,80 @@ +import java.util.Arrays; +public class TreeSort { + + public Node root; + + public TreeSort(Object x) { + root = new Node(x); + }//end TreeSort constructor + + public Node insert(Node node, Integer x) { + if (node == null) { + return node = new Node(x); + }//end if + if (x < (Integer) node.anElement) { + node.less = insert(node.less, x); + } //end if + else { + node.greater = insert(node.greater, x); + }//end else + return node; + }//end insert + + + public Node decimalInsert(Node node, Double x) { + if (node == null) { + return node = new Node(x); + }//end if + if (x < (Double) node.anElement) { + node.less = decimalInsert(node.less, x); + } //end if + else { + node.greater = decimalInsert(node.greater, x); + }//end else + return node; + }//end insert + + + public void treeSort(Node node) { + if (node != null) { + treeSort(node.less); + System.out.print(((Object) node.anElement) + ", "); + treeSort(node.greater); + }//end if + }//end TreeSort class + + + +public static void main(String args[]) { + int[] intArray = {12, 40, 9, 3, 19, 74, 7, 31, 23, 54, 26, 81, 12 }; + TreeSort ts = new TreeSort(new Integer(intArray[0])); + for (int i = 1; i < intArray.length; i++) { //sorts every index of the list one at a time + ts.insert(ts.root, new Integer(intArray[i])); //builds on the tree from a root node + }//end for + System.out.print("Integer Array Sorted in Increasing Order: "); + ts.treeSort(ts.root); + System.out.println(); //To sort a test array of integers + + Double[] decimalArray = {8.2, 1.5, 3.14159265, 9.3, 5.1, 4.8, 2.6}; + TreeSort dts = new TreeSort(new Double(decimalArray[0]).doubleValue()); + for (int i = 1; i < decimalArray.length; i++) { //sorts every index of the list one at a time + dts.decimalInsert(dts.root, new Double(decimalArray[i]).doubleValue()); //builds on the tree from a root node + }//end for + System.out.print("Decimal Array, Sorted in Increasing Order: "); + dts.treeSort(dts.root); + System.out.println(); + + String[] stringArray = {"c", "a", "e", "b","d", "dd","da","zz", "AA", "aa","aB","Hb", "Z"}; + int last = stringArray.length; + Arrays.sort(stringArray); //Uses an imported arrays method to automatically alphabetize + System.out.print("String Array Sorted in Alphabetical Order: "); + ts.insert(ts.root, last); + for(int i=0; i