Skip to content

Commit 5f1fb61

Browse files
Merge pull request TheAlgorithms#86 from JosephMPignataro/master
Adding a Node class and Binary Tree Sorting algorithm
2 parents 9b91f6c + 707bf5c commit 5f1fb61

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

Misc/Node.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Node {
2+
public Object anElement;
3+
public Node less;
4+
public Node greater;
5+
6+
public Node(Object theElement) {
7+
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
8+
//obviously the node can still be a child of other elements
9+
}
10+
11+
public Node(Object currentElement, Node lessSide, Node greaterSide) {
12+
anElement = currentElement;
13+
this.less = lessSide;
14+
this.greater = greaterSide;
15+
}
16+
}

Sorts/BinaryTreeSort.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy