-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortIt.java
106 lines (95 loc) · 2.82 KB
/
SortIt.java
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
import java.util.ArrayList;
public class SortIt {
public static void main(String[] args)
{
Nodes root = new Nodes();
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(3);
arr.add(5);
arr.add(1);
arr.add(8);
arr.add(9);
arr.add(6);
arr.add(2);
arr.add(7);
arr.add(10);
arr.add(4);
System.out.println("Before:");
for (int element : arr)
System.out.print(element + " ");
System.out.println();
arr = sort(root, arr);
System.out.println("After:");
for (int element : arr)
System.out.print(element + " ");
System.out.println();
}
public static ArrayList<Integer> sort(Nodes root, ArrayList<Integer> list)
{
ArrayList<Integer> newList = new ArrayList<Integer>();
for (int i = 0; i < list.size(); i++)
{
insert(root, list.get(i));
}
newList = inOrder(root);
return newList;
}
public static void insert(Nodes root, int element) {
// Start at root of tree
if (root.value == -12345) {
root.value = element;
return;
}
// if element < root.value, go left
if (element < root.value) {
if (root.leftChild == null) // if left child is null, make a new node with element
{
root.leftChild = new Nodes(element);
root.leftChild.parent = root;
}
else // otherwise keep going to the left
{
insert(root.leftChild, element);
}
}
else {
if (root.rightChild == null) // if right child is null, make a new node with element
{
root.rightChild = new Nodes(element);
root.rightChild.parent = root;
}
else // otherwise keep going to the right
{
insert(root.rightChild, element);
}
}
}
static ArrayList<Integer> sortedList = new ArrayList<Integer>();
public static ArrayList<Integer> inOrder(Nodes root)
{
if (root == null)
return sortedList;
inOrder(root.leftChild);
sortedList.add(root.value);
//System.out.print(root.value + " ");
inOrder(root.rightChild);
return sortedList;
}
}
class Nodes {
int value;
Nodes leftChild;
Nodes rightChild;
Nodes parent;
public Nodes() {
value = -12345;
rightChild = null;
leftChild = null;
parent = null;
}
public Nodes(int value) {
this.value = value;
rightChild = null;
leftChild = null;
}
}