Skip to content

Commit

Permalink
Format all Java files google style
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfiset committed May 30, 2019
1 parent 1f844cb commit a691337
Show file tree
Hide file tree
Showing 69 changed files with 2,601 additions and 3,174 deletions.
18 changes: 16 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
apply plugin: 'java'
apply plugin: "com.github.sherter.google-java-format"

buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.github.sherter.google-java-format:google-java-format-gradle-plugin:0.8"
}
}


// Assume Java 8
sourceCompatibility = 1.8
Expand All @@ -10,7 +23,6 @@ repositories {
}

dependencies {

// JUnit framework
testCompile 'junit:junit:4.+'
compile 'junit:junit:4.+'
Expand All @@ -29,14 +41,16 @@ dependencies {

// Apache commons lang
compile 'org.apache.commons:commons-lang3:3.6'

// Google java formatter to format source code.
compile 'com.google.googlejavaformat:google-java-format:1.7'
}

// Tests display stdout and stderr
test {
dependsOn cleanTest
testLogging.showStandardStreams = true
//useJUnitPlatform()

}


Expand Down
133 changes: 55 additions & 78 deletions com/williamfiset/datastructures/balancedtree/AVLTreeRecursive.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/**
* This file contains an implementation of an AVL tree. An AVL tree
* is a special type of binary tree which self balances itself to keep
* operations logarithmic.
* This file contains an implementation of an AVL tree. An AVL tree is a special type of binary tree
* which self balances itself to keep operations logarithmic.
*
* @author William Fiset, william.alexandre.fiset@gmail.com
**/
*/
package com.williamfiset.datastructures.balancedtree;

import com.williamfiset.datastructures.utils.TreePrinter;
import com.williamfiset.datastructures.utils.TreePrinter.PrintableNode;

public class AVLTreeRecursive <T extends Comparable<T>> implements Iterable<T> {
public class AVLTreeRecursive<T extends Comparable<T>> implements Iterable<T> {

public class Node implements PrintableNode {

public class Node implements PrintableNode {

// 'bf' is short for Balance Factor
public int bf;

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

// The left and the right children of this node.
// The left and the right children of this node.
public Node left, right;

public Node(T value) {
this.value = value;
}

@Override
@Override
public PrintableNode getLeft() {
return left;
}
Expand All @@ -44,7 +43,6 @@ public PrintableNode getRight() {
public String getText() {
return value.toString();
}

}

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

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

// Recursive contains helper method.
private boolean contains(Node node, T value) {

if (node == null) return false;

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

// Found value in tree.
return true;

}

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

// Inserts a value inside the AVL tree.
private Node insert(Node node, T value) {

// Base case.
if (node == null) return new Node(value);

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

// Insert node in left subtree.
if (cmp < 0) {
node.left = insert(node.left, value);;
node.left = insert(node.left, value);
;

// Insert node in right subtree.
// Insert node in right subtree.
} else {
node.right = insert(node.right, value);
}
Expand All @@ -129,21 +127,19 @@ private Node insert(Node node, T value) {

// Re-balance tree.
return balance(node);

}

// Update a node's height and balance factor.
private void update(Node node) {
int leftNodeHeight = (node.left == null) ? -1 : node.left.height;

int leftNodeHeight = (node.left == null) ? -1 : node.left.height;
int rightNodeHeight = (node.right == null) ? -1 : node.right.height;

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

// Update balance factor.
node.bf = rightNodeHeight - leftNodeHeight;

}

// Re-balance a node if its balance factor is +2 or -2.
Expand All @@ -155,29 +151,27 @@ private Node balance(Node node) {
// Left-Left case.
if (node.left.bf <= 0) {
return leftLeftCase(node);
// Left-Right case.

// Left-Right case.
} else {
return leftRightCase(node);
}

// Right heavy subtree needs balancing.
// Right heavy subtree needs balancing.
} else if (node.bf == +2) {

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

// Right-Left case.
// Right-Left case.
} else {
return rightLeftCase(node);
}

}

// Node either has a balance factor of 0, +1 or -1 which is fine.
return node;

}

private Node leftLeftCase(Node node) {
Expand Down Expand Up @@ -232,41 +226,41 @@ public boolean remove(T elem) {

// Removes a value from the AVL tree.
private Node remove(Node node, T elem) {

if (node == null) return null;

int cmp = elem.compareTo(node.value);

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

// Dig into right subtree, the value we're looking
// for is greater than the current value.
// Dig into right subtree, the value we're looking
// for is greater than the current value.
} else if (cmp > 0) {
node.right = remove(node.right, elem);

// Found the node we wish to remove.
// Found the node we wish to remove.
} else {

// This is the case with only a right subtree or no subtree at all.
// This is the case with only a right subtree or no subtree at all.
// In this situation just swap the node we wish to remove
// with its right child.
if (node.left == null) {
return node.right;
// This is the case with only a left subtree or
// no subtree at all. In this situation just
// swap the node we wish to remove with its left child.

// This is the case with only a left subtree or
// no subtree at all. In this situation just
// swap the node we wish to remove with its left child.
} else if (node.right == null) {
return node.left;

// When removing a node from a binary tree with two links the
// successor of the node being removed can either be the largest
// value in the left subtree or the smallest value in the right
// subtree. As a heuristic, I will remove from the subtree with
// the greatest hieght in hopes that this may help with balancing.
// When removing a node from a binary tree with two links the
// successor of the node being removed can either be the largest
// value in the left subtree or the smallest value in the right
// subtree. As a heuristic, I will remove from the subtree with
// the greatest hieght in hopes that this may help with balancing.
} else {

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

} else {

// Swap the value of the successor into the node.
T successorValue = findMin(node.right);
node.value = successorValue;
Expand All @@ -298,60 +292,60 @@ private Node remove(Node node, T elem) {

// Re-balance tree.
return balance(node);

}

// Helper method to find the leftmost node (which has the smallest value)
private T findMin(Node node) {
while(node.left != null)
node = node.left;
while (node.left != null) node = node.left;
return node.value;
}

// Helper method to find the rightmost node (which has the largest value)
private T findMax(Node node) {
while(node.right != null)
node = node.right;
while (node.right != null) node = node.right;
return node.value;
}

// Returns as iterator to traverse the tree in order.
public java.util.Iterator<T> iterator () {
public java.util.Iterator<T> iterator() {

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

return new java.util.Iterator<T> () {
return new java.util.Iterator<T>() {
Node trav = root;
@Override

@Override
public boolean hasNext() {
if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException();
if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException();
return root != null && !stack.isEmpty();
}
@Override
public T next () {


@Override
public T next() {

if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException();

while(trav != null && trav.left != null) {
while (trav != null && trav.left != null) {
stack.push(trav.left);
trav = trav.left;
}

Node node = stack.pop();

if (node.right != null) {
stack.push(node.right);
trav = node.right;
}

return node.value;
}
@Override

@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
};
}

Expand All @@ -367,25 +361,8 @@ public boolean validateBSTInvarient(Node node) {
if (node == null) return true;
T val = node.value;
boolean isValid = true;
if (node.left != null) isValid = isValid && node.left.value.compareTo(val) < 0;
if (node.left != null) isValid = isValid && node.left.value.compareTo(val) < 0;
if (node.right != null) isValid = isValid && node.right.value.compareTo(val) > 0;
return isValid && validateBSTInvarient(node.left) && validateBSTInvarient(node.right);
}

}
















Loading

0 comments on commit a691337

Please sign in to comment.