Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions src/main/java/com/thealgorithms/sorts/TreeSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static com.thealgorithms.sorts.SortUtils.print;

import com.thealgorithms.datastructures.trees.BSTRecursiveGeneric;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -30,38 +32,39 @@ public <T extends Comparable<T>> List<T> sort(List<T> 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;
// sort through the list path and copy the result back into the initial array
List<T> sortedList = doTreeSortList(Arrays.asList(unsortedArray));
return sortedList.toArray(unsortedArray);
}

private <T extends Comparable<T>> List<T> doTreeSortList(Iterable<T> unsortedList) {
// create a generic BST tree
BSTRecursiveGeneric<T> tree = new BSTRecursiveGeneric<T>();
// the BST ignores duplicate keys, so pair every element with its position to keep equal values apart
BSTRecursiveGeneric<Keyed<T>> tree = new BSTRecursiveGeneric<>();

// add all elements to the tree
int position = 0;
for (T element : unsortedList) {
tree.add(element);
tree.add(new Keyed<>(element, position++));
}

// get the sorted list by inorder traversal of the tree and return it
return tree.inorderSort();
List<T> sortedList = new ArrayList<>();
for (Keyed<T> keyed : tree.inorderSort()) {
sortedList.add(keyed.value());
}
return sortedList;
}

/**
* An element plus its original position. Equal elements are ordered by position,
* which also makes the sort stable.
*/
private record Keyed<T extends Comparable<T>>(T value, int position) implements Comparable<Keyed<T>> {
@Override
public int compareTo(Keyed<T> other) {
int byValue = value.compareTo(other.value);
return byValue != 0 ? byValue : Integer.compare(position, other.position);
}
}

public static void main(String[] args) {
Expand Down
13 changes: 12 additions & 1 deletion src/test/java/com/thealgorithms/sorts/TreeSortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@
* @see TreeSort
*/

public class TreeSortTest {
public class TreeSortTest extends SortingAlgorithmTest {
private TreeSort treeSort = new TreeSort();

@Override
SortAlgorithm getSortAlgorithm() {
return new TreeSort();
}

@Test
public void treeSortKeepsDuplicates() {
Integer[] array = {-1, -1, 0};
assertArrayEquals(new Integer[] {-1, -1, 0}, treeSort.sort(array));
}

@Test
public void treeSortEmptyArray() {
Integer[] inputArray = {};
Expand Down
Loading