forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeSort.java
More file actions
129 lines (111 loc) · 3.84 KB
/
TreeSort.java
File metadata and controls
129 lines (111 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.thealgorithms.sorts;
import static com.thealgorithms.sorts.SortUtils.print;
import com.thealgorithms.datastructures.trees.BSTRecursiveGeneric;
import java.util.List;
/**
* <h1> Implementation of the Tree Sort algorithm</h1>
*
* <p>
* Tree Sort: A sorting algorithm which constructs a Binary Search Tree using
* the unsorted data and then outputs the data by inorder traversal of the tree.
*
* Reference: https://en.wikipedia.org/wiki/Tree_sort
* </p>
*
* @author Madhur Panwar (https://github.com/mdrpanwar)
*/
public class TreeSort implements SortAlgorithm {
@Override
public <T extends Comparable<T>> T[] sort(T[] unsortedArray) {
return doTreeSortArray(unsortedArray);
}
@Override
public <T extends Comparable<T>> List<T> sort(List<T> unsortedList) {
return doTreeSortList(unsortedList);
}
private <T extends Comparable<T>> T[] doTreeSortArray(T[] unsortedArray) {
// create a generic BST tree
BSTRecursiveGeneric<T> tree = new BSTRecursiveGeneric<T>();
// add all elements to the tree
for (T element : unsortedArray) {
tree.add(element);
}
// get the sorted list by inorder traversal of the tree
List<T> sortedList = tree.inorderSort();
// add the elements back to the initial array
int i = 0;
for (T element : sortedList) {
unsortedArray[i++] = element;
}
// return the array
return unsortedArray;
}
private <T extends Comparable<T>> List<T> doTreeSortList(
List<T> unsortedList
) {
// create a generic BST tree
BSTRecursiveGeneric<T> tree = new BSTRecursiveGeneric<T>();
// add all elements to the tree
for (T element : unsortedList) {
tree.add(element);
}
// get the sorted list by inorder traversal of the tree and return it
return tree.inorderSort();
}
public static void main(String[] args) {
TreeSort treeSort = new TreeSort();
// ==== Integer Array =======
System.out.println("Testing for Integer Array....");
Integer[] a = { 3, -7, 45, 1, 343, -5, 2, 9 };
System.out.print(String.format("%-10s", "unsorted: "));
print(a);
a = treeSort.sort(a);
System.out.print(String.format("%-10s", "sorted: "));
print(a);
System.out.println();
// ==== Integer List =======
System.out.println("Testing for Integer List....");
List<Integer> intList = List.of(3, -7, 45, 1, 343, -5, 2, 9);
System.out.print(String.format("%-10s", "unsorted: "));
print(intList);
intList = treeSort.sort(intList);
System.out.print(String.format("%-10s", "sorted: "));
print(intList);
System.out.println();
// ==== String Array =======
System.out.println("Testing for String Array....");
String[] b = {
"banana",
"berry",
"orange",
"grape",
"peach",
"cherry",
"apple",
"pineapple",
};
System.out.print(String.format("%-10s", "unsorted: "));
print(b);
b = treeSort.sort(b);
System.out.print(String.format("%-10s", "sorted: "));
print(b);
System.out.println();
// ==== String List =======
System.out.println("Testing for String List....");
List<String> stringList = List.of(
"banana",
"berry",
"orange",
"grape",
"peach",
"cherry",
"apple",
"pineapple"
);
System.out.print(String.format("%-10s", "unsorted: "));
print(stringList);
stringList = treeSort.sort(stringList);
System.out.print(String.format("%-10s", "sorted: "));
print(stringList);
}
}