Skip to content

Commit 3631533

Browse files
Merge pull request #70 from aphitorite/614
Refactor Tree Sort
2 parents 5ebb2b4 + 74af3a6 commit 3631533

1 file changed

Lines changed: 59 additions & 109 deletions

File tree

src/sorts/TreeSort.java

Lines changed: 59 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
import utils.Reads;
77
import utils.Writes;
88

9-
//TODO: Code's an absolute mess. Refactor it at some point.
109
final public class TreeSort extends Sort {
1110
public TreeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes writeOps) {
1211
super(delayOps, markOps, readOps, writeOps);
1312

1413
this.setSortPromptID("Tree");
15-
this.setRunAllID("Tree Sort");
14+
this.setRunAllID("Tree Sort (Unbalanced)");
1615
this.setReportSortID("Treesort");
1716
this.setCategory("Insertion Sorts");
1817
this.isComparisonBased(true);
@@ -24,122 +23,73 @@ public TreeSort(Delays delayOps, Highlights markOps, Reads readOps, Writes write
2423
}
2524

2625
// Code retrieved from https://www.geeksforgeeks.org/tree-sort/
27-
28-
// Java program to
29-
// implement Tree Sort
30-
final class TreeSorter
31-
{
32-
33-
// Class containing left and
34-
// right child of current
35-
// node and key value
36-
final class Node
37-
{
38-
int key;
39-
Node left, right;
40-
41-
public Node(int item)
42-
{
43-
key = item;
44-
left = right = null;
45-
}
46-
}
47-
48-
// Root of BST
49-
Node root;
50-
51-
//Index for final result
52-
int index;
53-
54-
//Field to store current length
55-
int length;
56-
57-
// Constructor
58-
TreeSorter()
59-
{
60-
root = null;
61-
}
26+
27+
final private class Node {
28+
int key;
29+
Node left, right;
6230

63-
//Method has to be inside TreeSort to access Node class directly
64-
public Node treeWrite(Node element, int at) {
65-
Node node = new Node(0);
66-
67-
if(at < length) Highlights.markArray(1, at - 1);
68-
69-
Writes.changeTempWrites(1);
70-
71-
Writes.startLap();
72-
73-
node = element;
74-
75-
Writes.stopLap();
76-
77-
Delays.sleep(0.25);
78-
79-
return node;
80-
}
81-
82-
// This method mainly
83-
// calls insertRec()
84-
void insert(int key)
85-
{
86-
this.root = treeWrite(insertRec(root, key, 1), 1);
87-
}
31+
public Node(int item) {
32+
key = item;
33+
left = right = null;
34+
}
35+
}
36+
37+
private Node root;
38+
private int index, length;
8839

89-
/* A recursive function to
90-
insert a new key in BST */
91-
Node insertRec(Node root, int key, int depth)
92-
{
40+
private Node treeWrite(Node element, int at) {
41+
Node node = new Node(0);
42+
43+
if(at > 0 && at < this.length) Highlights.markArray(1, at - 1);
44+
Writes.changeTempWrites(1);
45+
Writes.startLap();
46+
node = element;
47+
Writes.stopLap();
48+
49+
Delays.sleep(0.25);
50+
51+
return node;
52+
}
9353

94-
/* If the tree is empty,
95-
return a new node */
96-
if (root == null)
97-
{
98-
root = treeWrite(new Node(key), 1);
99-
return root;
100-
}
54+
private void insert(int key) {
55+
this.root = this.treeWrite(insertRec(this.root, key, 1), 1);
56+
}
57+
58+
private Node insertRec(Node root, int key, int depth) {
59+
if (root == null) {
60+
root = treeWrite(new Node(key), 1);
61+
return root;
62+
}
10163

102-
/* Otherwise, recur
103-
down the tree */
104-
if (Reads.compare(key, root.key) == -1)
105-
root.left = treeWrite(insertRec(root.left, key, depth * 2), depth * 2);
106-
else if (Reads.compare(key, root.key) == 1)
107-
root.right = treeWrite(insertRec(root.right, key, (depth * 2) + 1), (depth * 2) + 1);
64+
if (Reads.compare(key, root.key) == -1)
65+
root.left = treeWrite(insertRec(root.left, key, depth * 2), depth * 2);
66+
else if (Reads.compare(key, root.key) == 1)
67+
root.right = treeWrite(insertRec(root.right, key, (depth * 2) + 1), (depth * 2) + 1);
10868

109-
/* return the root */
110-
return root;
111-
}
69+
return root;
70+
}
11271

113-
// A function to do
114-
// inorder traversal of BST
115-
void inorderRec(Node root, int[] array)
116-
{
117-
if (root != null)
118-
{
119-
inorderRec(root.left, array);
120-
Writes.write(array, this.index++, root.key, 1, true, false);
121-
inorderRec(root.right, array);
122-
}
123-
}
72+
private void traverseRec(Node root, int[] array) {
73+
if (root != null) {
74+
this.traverseRec(root.left, array);
75+
Writes.write(array, this.index++, root.key, 1, true, false);
76+
this.traverseRec(root.right, array);
77+
}
78+
}
12479

125-
void treeins(int arr[], int length)
126-
{
127-
for(int i = 0; i < length; i++)
128-
{
129-
Highlights.markArray(2, i);
130-
insert(arr[i]);
131-
}
132-
Highlights.clearMark(2);
133-
}
134-
135-
}
80+
private void treeIns(int arr[]) {
81+
for(int i = 0; i < this.length; i++) {
82+
Highlights.markArray(2, i);
83+
this.insert(arr[i]);
84+
}
85+
Highlights.clearMark(2);
86+
}
13687

13788
@Override
13889
public void runSort(int[] array, int currentLength, int bucketCount) {
139-
TreeSorter tree = new TreeSorter();
140-
tree.length = currentLength;
141-
tree.treeins(array, tree.length);
142-
tree.index = 0;
143-
tree.inorderRec(tree.root, array);
90+
this.length = currentLength;
91+
this.treeIns(array);
92+
this.index = 0;
93+
this.traverseRec(this.root, array);
14494
}
14595
}

0 commit comments

Comments
 (0)