Skip to content

Commit a691337

Browse files
committed
Format all Java files google style
1 parent 1f844cb commit a691337

File tree

69 files changed

+2601
-3174
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+2601
-3174
lines changed

build.gradle

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
apply plugin: 'java'
2+
apply plugin: "com.github.sherter.google-java-format"
3+
4+
buildscript {
5+
repositories {
6+
maven {
7+
url "https://plugins.gradle.org/m2/"
8+
}
9+
}
10+
dependencies {
11+
classpath "gradle.plugin.com.github.sherter.google-java-format:google-java-format-gradle-plugin:0.8"
12+
}
13+
}
14+
215

316
// Assume Java 8
417
sourceCompatibility = 1.8
@@ -10,7 +23,6 @@ repositories {
1023
}
1124

1225
dependencies {
13-
1426
// JUnit framework
1527
testCompile 'junit:junit:4.+'
1628
compile 'junit:junit:4.+'
@@ -29,14 +41,16 @@ dependencies {
2941

3042
// Apache commons lang
3143
compile 'org.apache.commons:commons-lang3:3.6'
44+
45+
// Google java formatter to format source code.
46+
compile 'com.google.googlejavaformat:google-java-format:1.7'
3247
}
3348

3449
// Tests display stdout and stderr
3550
test {
3651
dependsOn cleanTest
3752
testLogging.showStandardStreams = true
3853
//useJUnitPlatform()
39-
4054
}
4155

4256

com/williamfiset/datastructures/balancedtree/AVLTreeRecursive.java

Lines changed: 55 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
/**
2-
* This file contains an implementation of an AVL tree. An AVL tree
3-
* is a special type of binary tree which self balances itself to keep
4-
* operations logarithmic.
2+
* This file contains an implementation of an AVL tree. An AVL tree is a special type of binary tree
3+
* which self balances itself to keep operations logarithmic.
54
*
65
* @author William Fiset, william.alexandre.fiset@gmail.com
7-
**/
6+
*/
87
package com.williamfiset.datastructures.balancedtree;
98

109
import com.williamfiset.datastructures.utils.TreePrinter;
1110
import com.williamfiset.datastructures.utils.TreePrinter.PrintableNode;
1211

13-
public class AVLTreeRecursive <T extends Comparable<T>> implements Iterable<T> {
12+
public class AVLTreeRecursive<T extends Comparable<T>> implements Iterable<T> {
13+
14+
public class Node implements PrintableNode {
1415

15-
public class Node implements PrintableNode {
16-
1716
// 'bf' is short for Balance Factor
1817
public int bf;
1918

@@ -23,14 +22,14 @@ public class Node implements PrintableNode {
2322
// The height of this node in the tree.
2423
public int height;
2524

26-
// The left and the right children of this node.
25+
// The left and the right children of this node.
2726
public Node left, right;
2827

2928
public Node(T value) {
3029
this.value = value;
3130
}
3231

33-
@Override
32+
@Override
3433
public PrintableNode getLeft() {
3534
return left;
3635
}
@@ -44,7 +43,6 @@ public PrintableNode getRight() {
4443
public String getText() {
4544
return value.toString();
4645
}
47-
4846
}
4947

5048
// The root node of the AVL tree.
@@ -54,7 +52,7 @@ public String getText() {
5452
private int nodeCount = 0;
5553

5654
// The height of a rooted tree is the number of edges between the tree's
57-
// root and its furthest leaf. This means that a tree containing a single
55+
// root and its furthest leaf. This means that a tree containing a single
5856
// node has a height of 0.
5957
public int height() {
6058
if (root == null) return 0;
@@ -78,7 +76,7 @@ public boolean contains(T value) {
7876

7977
// Recursive contains helper method.
8078
private boolean contains(Node node, T value) {
81-
79+
8280
if (node == null) return false;
8381

8482
// Compare current value to the value in the node.
@@ -92,7 +90,6 @@ private boolean contains(Node node, T value) {
9290

9391
// Found value in tree.
9492
return true;
95-
9693
}
9794

9895
// Insert/add a value to the AVL tree. The value must not be null, O(log(n))
@@ -108,7 +105,7 @@ public boolean insert(T value) {
108105

109106
// Inserts a value inside the AVL tree.
110107
private Node insert(Node node, T value) {
111-
108+
112109
// Base case.
113110
if (node == null) return new Node(value);
114111

@@ -117,9 +114,10 @@ private Node insert(Node node, T value) {
117114

118115
// Insert node in left subtree.
119116
if (cmp < 0) {
120-
node.left = insert(node.left, value);;
117+
node.left = insert(node.left, value);
118+
;
121119

122-
// Insert node in right subtree.
120+
// Insert node in right subtree.
123121
} else {
124122
node.right = insert(node.right, value);
125123
}
@@ -129,21 +127,19 @@ private Node insert(Node node, T value) {
129127

130128
// Re-balance tree.
131129
return balance(node);
132-
133130
}
134131

135132
// Update a node's height and balance factor.
136133
private void update(Node node) {
137-
138-
int leftNodeHeight = (node.left == null) ? -1 : node.left.height;
134+
135+
int leftNodeHeight = (node.left == null) ? -1 : node.left.height;
139136
int rightNodeHeight = (node.right == null) ? -1 : node.right.height;
140137

141138
// Update this node's height.
142139
node.height = 1 + Math.max(leftNodeHeight, rightNodeHeight);
143140

144141
// Update balance factor.
145142
node.bf = rightNodeHeight - leftNodeHeight;
146-
147143
}
148144

149145
// Re-balance a node if its balance factor is +2 or -2.
@@ -155,29 +151,27 @@ private Node balance(Node node) {
155151
// Left-Left case.
156152
if (node.left.bf <= 0) {
157153
return leftLeftCase(node);
158-
159-
// Left-Right case.
154+
155+
// Left-Right case.
160156
} else {
161157
return leftRightCase(node);
162158
}
163159

164-
// Right heavy subtree needs balancing.
160+
// Right heavy subtree needs balancing.
165161
} else if (node.bf == +2) {
166162

167163
// Right-Right case.
168164
if (node.right.bf >= 0) {
169165
return rightRightCase(node);
170166

171-
// Right-Left case.
167+
// Right-Left case.
172168
} else {
173169
return rightLeftCase(node);
174170
}
175-
176171
}
177172

178173
// Node either has a balance factor of 0, +1 or -1 which is fine.
179174
return node;
180-
181175
}
182176

183177
private Node leftLeftCase(Node node) {
@@ -232,41 +226,41 @@ public boolean remove(T elem) {
232226

233227
// Removes a value from the AVL tree.
234228
private Node remove(Node node, T elem) {
235-
229+
236230
if (node == null) return null;
237-
231+
238232
int cmp = elem.compareTo(node.value);
239233

240234
// Dig into left subtree, the value we're looking
241235
// for is smaller than the current value.
242236
if (cmp < 0) {
243237
node.left = remove(node.left, elem);
244238

245-
// Dig into right subtree, the value we're looking
246-
// for is greater than the current value.
239+
// Dig into right subtree, the value we're looking
240+
// for is greater than the current value.
247241
} else if (cmp > 0) {
248242
node.right = remove(node.right, elem);
249243

250-
// Found the node we wish to remove.
244+
// Found the node we wish to remove.
251245
} else {
252246

253-
// This is the case with only a right subtree or no subtree at all.
247+
// This is the case with only a right subtree or no subtree at all.
254248
// In this situation just swap the node we wish to remove
255249
// with its right child.
256250
if (node.left == null) {
257251
return node.right;
258-
259-
// This is the case with only a left subtree or
260-
// no subtree at all. In this situation just
261-
// swap the node we wish to remove with its left child.
252+
253+
// This is the case with only a left subtree or
254+
// no subtree at all. In this situation just
255+
// swap the node we wish to remove with its left child.
262256
} else if (node.right == null) {
263257
return node.left;
264258

265-
// When removing a node from a binary tree with two links the
266-
// successor of the node being removed can either be the largest
267-
// value in the left subtree or the smallest value in the right
268-
// subtree. As a heuristic, I will remove from the subtree with
269-
// the greatest hieght in hopes that this may help with balancing.
259+
// When removing a node from a binary tree with two links the
260+
// successor of the node being removed can either be the largest
261+
// value in the left subtree or the smallest value in the right
262+
// subtree. As a heuristic, I will remove from the subtree with
263+
// the greatest hieght in hopes that this may help with balancing.
270264
} else {
271265

272266
// Choose to remove from left subtree
@@ -280,7 +274,7 @@ private Node remove(Node node, T elem) {
280274
node.left = remove(node.left, successorValue);
281275

282276
} else {
283-
277+
284278
// Swap the value of the successor into the node.
285279
T successorValue = findMin(node.right);
286280
node.value = successorValue;
@@ -298,60 +292,60 @@ private Node remove(Node node, T elem) {
298292

299293
// Re-balance tree.
300294
return balance(node);
301-
302295
}
303296

304297
// Helper method to find the leftmost node (which has the smallest value)
305298
private T findMin(Node node) {
306-
while(node.left != null)
307-
node = node.left;
299+
while (node.left != null) node = node.left;
308300
return node.value;
309301
}
310302

311303
// Helper method to find the rightmost node (which has the largest value)
312304
private T findMax(Node node) {
313-
while(node.right != null)
314-
node = node.right;
305+
while (node.right != null) node = node.right;
315306
return node.value;
316307
}
317308

318309
// Returns as iterator to traverse the tree in order.
319-
public java.util.Iterator<T> iterator () {
310+
public java.util.Iterator<T> iterator() {
320311

321312
final int expectedNodeCount = nodeCount;
322313
final java.util.Stack<Node> stack = new java.util.Stack<>();
323314
stack.push(root);
324315

325-
return new java.util.Iterator<T> () {
316+
return new java.util.Iterator<T>() {
326317
Node trav = root;
327-
@Override
318+
319+
@Override
328320
public boolean hasNext() {
329-
if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException();
321+
if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException();
330322
return root != null && !stack.isEmpty();
331323
}
332-
@Override
333-
public T next () {
334-
324+
325+
@Override
326+
public T next() {
327+
335328
if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException();
336329

337-
while(trav != null && trav.left != null) {
330+
while (trav != null && trav.left != null) {
338331
stack.push(trav.left);
339332
trav = trav.left;
340333
}
341-
334+
342335
Node node = stack.pop();
343-
336+
344337
if (node.right != null) {
345338
stack.push(node.right);
346339
trav = node.right;
347340
}
348-
341+
349342
return node.value;
350343
}
351-
@Override
344+
345+
@Override
352346
public void remove() {
353347
throw new UnsupportedOperationException();
354-
}
348+
}
355349
};
356350
}
357351

@@ -367,25 +361,8 @@ public boolean validateBSTInvarient(Node node) {
367361
if (node == null) return true;
368362
T val = node.value;
369363
boolean isValid = true;
370-
if (node.left != null) isValid = isValid && node.left.value.compareTo(val) < 0;
364+
if (node.left != null) isValid = isValid && node.left.value.compareTo(val) < 0;
371365
if (node.right != null) isValid = isValid && node.right.value.compareTo(val) > 0;
372366
return isValid && validateBSTInvarient(node.left) && validateBSTInvarient(node.right);
373367
}
374-
375368
}
376-
377-
378-
379-
380-
381-
382-
383-
384-
385-
386-
387-
388-
389-
390-
391-

0 commit comments

Comments
 (0)