-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathBST.java
205 lines (172 loc) · 4.42 KB
/
BST.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import java.lang.Integer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author zmiller
*/
class Node {
public int data;
public Node left;
public Node right;
public Node parent;
public Node(int data) {
this.data = data;
}
}
class BST extends YOP {
/**
* Inserts a value into a BST.
*
* @param data - what to insert
* @param head - where to insert
* @return insert node
*/
public Node insert (int data, Node head) {
if (head == null) {
return new Node(data);
}
if (data < head.data) {
if (head.left != null) {
return insert(data, head.left);
}
head.left = new Node(data);
head.left.parent = head;
return head;
}
if (data > head.data) {
if (head.right != null) {
return insert(data, head.right);
}
head.right = new Node(data);
head.right.parent = head;
return head;
}
// Ignore duplicates
return null;
}
/**
* Removes an element from the list; `n` will remain the head
* after removal;
*
* @param data - value to delete
* @param n - head of subtree
*/
public void remove(int data, Node n) {
remove(search(data, n), n);
}
/**
* Removes an element from the list; `n` will remain the head
* after removal;
*
* @param data - value to delete
* @param n - head of subtree
* @param head - a reference to the head of the tree
*/
protected void remove(Node remove, Node n) {
if (remove == null){
return;
}
// Get the smallest on the right or largest on the left, depending on
// what children are set
Node swap = remove.right != null ? smallest(remove.right) :
remove.left != null ? largest(remove.left) : null;
// Not a leaf node, recurse
if (swap != null) {
remove.data = swap.data;
remove(swap, remove);
}
// Leaf node, unset the reference
else {
Node parent = remove.parent;
parent.left = parent.left != remove ? parent.left : null;
parent.right = parent.right != remove ? parent.right : null;
}
}
/**
* Returns the object containing the provided data point
*
* @param data - value to be deleted
* @param n - tree to look at
*/
protected Node search(int data, Node n) {
// Not found
if (n == null) {
return null;
}
// Found, return the node
if (data == n.data) {
return n;
}
// Search the next tree
Node next = data < n.data ? n.left : n.right;
return search(data, next);
}
/**
* Finds the smallest element in a BST
*
* @param n - root of the tree
* @return the smallest node in the tree
*/
private Node smallest(Node n) {
if (n != null) {
while (n.left != null) {
n = n.left;
}
}
return n;
}
/**
* Finds the largest element in a BST
*
* @param n - root of the tree
* @return the largest node in the tree
*/
private Node largest(Node n) {
if (n != null) {
while (n.right != null) {
n = n.right;
}
}
return n;
}
/**
* Prints a tree in pre-order DFS.
*
* @param head - tree to print
*/
public void print(Node head) {
if (head == null) {
return;
}
System.out.println(head.data);
print(head.left);
print(head.right);
}
/**
* Executes the problem's solution
*
* @param input
*/
@Override
protected void run(String input) {
// Create the BST
String[] split = input.split("\\|");
int remove = Integer.parseInt(split[1].trim());
Node head = null;
for (int i : csvToIntArray(split[0])) {
if (head == null) {
head = new Node(i);
continue;
}
insert(i, head);
}
print(head);
remove(remove, head);
System.out.println();
print(head);
}
public static void main(String args[]) {
launch(new BST());
}
}